Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Help: Unable To Copy Data (In Structure) from Unmanged MFC/C++ App to .NET

Help: Unable To Copy Data (In Structure) from Unmanged MFC/C++ App to .NET

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpc++comhelpquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gaul
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups