包含引用类型/数组的编组结构
我遇到问题了。这是结构的 C 定义:
typedef struct _UNIMDM_CHG_DEVCFG {
DWORD dwCommand;
LPCWSTR lpszDeviceClass;
LPVARSTRING lpDeviceConfig;
DWORD dwOption;
DWORD dwValue;
} UNIMDM_CHG_DEVCFG;
我在 Windows CE 上使用 .NET Compact Framework 3.5。我尝试了多种方法来封送此结构,例如:
[StructLayout(LayoutKind.Sequential)]
internal struct UnimodemChangeDeviceConfiguration
{
public int dwCommand;
public byte[] lpszDeviceClass;
public byte[] lpDeviceConfig;
public int dwOption;
public int dwValue;
}
问题是当在此结构的实例上调用 Marshal.SizeOf 时,它会抛出 NotSupportedException。为什么?我尝试对 lpszDeviceClass 使用字符串,但又出现问题。看来指针类型无法被封送。
I am having problem. This is C definition of the structure:
typedef struct _UNIMDM_CHG_DEVCFG {
DWORD dwCommand;
LPCWSTR lpszDeviceClass;
LPVARSTRING lpDeviceConfig;
DWORD dwOption;
DWORD dwValue;
} UNIMDM_CHG_DEVCFG;
I use .NET Compact Framework 3.5 on Windows CE. I tried many ways to marshal this structure, for instance:
[StructLayout(LayoutKind.Sequential)]
internal struct UnimodemChangeDeviceConfiguration
{
public int dwCommand;
public byte[] lpszDeviceClass;
public byte[] lpDeviceConfig;
public int dwOption;
public int dwValue;
}
The problem is when calling Marshal.SizeOf on an instance of this structure, it throws NotSupportedException. Why? I tried using string for lpszDeviceClass, but again with problems. It seems that pointer types can not get marshaled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CF Marshaler 无法处理这个问题。您的结构包含一个指向数据的指针,当您尝试向下发送 byte[] 时,数据本身无法固定。
我认为第一个(LPCWSTR)可以作为字符串或 StringBuilder 发送(取决于数据是输入还是输出)。
LPVARSTRING 更为复杂。您是否查看过 OpenNETCF TAPI 库?在 structs.cs 中有一个 VarString 的实现,您可以在该结构中实习。
The CF Marshaler cannot handle this. Your struct contains a pointer to data and when you try to send a byte[] down, the data itself cannot be pinned.
I think the first (LPCWSTR) could be sent in as a string or a StringBuilder (depending on if the data is going in or coming out).
The LPVARSTRING is more complex. Have you looked at the OpenNETCF TAPI library? In structs.cs there is an implementation of the VarString that you could probably just intern in that struct.