Click to See Complete Forum and Search --> : Passing Structs between Apps


QBasicer
July 19th, 2006, 06:44 PM
I wrote a sender program that (correctly) finds the HWND of the remote program by registered class name. This is the code for the struct:
typedef struct{
char* msg;
char* title;
int flags;
}MSGBOX;
And the send code (excluding the search for the HWND):
HGLOBAL msgboxhandle = GlobalAlloc(0, sizeof(MSGBOX));
MSGBOX *foo;
foo = (MSGBOX*)GlobalLock(msgboxhandle);
foo->flags = MB_OK | MB_ICONINFORMATION;
foo->msg = "Testing";
foo->title = "Message Testing";
GlobalUnlock(msgboxhandle);
SendMessage(tWindow, WM_USER + 5, (WPARAM)NULL, (LPARAM)msgboxhandle);
GlobalFree(msgboxhandle);

I've tried a few things, including locally defining a MSGBOX on the stack and using &foo to send it (using both wParam and lParam, I'm not sure what the differences are).

And the receieving code:
case WM_USER + 5:

MSGBOX *in;
in = (MSGBOX*)GlobalLock((HGLOBAL)lParam);
MessageBox(hWnd, in->msg, "Incoming Message", MB_OK);
GlobalUnlock((HGLOBAL)lParam);
break;
It turns up blank every time on the receiving side.

Thanks.

wildfrog
July 19th, 2006, 08:07 PM
foo->msg = "Testing";
foo->title = "Message Testing";
You struct may be globally allocated but the strings are on the stack. So, on the receiving side, msg and title points to 'invalid' memory.

- petter

Viorel
July 20th, 2006, 02:36 AM
I think you should create a structure with no pointers, as results from the wildfrog's previous message:


struct MSGBOX
{
char msg[100];
char title[100];
int flags;
};

MSGBOX msgbox;

After filling such a structure, you can send it between applications using WM_COPYDATA message:


COPYDATASTRUCT cds = { 0 };
cds.cbData = sizeof(MSGBOX);
cds.lpData = &msgbox;
SendMessage(tWindow, WM_COPYDATA, (WPARAM)currentHwnd, (LPARAM)&cds);

The receiving application should process WM_COPYDATA message and get the passed data from received COPYDATASTRUCT structure via LPARAM. The lpData pointer will point to a local copy of sent MSGBOX structure.

I hope this helps.

golanshahar
July 20th, 2006, 06:21 AM
As Viorel for passing struct between applications you need to use IPC mechanism, WM_COPYDATA is one of them ;)

Cheers

QBasicer
July 23rd, 2006, 11:02 AM
Thanks! I was wondering how I would be able to send data between apps. It just seemed kinda weird to me.