C # comparison with C ++ data types and Structure Conversion (Collection and collation II)

Source: Internet
Author: User

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

 

C ++ C #
============================================
Word ushort
DWORD uint
Uchar int/byte can be replaced by INT in most cases. If strict alignment is required, bytebyte should be used.
Uchar * string/intptr
Unsigned char * [financialas (unmanagedtype. lparray)] Byte []/? (Intptr)
Char * string
Lpctstr string
Lptstr [financialas (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

Data Type ing between APIs and C #
API data type description C # type
Word 16-bit unsigned integer ushort char character char
Long 32-bit unsigned integer int dwordlong 64-bit long integer
DWORD 32-bit unsigned integer uint HDC device description 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 points to the 32-Bit String hparam 32-bit Message Parameter int
The 32-bit pointer 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

Description of unmanaged C-language managed classes in wtypes. h
Handle void * system. intptr 32-bit
Byte unsigned char system. byte 8-bit
Short short system. int16-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 modified with ANSI.
Lpstr char * system. string or system. stringbuilder is modified with ANSI.
Lpcstr const char * system. string or system. stringbuilder is modified with ANSI.
Lpwstr wchar_t * system. string or system. stringbuilder is modified with Unicode.
Lpcwstr const wchar_t * system. string or system. stringbuilder are modified with Unicode.
Float float system. Single 32-bit
Double double system. Double 64-bit
 
Conversion of structure data in C/C ++ under C #
 
During project transplantation, data type conversion is often encountered, and this time I encountered how the structure in C/C ++ is converted to C #. After one night, I finally had a perfect solution.
For example, the structure data in C/C ++ is as follows:
Typedef struct
{
Char slibname [256];
Char spathtolibrary [256];
Int32 ientries;
Int32 iused;
Uint16 isort;
Uint16 iversion;
Boolean fcontainssubdirectories;
Int32 ireserved;
} Libheader;
We want to convert it to the following structure type in 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;
}
It seems that there is no problem. hehahaha, in fact this is not the case. We have to give the C # compiler some information to tell it the size of some character arrays. Then they look like this in C:
[Structlayout (layoutkind. Sequential)]
Public struct libheader
{
[Financialas (unmanagedtype. byvalarray, sizeconst = 256)]
Public char [] slibname;
[Financialas (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 for conversion.
Public structtype converbytestostructure <structtype> (byte [] bytesbuffer)
{
// Check the length.
If (bytesbuffer. length! = Marshal. sizeof (typeof (structtype )))
{
Throw new argumentexception ("The bytesbuffer parameter and the structobject parameter have different bytes. ");
}

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;
}
Then our function example is as follows:
Filestream file = file. openread (@ "D:/jarged 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.
If you want to remove the code for converting the char array in the next two sentences, which code is as follows:
Structure Code in C #
[Structlayout (layoutkind. Sequential, charset = charset. ANSI)]
Public struct libheader
{
[Financialas (unmanagedtype. byvaltstr, sizeconst = 256)]
Public String slibname;
[Financialas (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.

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.