开发者

Marshalling structures containing reference types/arrays

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 ag开发者_JAVA技巧ain with problems. It seems that pointer types can not get marshaled.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜