1

I'm trying to send a windowsmessage from an app in C# (Compact Frame) to another one in C++ (MFC), both of them in a PDA.

In the receiver app (MFC) I realize that I receive the message but I can't read the string message, it shows a little square.

This is the code of the Sender (C#)

OpenNETCF.Win32.Win32Window.SendMessage(hwndVal, typeMsgVal, intValue, stringMessage);

And this is the code of the receiver (C++)

CString myStr = (CString) lParam;
AfxMessageBox(myStr);

Thanks a lot in advance!

  • 1
    http://stackoverflow.com/questions/184777/passing-data-between-c-mfc-app-and-c-sharp – user1849534 Dec 20 '12 at 09:12
  • How are you receiving the message in the receiver? Please post the receiver code to help interpret the issue – stamhaney Dec 20 '12 at 09:14
  • I don't know the OpenNETCF function - is that definitely marshalling the string across correctly and not just sending a pointer / object reference? You might need to use [WM_COPYDATA](http://stackoverflow.com/a/10619347/243245) – Rup Dec 20 '12 at 09:59

3 Answers3

1

Each of your application has it's own memory space on the computer. By sending the variable "stringMessage", you're actually sending the memory address of this string, which is unavailable to the other application.

I don't know if it applies to pda development, but this article could be useful.

If your ressources are limited, you could always send the characters of the string one by one. Since c# is Unicode, make that on the c++ sice, you use wchar_t to process the lParam!

EDIT: As Rup mentionned, you should use WM_COPYDATA if you need to send large strings or if you need to send messages very frequently.

Community
  • 1
  • 1
Goldorak84
  • 3,714
  • 3
  • 38
  • 62
  • 1
    +1 for the first bit, probably with [`WM_COPYDATA`](http://stackoverflow.com/a/10619347/243245). I'd think sending the characters one-by-one would have massive overhead, though! You'd be wrapping each character in a message structure and dumping it on a queue somewhere. – Rup Apr 02 '13 at 12:42
  • I agree that WM_COPYDATA is faster. On the other hand, I already implemented my solution without noticing any slowdowns. It must depend on the size of the string and the frequency of the messages and the level of performance needed. – Goldorak84 Apr 02 '13 at 13:53
0

C# strings are encoded as Unicode, whereas the default CString behaviour is LPCTSTR. Ensure your MFC application compiles using the MultiByte charset (using the _UNICODE directive or the representing compiler statement).

For more information refer to the MSDN.

Carsten
  • 11,287
  • 7
  • 39
  • 62
  • That doesn't mean you can just cast an LPCTSTR into a CString object though! – Rup Dec 20 '12 at 09:58
  • If you are using ANSI charset LPCTSTR is defined as LPCSTR which does not support multi byte characters that unicode uses. You can use conversion or macros to convert the lParam of the message to an LPCSTR, but it is easier to define LPCTSTR as LPCWSTR (const wchar_t*) so the application automatically uses unicode. This also enables you to easily sending messages back to the managed application. – Carsten Dec 20 '12 at 10:16
0

The better solution is create an C++/CLI dll and send the message to this dll and this dll will communicate with the MFC dll. The advantage is that, in C++/CLI dll, you can convert the System.String to CString and pass this CString to MFC dll.