问题描述
我正在为 WIN-CE 平台开发 C#.Net CF.在我的代码中,我使用
I am working on C#.Net CF for WIN-CE platform. In my code I am using
int size = Marshal.SizeOf(typeof(struct_Obj));
IntPtr newptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(struct_Obj, newptr, false);
我正在尝试发送此结构信息:
I am trying to send this struct info:
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
public struct __NIC_STAT
{
ulong Size; // Of this structure.
public Char[] ptcDeviceName; // The device name to be queried..
ulong DeviceState; // DEVICE_STATE_XXX above
ulong DeviceState; // DEVICE_STATE_XXX above
ulong MediaType; // NdisMediumXXX
ulong MediaState; // MEDIA_STATE_XXX above
ulong PhysicalMediaType;
ulong LinkSpeed; // In 100bits/s. 10Mb/s = 100000
UInt64 PacketsSent;
UInt64 PacketsReceived;
ulong InitTime; // In milliseconds
ulong ConnectTime; // In seconds
UInt64 BytesSent; // 0 - Unknown (or not supported)
UInt64 BytesReceived; // 0 - Unknown (or not supported)
UInt64 DirectedBytesReceived;
UInt64 DirectedPacketsReceived;
ulong PacketsReceiveErrors;
ulong PacketsSendErrors;
ulong ResetCount;
ulong MediaSenseConnectCount;
ulong MediaSenseDisconnectCount;
} ;
当我在 WIN-CE 机器上运行代码时,我得到不支持的异常".这两种方法都在抛出异常.
when I run the code in WIN-CE machine, I am getting "not supported exception".Those two methods are throwing exceptions.
谁能告诉我如何找到结构大小以及如何将结构转换为 Ptr 而不会出现任何 WIN-CE 问题.
Can anybody tell me how to find the structure size and how to convert structure to Ptr with out any issues for WIN-CE.
谢谢!!
推荐答案
您的结构声明不正确.C++ ULONG
是 32 位无符号类型.但是在 C# 中,ulong
是 64 位.这显然是个大问题.
Your struct is declared incorrectly. The C++ ULONG
is a 32 bit unsigned type. But in C#, ulong
is 64 bits. That's clearly a huge problem.
最重要的是,我必须承认,对于以您的方式使用 char[]
有点怀疑.我会把它作为一个带有 UnmanagedType.LPWStr
的字符串来做.
On top of that, I must admit to being slightly sceptical about using char[]
in the way you do. I would do it as a string with UnmanagedType.LPWStr
.
所以我会让你的结构是这样的:
So I would have your struct like this:
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
public struct __NIC_STAT
{
uint Size;
[MarshalAs(UnmanagedType.LPWStr)]
public string ptcDeviceName;
uint DeviceState;
uint DeviceState;
uint MediaType;
uint MediaState;
uint PhysicalMediaType;
uint LinkSpeed;
ulong PacketsSent;
ulong PacketsReceived;
uint InitTime;
uint ConnectTime;
ulong BytesSent;
ulong BytesReceived;
ulong DirectedBytesReceived;
ulong DirectedPacketsReceived;
uint PacketsReceiveErrors;
uint PacketsSendErrors;
uint ResetCount;
uint MediaSenseConnectCount;
uint MediaSenseDisconnectCount;
};
我不知道为什么 Marshal.SizeOf
对你来说失败了.您可能需要将 ptcDeviceName
声明为 IntPtr
并使用 Marshal.StringToHGlobalUni
来设置该值.这至少使结构 blittable,如果 Marshal.SizeOf
仍然失败,那么你可以依靠 sizeof
.
I'm not sure why Marshal.SizeOf
is failing for you. You may need to declare ptcDeviceName
as IntPtr
and use Marshal.StringToHGlobalUni
to set the value. That at least makes the struct blittable and if Marshal.SizeOf
still fails then you can fall back on sizeof
.
这篇关于Marshal.SizeOf() &Marshal.StructureToPtr() 在 .NET CF WIN-CE 中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!