C # vs. C + + data type comparison and struct-body conversions

Source: Internet
Author: User

Forward to Http://www.blogjava.net/heting/archive/2010/03/20/315998.html

C + + C #
=====================================
WORD ushort
DWORD UINT
UCHAR Int/byte can be replaced in most cases using int, and if strict alignment is required, it should be used Bytebyte
uchar* string/intptr
unsigned char* [MarshalAs (UnmanagedType.LPArray)]byte[]/? (INTPTR)
char* string
LPCTSTR string
LPTSTR [MarshalAs (UNMANAGEDTYPE.LPTSTR)] string
Long int
ULONG UINT
Handle INTPTR
HWND INTPTR
void* INTPTR
int int
int* ref int
*int INTPTR
unsigned int UINT
COLORREF UINT





API vs. C # data type correspondence table
API datatype type description C # type API data type type description C # type
WORD 16-bit unsigned integer ushort char character Char
Long 32-bit unsigned integer int dwordlong 64-bit length integer long
DWORD 32-bit unsigned integer UINT HDC device descriptor table handle int
HANDLE handle, 32-bit integer int hgdiobj GDI object handle int
UINT 32-bit unsigned integer UINT hinstance instance handle int
BOOL 32-bit Boolean integer bool HWM window handle int
LPSTR 32-bit pointer to character string Hparam 32-bit message parameter int
LPCSTR 32-bit pointer to a constant character String LPARAM 32-bit message parameter int
BYTE byte WPARAM 32-bit message parameter int








Bool=system.int32
Boolean=system.int32
Byte=system.uint16
Char=system.int16
Colorref=system.uint32
Dword=system.uint32
Dword32=system.uint32
Dword64=system.uint64
Float=system.float
Haccel=system.intptr
Handle=system.intptr
Hbitmap=system.intptr
Hbrush=system.intptr
Hconv=system.intptr
Hconvlist=system.intptr
Hcursor=system.intptr
Hdc=system.intptr
Hddedata=system.intptr
Hdesk=system.intptr
Hdrop=system.intptr
Hdwp=system.intptr
Henhmetafile=system.intptr
Hfile=system.intptr
Hfont=system.intptr
Hgdiobj=system.intptr
Hglobal=system.intptr
Hhook=system.intptr
Hicon=system.intptr
Himagelist=system.intptr
Himc=system.intptr
Hinstance=system.intptr
Hkey=system.intptr
Hlocal=system.intptr
Hmenu=system.intptr
Hmetafile=system.intptr
Hmodule=system.intptr
Hmonitor=system.intptr
Hpalette=system.intptr
Hpen=system.intptr
Hrgn=system.intptr
Hrsrc=system.intptr
Hsz=system.intptr
Hwinsta=system.intptr
Hwnd=system.intptr
Int=system.int32
Int32=system.int32
Int64=system.int64
Long=system.int32
Long32=system.int32
Long64=system.int64
Longlong=system.int64
Lparam=system.intptr
Lpbool=system.int16[]
Lpbyte=system.uint16[]
Lpcolorref=system.uint32[]
Lpcstr=system.string
Lpctstr=system.string
Lpcvoid=system.uint32
Lpcwstr=system.string
Lpdword=system.uint32[]
Lphandle=system.uint32
Lpint=system.int32[]
Lplong=system.int32[]
Lpstr=system.string
Lptstr=system.string
Lpvoid=system.uint32
Lpword=system.int32[]
Lpwstr=system.string
Lresult=system.intptr
Pbool=system.int16[]
Pboolean=system.int16[]
Pbyte=system.uint16[]
Pchar=system.char[]
Pcstr=system.string
Pctstr=system.string
Pcwch=system.uint32
Pcwstr=system.uint32
Pdword=system.int32[]
Pfloat=system.float[]
Phandle=system.uint32
Phkey=system.uint32
Pint=system.int32[]
Plcid=system.uint32
Plong=system.int32[]
Pluid=system.uint32
Pshort=system.int16[]
Pstr=system.string
Ptbyte=system.char[]
Ptchar=system.char[]
Ptstr=system.string
Puchar=system.char[]
Puint=system.uint32[]
Pulong=system.uint32[]
Pushort=system.uint16[]
Pvoid=system.uint32
Pwchar=system.char[]
Pword=system.int16[]
Pwstr=system.string
Regsam=system.uint32
Sc_handle=system.intptr
Sc_lock=system.intptr
Short=system.int16
Size_t=system.uint32
Ssize_=system.uint32
Tbyte=system.char
Tchar=system.char
Uchar=system.byte
Uint=system.uint32
Uint32=system.uint32
Uint64=system.uint64
Ulong=system.uint32
Ulong32=system.uint32
Ulong64=system.uint64
Ulonglong=system.uint64
Ushort=system.uint16
Word=system.uint16
Wparam=system.intptr

<---------supplement----------->

Non-managed type unmanaged C language type managed class name in Wtypes.h description
HANDLE void* System.IntPtr 32-bit
BYTE unsigned char System.Byte 8-bit
Short short system.int16 16-bit
WORD unsigned short system.uint16 16-bit
int int System.Int32 32 bit
UINT unsigned int System.UInt32 32-bit
Long Long System.Int32 32-bit
BOOL Long System.Int32 32 bit
DWORD unsigned long system.uint32 32 bit
ULONG unsigned long system.uint32 32 bit
Char Char System.Char is decorated with ANSI.
LPSTR char* System.String or system.stringbuilder are decorated with ANSI.
LPCSTR Const char* System.String or system.stringbuilder is decorated with ANSI.
LPWSTR wchar_t* System.String or system.stringbuilder are decorated with Unicode.
LPCWSTR Const wchar_t* System.String or system.stringbuilder is decorated with Unicode.
float float System.Single 32-bit
Double Double System.Double 64-bit





Transformation of struct-type data in C/C + +


When you do a project transplant, you often encounter data type conversions, and this time I came across the structure of C/ Toss a night to finally have a perfect plan.
For example, we have the following structure data in C + +:
typedef struct
{
Char slibname[256];
Char spathtolibrary[256];
INT32 ientries;
INT32 iused;
UINT16 Isort;
UINT16 iversion;
BOOLEAN fcontainssubdirectories;
INT32 ireserved;
} Libheader;
We want to turn it into a struct type below C #:
public struct Libheader
{
Public char[] Slibname;
Public char[] Spathtolibrary;
Public Int32 ientries;
Public Int32 iused;
Public UInt16 Isort;
Public UInt16 iversion;
Public Boolean fcontainssubdirectories;
Public Int32 ireserved;
}
Looks like no problem, hehe, actually this is not possible, we have to give the C # compiler some information, tell it some character array size. Then they look like this in C #:
[StructLayout (LayoutKind.Sequential)]
public struct Libheader
{
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 256)]
Public char[] Slibname;
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 256)]
Public char[] Spathtolibrary;
Public Int32 ientries;
Public Int32 iused;
Public UInt16 Isort;
Public UInt16 iversion;
Public Boolean fcontainssubdirectories;
Public Int32 ireserved;
}
Then write a function that is responsible for the conversion.
Public Structtype converbytestostructure<structtype> (byte[] bytesbuffer)
{
Check the length.
if (bytesbuffer.length! = marshal.sizeof (typeof (Structtype)))
{
The throw new ArgumentException ("Bytesbuffer parameter and Structobject parameter byte lengths are inconsistent. ");
}

IntPtr Bufferhandler = Marshal.allochglobal (bytesbuffer.length);
for (int index = 0; index < bytesbuffer.length; index++)
{
Marshal.writebyte (Bufferhandler, Index, Bytesbuffer[index]);
}
Structtype Structobject = (structtype) marshal.ptrtostructure (Bufferhandler, typeof (Structtype));
Marshal.freehglobal (Bufferhandler);
return structobject;
}
And then our function use case is this:
FileStream file = File.openread (@ "D:\Jagged Alliance 2 gold\install. LOG ");
byte[] buffer = new byte[marshal.sizeof (typeof (Libheader))];
File. Read (buffer, 0, buffer. Length);
Libheader TestValue = commontools.converbytestostructure<libheader> (buffer);
String libname = new string (testvalue.slibname);
String pathtolibrary= new String (testvalue.spathtolibrary);
OK, get it done.
If you want to remove the conversion of the char array from the following two sentences, which code is
Structure code in C #
[StructLayout (LayoutKind.Sequential, Charset=charset.ansi)]
public struct Libheader
{
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 256)]
public string Slibname;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 256)]
public string spathtolibrary;
Public Int32 ientries;
Public Int32 iused;
Public UInt16 Isort;
Public UInt16 iversion;
Public Boolean fcontainssubdirectories;
Public Int32 ireserved;
}
Other code can be used without modification.

C # vs. C + + data type comparison and struct-body conversions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.