1

I have two VC++ applications running, both written by me. They are not two instances of the same EXE; they are completely different projects. I want to send a string from one to the other.

First application has the following code:

HWND tgtHwnd = FindWindow(_T("Target_Class"), _T("Target_Name"));
SendMessage(tgtHwnd, 1234, 0, (LPARAM)L"Hello");

The second has the following code to process this message:

// snippet of the WndProc function

case 1234:
  LPCWSTR myText = (LPCWSTR)lParam;
  MessageBox(NULL, myText, _T("My Text"), 0);

// End snippet

When I run it, and pass the message, I get an access violation error in the receiving application.

enter image description here

Tried to search conversion to and from LPCWSTR and LPARAM, but couldn't find any helpful example. Even more confused with the Bad Ptr error. I remember reading that the pointer that is sent might not be available to the second process or something like that. But I have no clue where to start looking.

How can I send the string from one EXE to another?

1 Answer 1

2

Surprised it got that far to be honest. No idea what Casting "Hello" to long is doing, but it's only going to be inside the sending exe and unless you are only sending four ascii chars not a lot of use.

Look for WM_CopyData, it was designed just for this.

Sign up to request clarification or add additional context in comments.

7 Comments

The cast to LPARAM was to silence a compiler warning. Of course, it won't work since all it does is convert the address of the string into an LPARAM and sending that. And that address is useless, since each app has its own address space. WM_COPYDATA is the correct solution.
WM_COPYDATA worked. Thanks. Because apps have own address space, I'm curious - who else uses LPARAM to send data to the app, apart from the OS itself? How else is LPARAM used?
A lot of window messages use LPARAM to pass pointers around. But they are only sent and received within the same address space, thus the pointers remain valid. Only a few select window messages are able to marshal pointers across process boundaries, and WM_COPYDATA is one of them.
It was to silence a compiler warning? Oh that's alright then. Explains everything that does, even why it was never going to work..
"The cast to LPARAM was to silence a compiler warning." says it all. Casts are for very specific circumstances and shouldn't normally be required. They should NEVER be used just to shut up the compiler which is complaining for a reason.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.