Sending / receiving a string through PostMessage

Although there are already several resources on the Internet that address this gross topic, I still haven't found an answer that works for me.

I want to have a complete connection between my VB.net process and my C ++ process. I would like to be able to send a string to and from a C ++ process, but for now I need to achieve:

Sending a string to a C ++ process and processing it.

This creates a few points that I'm not sure about, but I will try to make it as simple as possible ...

Using the following function declaration in VB ;

Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _ ByVal hWnd As IntPtr, _ ByVal Msg As UInteger, _ ByVal wParam As IntPtr, _ ByVal lParam As String _ ) As Boolean 

And send a message like this:

 PostMessage(hWnd, SM_PING, Nothing, "schlampe") 

With the following method declaration for capturing a message in C ++ ;

 LRESULT CALLBACK newWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 

And to check if I can access the string with;

 char buffer[50]; sprintf(buffer, "Received: %s", (char *)lParam); MsgBox(buffer); 


I looked through a lot of details, which, in my opinion, are not needed, but ask, and this will be given to you.

My problem is that the message received and "processed" ... but the message field created by the C ++ process does not contain my test message (it reads: "Received").

So, how can I send a string via PostMessage / SendMessage from VB to C ++?




Decision:

See the accepted answer for the solution ... but what's more, here is how I get the string (C ++):

 LRESULT CALLBACK newWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_COPYDATA: MsgBox("Received a WM_COPYDATA message"); COPYDATASTRUCT * pcds = (COPYDATASTRUCT *)lParam; LPCTSTR lpszString = (LPCTSTR)(pcds->lpData); MsgBox(lpszString); return 1L; } return CallWindowProc(instance->OriginalProcessor(), hwnd, uMsg, wParam, lParam); } 


And finally, I used the IPC example here to send a message. This example sends a message using C #, but the concept was all I needed (not to mention that it was a walk in the park to convert such code to VB). Note that in my VB implementation, I did not have to break the line with a null character.

+6
source share
1 answer

When using Windows messages, you must use WM_COPYDATA to transfer string data between processes. If you use custom message identifiers, then the string data will not be distributed between two different process address spaces.

And that is why your current code crashes. The receive process is passed to the lParam pointer to memory in the address space of the calling processes. And of course, this is pointless in another process.

Although there are other ways to route data like this between processes with Windows messages, WM_COPYDATA by far the easiest. If your requirement becomes much more complex, you may need a more comprehensive IPC approach than Windows messaging.

+6
source

Source: https://habr.com/ru/post/915910/


All Articles