본문 바로가기

C / C++ / MFC

SendMessage C++ to C# using WM_COPYDATA

1. C++ (Sender)

 typedef struct tagCOPYDATASTRUCT {
     ULONG_PTR dwData;
     DWORD     cbData;
     CHAR*     lpData; 
} COPYDATASTRUCT;

 

char strChar[] = "20150601130012.xml";    //전송 할 STRING
 
 HWND hWnd = ::FindWindow(NULL, _T("XXXX"));    //전송 할 Application Handle

 COPYDATASTRUCT cpd;
 cpd.dwData = 0;
 cpd.cbData = strlen(strChar);
 cpd.lpData =  strChar;

 ::SendMessage(hWnd, WM_COPYDATA, NULL, (LPARAM)&cpd);

 

2. C# (Receiver)

        public struct COPY_DATA
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

 

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

 

IntPtr WndProc(IntPtr hWnd, int nMsg, IntPtr wParam, IntPtr lParam, ref bool handled)
{

if (nMsg == ConstString.WM_COPYDATA)
{

string strValue = string.Empty;
COPY_DATA copyData = (COPY_DATA)Marshal.PtrToStructure(lParam, typeof(COPY_DATA));
if (null != copyData.lpData)
{
        strValue = copyData.lpData;
}
else
        return IntPtr.Zero;

}

 

return IntPtr.Zero;

 

}

'C / C++ / MFC' 카테고리의 다른 글

[MFC]CListCtrl > Selection change  (0) 2019.01.24
C++ 64비트 컨버팅 이슈  (0) 2013.03.04
[MFC]디렉토리 유무 확인  (0) 2012.08.20
CString to char * , char * to CString  (0) 2012.04.06