Search code examples
stringdelphipointersdelphi-2010

Concat PChar with string in Delphi


I need to construct a string and send it via PostMessage, ie.

FileName := String_1 + String_2 + String_3;
PostMessage(FWndHandle, WM_BLA_BLA, NotifyData^.Action, LParam(FileName));

but something isn't working. Plus, FileName is a PChar. The code looks like this:

var
   FileName : PChar;
   Directory_Str : String;
   AnotherString : String;
begin
    // Get memory for filename and fill it with data
    GetMem(FileName, NotifyData^.FileNameLength + SizeOf(WideChar));
    Move(NotifyData^.FileName, Pointer(FileName)^, NotifyData^.FileNameLength);
    PWord(Cardinal(FileName) + NotifyData^.FileNameLength)^ := 0;

    // TODO: Contact string before sending message
    // FileName := AnotherString + Directory_Str + FileName;

    PostMessage(FWndHandle, WM_BLA_BLA, NotifyData^.Action, LParam(FileName));

    ...
end;

Now I need to do contact another string to the variable FileName before calling PostMessage, ie.

FileName := AnotherString + Directory_Str + FileName;
PostMessage(FWndHandle, WM_BLA_BLA, NotifyData^.Action, LParam(FileName));

This would work if FileName was a string, which is not the case here.

Anyone knows how to do that with PChar? I tried these methods, works sometimes but always something breaks at the end:

StrPCopy(FileName, FDirectory + String(FileName));

OR

FileName := PChar(AnotherString + Directory_Str + FileName);

Solution

  • You cannot easily use PostMessage with data passed by reference. The reason being that PostMessage executes asynchronously and you need to keep the memory you are passing alive until the message has been processed by its recipient. I guess that's what is behind your GetMem code.

    Obviously this only works within the same process. And you will also find that Windows won't let you use PostMessage for any of its messages that receive pointers. For example, PostMessage with WM_SETTEXT always fails. You can only hope to do this using a user-defined message. And of course you'll need to deallocate the memory in the code that receives the message.

    I'm going to assume that you are using a user defined message that does allow sending a string with PostMessage. In which case you already have the solution. Do the concatenation using string variables and then use the first block of code in your answer.

    Although you can make it much cleaner like this:

    function HeapAllocatedPChar(const Value: string): PChar;
    var
      bufferSize: Integer;
    begin
      bufferSize := (Length(Value)+1)*SizeOf(Char);
      GetMem(Result, bufferSize);
      Move(PChar(Value)^, Result^, bufferSize);
    end;
    
    procedure PostString(Window: HWND; Msg: UINT; wParam: WPARAM; 
      const Value: string);
    var
      P: PChar;
    begin
      P := HeapAllocatedPChar(Value);
      if not PostMessage(Window, Msg, wParam, LPARAM(P)) then
        FreeMem(P);
    end;
    

    And you can just call that procedure like this:

    PostString(FWndHandle, WM_BLA_BLA, NotifyData^.Action, FDirectory + FileName);
    

    Your current code fails because:

    1. When you call StrPCopy you don't allocate any memory for the longer string.
    2. When you write PChar(AnotherString + Directory_Str + FileName) you fall into the trap of that you were trying to avoid with GetMem. That's a local string which has been deallocated by the time the message is processed.

    If you can find a way of solving your problem without using PostMessage to pass a string, that might be preferable to all this complexity.