2

how we will code these structures(Written in C++) in 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;
1
  • 4
    You have asked 10 questions, but did not accept a single answer. That is not nice. If a question was answerd then mark that answer as accepted (click on empty check mark which is to the left of the answer). Commented Nov 22, 2010 at 10:03

2 Answers 2

3

Like this:

[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;
 }

and this:

[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;
 }
Sign up to request clarification or add additional context in comments.

4 Comments

thanks aamir but i want to ask that weather char and wchar are declared in same way or i should use it UnmanagedType.Bstr and why charset is unicode. please guid me
Charset is something that dictates that what type of data are you going to put in the strings.
and what about char and wchar?
Only CharSet.Ansi is appropriate. The szNickName member is nasty, can't do anything reasonable but char[] and ByValArray. Convert it to a string by searching for the terminating 0.
1

use ansi in first structure as

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

you will split the second structure as this struct contain mixed string of ansi and unicode you will split this struct as

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SZNickName
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string NickName;
}

and your FT_SERVER_USB_DEVICE will use CharSet.Ansi as

[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;  
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.