Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:22:22+00:00 2026-06-14T13:22:22+00:00

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

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:22:23+00:00Added an answer on June 14, 2026 at 1:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to construct the URL of a page in a String, to send
I need to construct a long string with javascript. Thats how i tried to
I need to send a string from C# to a C++ WindowProc. There are
I've little knowledge of Java. I need to construct a string representation of an
I need to construct a form who's action takes you back to the exact
Need to send the values of a Sqlite table to the server via an
I need to construct a link in Wordpress based on a variable out of
I need to construct an if statement from the data coming from the client
I need to construct a file path inside a java program. Which path separator
I need to construct directed graph (at runtime) with cycles in Prolog and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.