Marshal类的PtrToStructure

爷,独闯天下 提交于 2020-01-13 17:25:05

PtrToStructure,将数据从非托管内存块封送到新分配的指定类型的托管对象

应用:MFC发送消息到WPF界面上

  public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {

            if ((MsgType)msg == MsgType.WM_TestChip_R)
            {
                ChipTest MsgData;
                MsgData = (ChipTest)Marshal.PtrToStructure(wParam, typeof(ChipTest)); // 接收封装的消息
                currentInfoControlVM.UpdataR(MsgData);
               
                handled = true;
                return IntPtr.Zero;
                
            }

 

 [StructLayout(LayoutKind.Sequential, Pack = 4)]//设置结构体对齐
    public struct tagTestChip
    {
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 30, ArraySubType = UnmanagedType.R4)]
        public double[] dRst;
    }
    public struct ChipTest
    {
        public tagTestChip X_Index;//X索引
        public tagTestChip Y_Index;//Y索引
        public tagTestChip X_shift;//X偏移
        public tagTestChip Y_shift;//Y偏移
        public tagTestChip Angle;  //旋转角
        public tagTestChip Pd;     //判定结果
    }

底层MFC

参考:

https://blog.csdn.net/cai_huan_123/article/details/48436135

https://blog.csdn.net/livelylittlefish/article/details/2423764

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!