I'm trying to achieve inter-process communication via WM_COPYDATA messages. lpData member of COPYDATASTRUCT can not contain pointers. My problem is, what is the difference between char arrays and other arrays or vectors.
When I use char array like here, it sends messages succesfully.
typedef struct tagMYSTRUCT {
wchar_t x[40] = {0};
} MYSTRUCT, *PMYSTRUCT;
But when I use a vector, receiving application can not get it.
typedef struct tagOTHERSTRUCT {
wchar_t one[40] = { 0 };
wchar_t two[20] = { 0 };
wchar_t three[20] = { 0 };
wchar_t four[4] = { 0 };
wchar_t five[3] = { 0 };
} OTHERSTRUCT, *POTHERSTRUCT;
typedef struct tagMYSTRUCT2 {
std::vector<OTHERSTRUCT> y;
} MYSTRUCT2, *PMYSTRUCT2;
I have an externed global vector 'gOtherStructList'. In the various parts of the program, this global variable is filled. For example,
DWORD WINAPI foo(LPCTSTR lpchText) {
...
if (sizeof(lpchText[0]) == 2) {
wcscpy(gOtherStructList[0].one, lpchText);
}
...
}
Then, when I'm sending this global list, I'm copying it into MYSTRUCT2 variable (for unrelated reasons) with wcscpy() for every wchar_t of every element.
This is how I send to receiver app:
MYSTRUCT2 my_struct; //my_struct.y is a vector
// Filled my_struct.y here with wcscpy()
COPYDATASTRUCT cds;
cds.dwData = MY_CASE;
cds.cbData = sizeof(OTHERSTRUCT) * my_struct.y.size();
cds.lpData = &my_struct;
SendMessage(gDataReceiver, WM_COPYDATA, NULL, (LPARAM)&cds);
If it makes difference, receiving application uses these messages like:
case WM_COPYDATA:
{
PCOPYDATASTRUCT pcopydata = (PCOPYDATASTRUCT)lParam;
switch (pcopydata->dwData) {
case MY_CASE:
// When I code this, it works
PMYSTRUCT p = (PMYSTRUCT)(pcopydata->lpData);
wcscpy(mylocalvar, p->x); // for char array
...
// But, this doesn't work.
std::cout << "New message received" << std::endl; // this gets printed, then program crashes.
PMYSTRUCT2 p = (PMYSTRUCT2)(pcopydata->lpData);
OTHERSTRUCT mylocallist[100],
wcscpy(mylocallist[0].one, p->y[0].one);
}
I understand why we can't use pointers for WM_COPYDATA. What I don't understand is, what is the difference of these examples. Why we can use char arrays but not vectors?
Edit: Edited my question, based on informative comments.
Thanks