2010-11-22 80 views
1

我們將如何在C#編組的C++結構到C#

typedef struct 
{ 
    USHORT idVendor; 
    USHORT idProduct; 
    USHORT bcdDevice; 
    CHAR szSerialNumber[256]; 
} FT_USB_UNIQID, *PFT_USB_UNIQID; 

typedef struct { 
    FT_USB_UNIQID usbHWID; 
    eFtUsbDeviceStatus status; 
    BOOL bExcludeDevice; 
    BOOL bSharedManually; 
    ULONG ulDeviceId; 
    ULONG ulClientAddr; 
    CHAR szUsbDeviceDescr[256]; 
    CHAR szLocationInfo[256]; 
    WCHAR szNickName[256]; 
} FT_SERVER_USB_DEVICE, *PFT_SERVER_USB_DEVICE; 
+4

你問10個問題,但沒有接受一個單一的答案。那不好。如果回答問題,則將該答案標記爲已接受(單擊答案左側的空白複選標記)。 – Dialecticus 2010-11-22 10:03:45

回答

1

使用ANSI在第一個結構中爲

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 
public struct FT_USB_UNIQID 
{ 
    ...... 
} 

你會分割第二結構,該結構包含的ANSI和Unicode字符串混合您將在本結構拆分爲

​​

和你FT_SERVER_USB_DEVICE將使用CharSet.Ansi作爲

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]     
public struct FT_SERVER_USB_DEVICE    
{   
    public FT_USB_UNIQID usbHWID;  
    public eFtUsbDeviceStatus status;  
    public bool bExcludeDevice;  
    public bool bSharedManually;   
    public uint ulDeviceId;  
    public uint ulClientAddr;   
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]   
    public string szUsbDeviceDescr;   
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]   
    public string szLocationInfo;   
    public SZNickName szNickName; 
} 
+0

感謝jalil bhai現在工作正常 – Ehtsham 2010-11-23 11:23:29

3

這樣的代碼,這些結構(用C++):

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
public struct FT_USB_UNIQID 
{ 
    public ushort idVendor; 
    public ushort idProduct; 
    public ushort bcdDevice; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
    public string szSerialNumber; 
} 

這:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
public struct FT_SERVER_USB_DEVICE 
{ 
    public FT_USB_UNIQID usbHWID; 
    public eFtUsbDeviceStatus status; 
    public bool bExcludeDevice; 
    public bool bSharedManually; 
    public uint ulDeviceId; 
    public uint ulClientAddr; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
    public string szUsbDeviceDescr; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
    public string szLocationInfo; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
    public string szNickName; 
} 
+0

感謝阿米爾,但我想問天氣char和wchar是以相同的方式聲明,或者我應該使用它UnmanagedType.Bstr和爲什麼charset是unicode。請指導我 – Ehtsham 2010-11-22 10:28:05

+0

字符集是指明您要將什麼類型的數據放入字符串中。 – Aamir 2010-11-22 10:36:13

+0

和char和wchar怎麼樣? – Ehtsham 2010-11-22 10:41:06