将IntPtr转换为对象C#

时间:2014-12-05 08:18:02

标签: c# c++ wpf type-conversion intptr

从我的遗留应用程序(在单独的进程下运行)我将在像这样的对象中发送双值

#define MYMESSAGECODE (WM_APP + 123 )
typedef struct
{
    float f;
    double d;
} MyDataStruct;

MyDataStruct data;
data.f = 1.0;
data.d = 2.0;
pWpfWnd->SendMessage( MYMESSAGECODE, 0, (LPARAM) &data );

这是在一个单独的过程中在WndProc中收到的像这样

private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
      switch (msg)
      {
           case GA_SLOT_COORDINATES:
           // Need solution here to convert lParam to MyDataStruct
      }
      return IntPtr.Zero;
}

我想将lParam中传递的数据从遗留应用程序转换为在不同进程下运行的.Net应用程序中的同一对象。我怎样才能做到这一点?谢谢!

1 个答案:

答案 0 :(得分:1)

如果您是内部流程,Marshal.PtrToStructure会帮助您。

您似乎正在尝试在整个过程中执行此操作,但这是不可能的。你的LParam是指向另一个进程内存的指针,你不能像这样共享内存。您需要使用任何 Inter process communication 技术。看看Wm_CopyData

相关问题