cast of (managed) Message to COPYDATASTRUCT

70 Views Asked by At

I'm using an overriden WndProc to get an inter process communication done, according to this example: https://learn.microsoft.com/en-us/windows/win32/dataxchg/using-data-copy I have no problem with the sender, this works fine. However, as soon as I want to get the content of the LParam, I cannot get it compiled and I simply don't understand what is causing the problem.

protected:
    [SecurityPermission(SecurityAction::Demand, Flags = SecurityPermissionFlag::UnmanagedCode)]
    virtual void WndProc(Message %m) override

    {
        PCOPYDATASTRUCT pMyCDS;
        // Listen for operating system messages.
        switch (m.Msg)
        {
        case WM_COPYDATA:
            pMyCDS = (PCOPYDATASTRUCT)m.lParam; //doesn't work: Failure: E0413  no suitable conversion function from "System::IntPtr" to "PCOPYDATASTRUCT" exists
            break;
        }
    }

If I simply do some other stuff inside the WM_COPYDATA case I can confirm that the event fires, but of course I need to get to the content of lParam. How can I copy or cast the content of lParam to COPYDATASTRUCT?

I'm using Visual Studio 2019.

1

There are 1 best solutions below

2
Pepijn Kramer On

You might try this, not sure it works. I haven't done any C++/clr in a while. But my guess is that you try to access managed memory straight from c++ without marshalling it first.

switch (m.Msg)
{
case WM_COPYDATA:
    auto copydata = System::Runtime::InteropServices::Marshal::PtrToStructure<COPYDATASTRUCT>(m.LParam);
    // uses copydata here
    break;
}

At least I hope this will point you in a direction. :)