COPYDATASTRUCT 의

cbData에 숫자를, lpData에 문자열만 넣어서 

간단한 자료만 송수신 하는 예제 코드




송신부 (C#)

        public struct COPYDATASTRUCT

        {

            public IntPtr dwData;

            public int cbData;

            [MarshalAs(UnmanagedType.LPStr)]

            public string lpData;

        }


        private void OnButtonSendClick(object sender, EventArgs e)

        {

            string msg = this.tbMsg.Text.Trim();


            if (string.IsNullOrEmpty(msg))

            {

                MessageBox.Show("메세지를 입력해주세요");

                return;

            }


            IntPtr hwndRcvr = FindWindow(null, "CppReceiveWM_COPYDATA"); //"LHJ WINDOW");

//                byte[] buff = System.Text.Encoding.Default.GetBytes(msg);


                COPYDATASTRUCT cds = new COPYDATASTRUCT();

                cds.dwData = IntPtr.Zero;

//                cds.cbData = buff.Length + 1; //+1을 빼면 수신측에서 cds.lpdata의 뒤에 이상한 글자 붙음.

                cds.cbData = msg.Length+1; //+1을 빼면 수신측에서 cds.lpdata의 뒤에 이상한 글자 붙음.

                cds.lpData = msg;


                SendMessage(hwndRcvr, WM_COPYDATA, 0, ref cds);

        }





수신부 (C++)

 : memcpy_s(.) 을 없애서 빠른 응답속도를 지원토록 함


typedef struct tagCOPYDATASTRUCT2 {
    ULONG_PTR dwData;
    DWORD cbData;
     PVOID lpData;
} COPYDATASTRUCT2, *PCOPYDATASTRUCT2;


INT_PTR CALLBACK DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

//LHJ version //15.3.28 ---------------

    switch (message)

    {

case WM_COPYDATA:        

HandleWMCOPYDATA(hWnd, wParam, lParam);

break;

    default:

return DefWindowProc(hWnd, message, wParam, lParam);

    }

    return 0;

}


bool HandleWMCOPYDATA(HWND hWnd, WPARAM wParam, LPARAM lParam)

{

PCOPYDATASTRUCT2 pcds;

pcds = (PCOPYDATASTRUCT2)lParam;

try 

{

    // If the size matches

int nsize = pcds->cbData;

{

        SetDlgItemInt(hWnd, IDC_NUMBER_STATIC, nsize, TRUE);

//   std::wstring s1 = (LPCWSTR)pcds->lpData; //송신부가  [MarshalAs(UnmanagedType.LPStr)] 이므로 LPCWSTR은 에러


std::string s2 = (LPSTR)pcds->lpData;

std::wstring s1;

s1.assign(s2.begin(), s2.end());  


// SetDlgItemTextW(hWnd, IDC_MESSAGE_STATIC, (LPCWSTR)pcds->lpData); // >image);

SetDlgItemText(hWnd, IDC_MESSAGE_STATIC, s1.c_str()); // >image);




// The receiving application should consider the data (pcds->lpData) 

        // read-only. The pcds parameter is valid only during the processing 

        // of the message. The receiving application should not free the 

        // memory referenced by pcds. If the receiving application must 

        // access the data after SendMessage returns, it must copy the data 

        // into a local buffer. 

//        memcpy_s(&myStruct, sizeof(myStruct), pcds->lpData, pcds->cbData);

//        memcpy_s(&myStruct, nsize, pcds->lpData, pcds->cbData);


        // Display the MY_STRUCT value in the window.

        // Display the MY_STRUCT value in the window.

//        SetDlgItemInt(hWnd, IDC_NUMBER_STATIC, myStruct.Number, TRUE);

//        SetDlgItemText(hWnd, IDC_MESSAGE_STATIC, myStruct.Message);


//wchar_t text1[50000];

//mbstowcs(text1, myStruct.Message, strlen(myStruct.Message) + 1);

//        SetDlgItemText(hWnd, IDC_MESSAGE_STATIC, text1);


//        SetDlgItemInt(hWnd, IDC_NUMBER_STATIC, pMyStruct->Number, TRUE);

// LPCTSTR s2;

// s2 = (LPCTSTR) pMyStruct->Message;

//        SetDlgItemText(hWnd, IDC_MESSAGE_STATIC, s2);

    }

}

catch( int)

{

//MessageBox(hWnd, L"1", L"2", MB_OK);

}

return true;

}



Posted by 세모아
,