简体   繁体   中英

how to create custom wm_copydata in mfc c++

How can I create a custom windows message that passes any data to a different program.

I am messaging between a GUI-program and a server-program. My background is in *nix programming and I am a bit lost in the windows world.

Currently both programs are created by Visual Studio's wizard by chosing "MFC windowed application". Now I want an elegant way to communicate between the programs. Google gives me Cwnd->sendmessage + registering your own messages etc. However, I cannot pass a char pointer to a different program with standard custom messages (well, I can but the memory area is wrong, and the program segfaults). So, google again gives me sendmessage(WM_COPYDATA, hparam, lparam) which is marshalled. When googling marshalling... I ran into a wall.

I assume that marshalled messages (or their data) are passed into a shared memory area which is readable only by the sender and the receiver program (correct?). And by creating a custom message that has a pointer of marshalled data as lparam, I can pass any object to another program (correct?). How do I do this in practice? I tried the following:

pWin->SendMessage(pTargetWin, WM_CUSTOM_MESSAGE, pSourceWin, pData);

Above works if pData is integer. If pData is a pointer to object, I cannot use the object because of the missing marhsalling. I know that I can do a wrapper COPYDATASTRUCT wrapper to pData and change to WM_COPYDATA. Should I do that instead?

br, Juha

WM_COPYDATA does marshalling for you, provided you've correctly initialized the COPYDATASTRUCT that you're passing as the LPARAM (lpData is the pointer, not dwData). Custom messages won't do that, and you don't want to reimplement marshalling by hand. If you need marshalling, use WM_COPYDATA. Is that what you're asking? It sounds like you know the answer already.

If you need to support multiple different kinds of messages, you could just stuff an enum in COPYDATASTRUCT.dwData to specify what the rest of the data means. If four bytes isn't enough, you could define a header on your marshalled data.

One way or another, as long as you can pass a big pile of zeroes and ones, you can communicate anything you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM