Help: Unable To Copy Data (In Structure) from Unmanged MFC/C++ App to .NET
-
I need to copy and send the data in a private structure from an MFC app to a .NET app. Somehow, the following does not seem to work. When I use the WM_COPYDATA windows message as indicated below, it is not being received by the WndProc on the .NET side, but when I use a private message, it is received. Unfortunately, when I use the private message, the data seems not to be copied across and I get an assertion error for null data. WM_COPY is not received on the .NET side, but when a private message is used, it is received but then Marshal.PtrToStructure asserts reporting a null value. Using COM interop is not an option. The client thinks that COM will slow things down as the function is called thousands of times. Does any one have a suggestion, or a better approach? Thanks Gaul **************************** ON THE MFC SIDE **************************** //////////////////////////////////////////////////////////// // Increment is called on the MFC side /////////////////////////////////////////Increment Counter long CTSPerfMon::Increment(int objectID, int instID) { _msg.nMsgID = TS_MSG_INCREMENT; _msg.nObjectID = objectID; _msg.nInstID = instID; _msg.nValue = -1; return SendMessage(); } // Send Message across to the .NET side module long CTSPerfMon::SendMessage() { // Fill the WIN32 COPYDATASTRUCT structure _copyData.dwData = _nPerfMsg; // Message identifier _copyData.cbData = sizeof(_msg); // size of data _copyData.lpData = &_msg; // data structure // Call function, passing data in &_msg long nRetCode = -1; if ( _hPerfWnd != NULL ) { HWND hWnd = GetForegroundWindow(); if (hWnd == NULL) hWnd = GetDesktopWindow(); nRetCode = ::SendMessage( _hPerfWnd, WM_COPYDATA, //private message works here (WPARAM)(HWND) hWnd, //windows sending the data (LPARAM)(LPVOID) &_msg); } return nRetCode; } **************************** ON THE C# .NET SIDE **************************** protected override void WndProc(ref System.Windows.Forms.Message m) { if (m.Msg == (int) Msgs.WM_COPYDATA) { COPYDATASTRUCT copyData = (COPYDATASTRUCT) Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT)); if ((object) copyData != null) { IntPtr nPerfMsg = copyData.dwData; if (nPerfMsg.ToInt32() == _nPerfMsg) { TSMSG msg = (TSMSG) Marshal.PtrToStructure(copyData.lpData, typeof(TSMSG)); int nRetCode = ProcessMessage(ref msg); m.Result = (IntPtr) nRetCode; r