开发者

Problem in Communication between two dialog boxes through WM_COPYDATA?

Friends its really giving me a great head ache about the problem I am facing for the couple of days...Its simple...I want to communicate between two/more dialog boxes for example if there is a variable CString test..I want this test variable to be common for the dialogs/classes(considering each dialog having separate classes)...I tried lot methods, everything failed..atlast I tried this WM_COPYDATA method...even now, am not achieve what i wanted to do...

Sender Class:

#define ORGININFO 1

typedef struct ShareMessage
{
    CString mydata;
    int myValue;
}MYDATA;

void CCopyDataDlg::OnBnClickedOk()
{
    // TODO: Add your control notification handler code here
    MYDATA myData;
    COPYDATASTRUCT cData;

    myData.mydata.SetString(L"Rakesh");

    cData.dwData = ORGININFO;
    cData.cbData = sizeof(myData);
    cData.lpData = &myData;

开发者_开发技巧    HWND hwnd  = (HWND)FindWindow(L"Dialog1",L"Test");



    SendMessageA(m_hWnd,WM_COPYDATA,(WPARAM)hwnd,(LPARAM)(LPVOID)&myData);


    Dialog1 dlg;
    dlg.DoModal();

}

Receiver class:

#define iMessage 1

typedef struct MyDatas
{
    CString myData;
    int myint;
}DATA;
PCOPYDATASTRUCT pData;


LRESULT Dialog1::WindowProc(UINT message,WPARAM wParam,LPARAM lparam)
{

    if(WM_COPYDATA != NULL)
        pData = (PCOPYDATASTRUCT)lparam;
        switch(pData->dwData)
        {
        case iMessage:
            MessageBoxA((HWND)AfxGetInstanceHandle(),(LPCSTR)(LPCTSTR)((DATA*)(pData->lpData))->myData,(LPCSTR)L"Test",MB_OK);

        }
    return 0;
}

in the above I dont know what is the mistake I am doing but its not receiving the data from the CCopyDialog class...Please help me with this...


Your CString may be in the struct, but the memory to store it is allocated on the heap. You have to go low-tech here: put a wchar (or char, or TCHAR depending upon your desires) array in the ShareMessage struct and copy the string contents to that array. In your receiver code, read the string out of the wchar array. Oh, and you have sent the address of YOUR struct, not the COPYDATASTRUCT, and sent it to yourself, not the other dialog. modify the SendMessage call like this:

SendMessage (hWnd,WM_COPYDATA,(WPARAM)hwnd,(LPARAM)(LPVOID)&cData);

Also, are you SURE the FindWindow call is working? That class name looks very suspicious to me. Better to use NULL, and rely on the window title. I have this vague memory that MFC dialogs have a fixed class name.

Your code should work then.

For example:

typedef struct ShareMessage
{
    wchar szMyString [100];
    int myValue;
}MYDATA;

void CCopyDataDlg::OnBnClickedOk()
{
   MYDATA myData;
   COPYDATASTRUCT cData;
   ZeroMemory (&myData, sizeof(myData);

   wcscpy (myData.szMyString, (L"Rakesh"));

   cData.dwData = ORGININFO;
   cData.cbData = sizeof(myData);
   cData.lpData = &myData;
   ...

I haven't tested that code, it's off the top of my head. I assumed wchar because you used the L modifier on your constant string.

Additionally, in your receiver code you have this line:

if (WM_COPYDATA != NULL)

which doesn't make sense. I assume you meant to test the received message number against the constant WM_COPYDATA.


I avoided to use WinProc..Instead of that I wrote a normal function(CopyData) in the class Dialog11..Created a modeless dialog in the class CCopyDialog1.. and called that function(CopyData)..It worked..Please check the below code...

//CCopyDialog Class(sender)
void CCopyDataDlg::OnBnClickedOk()
{
    // TODO: Add your control notification handler code here
    Dialog1* dialog1 = new Dialog1();
    dialog1->Create(IDD_DIALOG1,0);
    dialog1->ShowWindow(SW_SHOW);
    ZeroMemory(&myData,sizeof(myData));
    wcscpy(myData.mydata,(L"Rakesh"));

    cData.dwData = ORGININFO;
    cData.cbData = sizeof(myData);
    cData.lpData = &myData;

       HWND rs = ::FindWindow(NULL,L"Rakesh");

    dialog1->CopyData(WM_COPYDATA,(WPARAM)rs,(LPARAM)&cData);


}


Dialog1 class(receiver)
LRESULT Dialog1::CopyData(UINT message,WPARAM wParam,LPARAM lparam)
{
    if(message == WM_COPYDATA)
    {
        pData = (PCOPYDATASTRUCT)lparam;
        wchar_t tes[50];
        memcpy(tes,((DATA*)(pData->lpData))->myData,sizeof(DATA));

    }
    else
    {
        return FALSE;
    }

    return 0;
}

Basically when compared to my previous code(code in my question)...theres lot of difference/mistakes..

1.In the sendmessage i passed the structure instead of COPYDATASTRUCT.. 2.Called FindWindow before calling the Dialog1 window.. 3.Used the function WinProc function to receive the message..which was very hard to make it work..then avoided that and used a normal function 4.Didnt pass a proper window handle... the above all are corrected by Bob Moore...credit goes to him....


Further to my answer above, this is the receive code

LRESULT Dialog1::WindowProc(UINT message,WPARAM wParam,LPARAM lparam)
{
   CString csPassedString;
   PCOPYDATASTRUCT pData;
   MYDATA myStuff;

   if (message == WM_COPYDATA)
   {
      pData = (PCOPYDATASTRUCT)lparam;
      if (pData)
      {
         memcpy (&myStuff, pData->lpData, sizeof(myData));
         csPassedString = myStuff.szMyString;
         switch(pData->dwData)
         {
           case iMessage:
              MessageBox (csPassedString,
                          L"Test",
                          MB_OK);
....

But there's another more basic question here... in an MFC application, overriding the WindowProc just to handle a basic message seems very odd.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜