typedef struct ESG_SCHEDULE_ITEM_s
{
wchar_t Date[30];
wchar_t Time[30];
wchar_t TitleStr[64];
wchar_t ExtentInfo[64];
}ESG_SCHEDULE_ITEM;struct PROGRAMESG
{
unsigned int freqPoint;
unsigned char LogicID;
unsigned int ServerID;
unsigned int nts;
unsigned int startTs;
unsigned char demod;
    unsigned int ScramblingMode;
int type;
wchar_t ServerName[20];
ESG_SCHEDULE_ITEM * esgschedule;
int schedulesum;
};struct PRINFO
{
PROGRAMESG pInfo[50];
UINT32 mProgramSum;
};
这是C++里的定义,现在从C++传递一个消息到C#处理,,该消息就是PRINFO结构体指针,,C#该如何将指针转成想要的消息数据呢?

解决方案 »

  1.   

            [StructLayout(LayoutKind.Sequential)]  
            public struct ESG_SCHEDULE_ITEM
            {
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
                public UInt16[] Date;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
                public UInt16[] Time;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
                public UInt16[] TitleStr;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
                public UInt16[] ExtentInfo;
            }
            [StructLayout(LayoutKind.Sequential)]  
            public struct PROGRAMESG
            {
                public uint freqPoint;
                public byte LogicID;
                public uint ServerID;
                public uint nts;
                public uint startTs;
                public byte demod;
                public uint ScramblingMode;
                public int type;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
                public UInt16[] ServerName;
                public IntPtr esgschedule;
                public int schedulesum;
            }
            [StructLayout(LayoutKind.Sequential)]  
            public struct PRINFO
            {
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
                public PROGRAMESG[] pInfo;
                public uint mProgramSum;
            }
    这是我目前定义的C#对应的结构体类型,,然后用PRINFO RecInfo = (PRINFO)Marshal.PtrToStructure(m.WParam, typeof(PRINFO));就会出现NotSurpportedException异常错误!各路过的客官有空支支招啊!
      

  2.   

    感觉你的wchar_t转为C#,好像该对应string吧
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)] 
    public string m_Date;
      

  3.   

    http://topic.csdn.net/u/20100126/13/8bb52c6b-165e-4d32-8691-bc97f7126adc.html
      

  4.   

    感觉你的wchar_t转为C#,好像该对应string吧
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]  
    public string m_Date;最主要是它不是字符串,当成数组来用的!
      

  5.   

    有一点必须提的是!我用应该用的环境是 compact framework 3.5的!我在怀疑是不是本身环境不支持多重结构体及数组嵌套!我现在C#用在通信方面很不好用!结构嵌套不参解决的话,,那会很多的问题的!
      

  6.   

    如果是 struct a{int aa;int bb} strutc {A[] CC;int d}的话,是很容易用PRINFO RecInfo = (PRINFO)Marshal.PtrToStructure(m.WParam, typeof(PRINFO));这样的方式转出来的!只要在a 结构再加进指针或者数组都有问题的!也就是结构体数组变量的成员类型是不能含用数组或者指针形式的成员!