Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi InternalGetWindowText & Unicode String ---> String (https://www.delphipraxis.net/239-internalgetwindowtext-unicode-string-string.html)

toms 24. Jun 2002 15:33


InternalGetWindowText & Unicode String ---> String
 
Hi,

Wie wandelt man am einfachsten einen Unicode String in einen String um?

lpString hat den Wert
('F', #0, 'o', #0, 'r', #0, 'm', #0, '1', #0, #0, #0, 'A', #10, #1, '&', #0, #0, ...)
Wobei dann Showmessage nur 'F' anzeigt.
(Klar, wegen den null-term. Strings)


Code:
function NT_InternalGetWindowText(Wnd: HWND): String;
type
  TInternalGetWindowText = function(Wnd: HWND;lpString: PChar;nMaxCount: Integer): Integer; stdcall;
var
  hUserDll: THandle;
  InternalGetWindowText: TInternalGetWindowText;
  lpString: array[0..MAX_PATH] of Char; //Buffer for window caption
  oemStr : PChar;
begin
  Result := '';
  hUserDll := GetModuleHandle('user32.dll');
  if (hUserDll > 0) then
  begin
    @InternalGetWindowText := GetProcAddress(hUserDll, 'InternalGetWindowText');
    if Assigned(InternalGetWindowText) then
    begin
      InternalGetWindowText(Wnd, lpString, SizeOf(lpString));
      Result := StrPas(lpString);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(NT_InternalGetWindowText(handle));
end;

sakura 24. Jun 2002 15:37

Folgende drei Zeilen müssen geändert werden.

lpString: array[0..MAX_PATH] of WideChar; //Buffer for window caption

InternalGetWindowText(Wnd, @lpString, SizeOf(lpString));
Result := AnsiString(lpString);

Das wars.

toms 24. Jun 2002 15:39

Danke.

sakura 24. Jun 2002 15:42

Okay, die letzte Möglichkeit war nicht perfekt. Diese Lösung ist wohl besser.
Code:
function NT_InternalGetWindowText(Wnd: HWND): String;
type
  TInternalGetWindowText = function(Wnd: HWND;lpString: PWideChar;nMaxCount: Integer): Integer; stdcall;
var
  hUserDll: THandle;
  InternalGetWindowText: TInternalGetWindowText;
  lpString: array[0..MAX_PATH] of WideChar; //Buffer for window caption
  oemStr : PChar;
begin
  Result := '';
  hUserDll := GetModuleHandle('user32.dll');
  if (hUserDll > 0) then
  begin
    @InternalGetWindowText := GetProcAddress(hUserDll, 'InternalGetWindowText');
    if Assigned(InternalGetWindowText) then
    begin
      InternalGetWindowText(Wnd, lpString, SizeOf(lpString));
      Result := String(lpString);
    end;
  end;
end;
Änderungen zu Deiner Lösung:

TInternalGetWindowText = function(Wnd: HWND;lpString: PWideChar;nMaxCount: Integer): Integer; stdcall;

lpString: array[0..MAX_PATH] of WideChar; //Buffer for window caption

Result := String(lpString);


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:14 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz