While the thread is in the same process (so it has the same address space), your code should work. This, however, is unnecessarily complicated and has a memory leak ( PasswordBuffer
never freed).
You can use a string variable in the stream and pass the address to your internal pre-allocated buffer in the main stream:
type TTestThread = class(TThread) private fHwnd: HWND; protected procedure Execute; override; public constructor Create(AWnd: HWND); end; constructor TTestThread.Create(AWnd: HWND); begin fHwnd := AWnd; inherited Create(False); end; procedure TTestThread.Execute; const MAXLEN = 1024; var s: string; begin SetLength(s, MAXLEN); if SendMessage(fHwnd, WM_GETPASSWORD, MAXLEN, LPARAM(@s[1])) > 0 then begin s := PChar(s); // don't use VCL here Windows.MessageBox(0, PChar('password is "' + s + '"'), 'password', MB_ICONINFORMATION or MB_OK); end; end;
In the main stream, the password is placed in a buffer whose length is limited by the size of the buffer:
procedure TForm1.WMGetPassword(var AMsg: TMessage); var Pwd: string; begin if InputQuery('Password Entry', 'Please enter the password:', Pwd) and (Pwd <> '') then begin StrPLCopy(PChar(AMsg.LParam), Pwd, AMsg.WParam); AMsg.Result := 1; end else AMsg.Result := -1; end;
mghie source share