Search code examples
stringdelphidelphi-7pchar

Ask user and send the Response back in a Message-Receiver


I want to ask the user to input a password. As the password is sometimes needed in a different thread than the main thread where VCL runs, I tried to send a Message to the main window and ask for the password. Then the main window asks the user.

How I ask the user:

procedure TMainForm.WMGetPassword(var Msg: TMessage);
var
  Password: String;
begin
  if QueryPassword(Password) then // function QueryPassword(out Password: String): boolean;
  begin
    Password := Password + #0; // Add #0-Terminator
    Move(Password[1], Msg.wParam, Length(Password) * sizeOf(Char)); // Copy the String in my buffer
    Msg.Result := 1;
  end
  else
  begin
    Msg.Result := 0;
  end;
end;

How I ask the main window:

var
  PasswordBuffer: PChar;
  Password: String;
begin
  PasswordBuffer := AllocMem(100 * sizeof(Char));
  PasswordResult := SendMessage(MainFormHWND, WM_GetPassword, Integer(PasswordBuffer), 0);
  Result := (PasswordResult <> -1);
  if not Result then
    Exit;

  SetString(Password, PasswordBuffer, 100);
  ShowMessage(Password);
end;

But Password and PasswordBuffer are empty afterwards. What am I doing wrong?


Solution

  • As long as the thread is in the same process (so it shares the same address space) your code should work. It is however needlessly complicated and has a memory leak (PasswordBuffer is never freed).

    You can use a string variable in the thread and pass an address to its internal preallocated buffer to the main thread:

    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 thread the password is put into the buffer, length-limited to the buffer size:

    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;