详细描述: 主要是由C#调用DLL库造成的问题。 现有两个C#结构体,是从C语言中翻译过来的。 第一个: [StructLayoutAttribute(LayoutKind.Sequential)]
public struct STATUS
{
public ushort fwDevice;
public ushort fwMedia;
public ushort fwRetainBin;
public ushort fwSecurity;
public ushort usCards;
public ushort fwChipPower;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string lpszExtra;
} 第二个: [StructLayoutAttribute(LayoutKind.Sequential)]
public struct RESULT
{
public uint RequestID;
public ushort hService;
public int hResult;
public int u;
public IntPtr lpBuffer; //指向第一个结构体的指针;
} 通过消息处理,我从WinForm的Message.LParam中获得了一个第二个结构体(RESULT)的指针并将其转化为了RESULT的一个变量,通过这个变量可以正常访问其字段,唯有: “public IntPtr lpBuffer; //指向第一个结构体(STATUS)的指针”使用失败,该如何定义和操作结构体才能正确使用该指针呢? 补充说明:我用WinForm窗体来接收C++编写的DLL发送过来的消息,DLL导出函数中将WinForm窗体句柄传给导出函数,其会将消息传送给WinForm。经测试证明:DLL传过来的消息Message.LParam指针可以转化为对应的结构体,就是该结构体内的指向另结构体的指针出错,不能转化为对于的结构体,但值有的。为什么呢?该如何解决这类问题?
产生错误的代码: Status = (STATUS)Marshal.PtrToStructure(Result.lpBuffer, typeof(STATUS)); 其中Status为STATUS类型的结构体,Result为RESULT类型的结构体变量。 错误提示: 检测到FatalExecutionEngineError 运行时遇到了错误。此错误的地址为 0x661ffc37,在线程 0xe0c 上。错误代码为 0xc0000005。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。 请求各位高手指教啊!我郁闷一个上午了,都没有找到解决方法。
public struct STATUS
{
public ushort fwDevice;
public ushort fwMedia;
public ushort fwRetainBin;
public ushort fwSecurity;
public ushort usCards;
public ushort fwChipPower;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string lpszExtra;
} 第二个: [StructLayoutAttribute(LayoutKind.Sequential)]
public struct RESULT
{
public uint RequestID;
public ushort hService;
public int hResult;
public int u;
public IntPtr lpBuffer; //指向第一个结构体的指针;
} 通过消息处理,我从WinForm的Message.LParam中获得了一个第二个结构体(RESULT)的指针并将其转化为了RESULT的一个变量,通过这个变量可以正常访问其字段,唯有: “public IntPtr lpBuffer; //指向第一个结构体(STATUS)的指针”使用失败,该如何定义和操作结构体才能正确使用该指针呢? 补充说明:我用WinForm窗体来接收C++编写的DLL发送过来的消息,DLL导出函数中将WinForm窗体句柄传给导出函数,其会将消息传送给WinForm。经测试证明:DLL传过来的消息Message.LParam指针可以转化为对应的结构体,就是该结构体内的指向另结构体的指针出错,不能转化为对于的结构体,但值有的。为什么呢?该如何解决这类问题?
产生错误的代码: Status = (STATUS)Marshal.PtrToStructure(Result.lpBuffer, typeof(STATUS)); 其中Status为STATUS类型的结构体,Result为RESULT类型的结构体变量。 错误提示: 检测到FatalExecutionEngineError 运行时遇到了错误。此错误的地址为 0x661ffc37,在线程 0xe0c 上。错误代码为 0xc0000005。此错误可能是 CLR 中的 bug,或者是用户代码的不安全部分或不可验证部分中的 bug。此 bug 的常见来源包括用户对 COM-interop 或 PInvoke 的封送处理错误,这些错误可能会损坏堆栈。 请求各位高手指教啊!我郁闷一个上午了,都没有找到解决方法。
lpszExtra;实际上是个指针,如果是我,我会这样声明(当然其他人有其他方法)
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct STATUS
{
public ushort fwDevice;
public ushort fwMedia;
public ushort fwRetainBin;
public ushort fwSecurity;
public ushort usCards;
public ushort fwChipPower;
public uint lpszExtra;
}
使用时候byte* lpName = (byte*)((uint)lpszExtra);
string nameStr = new string((sbyte*)lpName);[StructLayoutAttribute(LayoutKind.Sequential)]
public struct RESULT
{
public uint RequestID;
public ushort hService;
public int hResult;
public int u;
public uint lpBuffer; //指向第一个结构体的指针;
}使用时将lpbuffer强制转换为(STATUS *)
(*(STATUS *)lpBuffer).fwDevice
(*(STATUS *)lpBuffer).fwMedia等