C # Call all data type conversion methods of C ++ DLL

Source: Internet
Author: User

I thought the collected code would be a good article. It took a day to find an article called "C # And C ++ data type table. it's almost covered by most of the data, which is a blow to me. some data in this article is not tested. there are also some good news that I have read more than a hundred articles. hope to help.

The prototype of the DLL function in/C ++ is // extern "C" _ declspec (dllexport) bool method name 1 (const char * variable name 1, unsigned char * variable name 2) // extern "C" _ declspec (dllexport) bool method name 2 (const unsigned char * variable name 1, char * variable name 2) // C # Call the C ++ DLL to collect and sort all the data type conversion methods. There may be repeated or multiple solutions. perform multiple tests on your own. // C ++: handle (void *) ---- C #: system. intptr // C ++: byte (unsigned char) ---- C #: system. byte // C ++: Short (short) ---- C #: system. int16 // C ++: Word (unsigned short) ---- C #: system. uint16 // C ++: int (INT) ---- C #: system. int16 // C ++: int (INT) ---- C #: system. int32 // C ++: uint (unsigned INT) ---- C #: system. uint16 // C ++: uint (unsigned INT) ---- C #: system. uint32 // C ++: Long (long) ---- C #: system. int32 // C ++: ulong (unsigned long) ---- C #: system. uint32 // C ++: DWORD (unsigned long) ---- C #: system. uint32 // C ++: decimal ---- C #: system. decimal // C ++: bool (long) ---- C #: system. boolean // C ++: Char (char) ---- C #: system. char // C ++: lpstr (char *) ---- C #: system. string // C ++: lpwstr (wchar_t *) ---- C #: system. string // C ++: lpcstr (const char *) ---- C #: system. string // C ++: lpcwstr (const wchar_t *) ---- C #: system. string // C ++: pcahr (char *) ---- C #: system. string // C ++: BSTR ---- C #: system. string // C ++: Float (float) ---- C #: system. single // C ++: Double (double) ---- C #: system. double // C ++: variant ---- C #: system. object // C ++: pbyte (byte *) ---- C #: system. byte [] // C ++: BSTR ---- C #: stringbuilder // C ++: lpctstr ---- C #: stringbuilder/C ++: lpctstr ---- C #: string // C ++: lptstr ---- C #: [financialas (unmanagedtype. lptstr)] string // C ++: lptstr output variable name ---- C #: stringbuilder output variable name/C ++: lpcwstr ---- C #: intptr/C ++: bool ---- C #: bool // C ++: hmodule ---- C #: intptr // C ++: hinstance ---- C #: intptr/C ++: struct ---- C #: public struct {}; // C ++: struct ** variable name ---- C #: out variable name // C # declare the variable name after a struct is instantiated in advance // C ++: struct & variable name ---- C #: ref struct variable name // C ++: Word ---- C #: ushort/C ++: DWORD ---- C #: uint/C ++: DWORD ---- C #: int // C ++: uchar ---- C #: int/C ++: uchar ---- C #: Byte/C ++: uchar * ---- C #: string // C ++: uchar * ---- C #: intptr // C ++: guid ---- C #: guid // C ++: handle ---- C #: intptr // C ++: hwnd ---- C #: intptr // C ++: DWORD ---- C #: int // C ++: colorref ---- C #: uint // C ++: unsigned char ---- C #: Byte/C ++: unsigned char * ---- C #: ref byte/C ++: unsigned char * ---- C #: [financialas (unmanagedtype. lparray)] Byte [] // C ++: unsigned char * ---- C #: [marshalas (unmanagedtype. lparray)] intptr // C ++: unsigned char & ---- C #: ref byte // C ++: unsigned char variable name ---- C #: byte variable name // C ++: Unsigned short variable name ---- C #: ushort variable name/C ++: Unsigned int variable name ---- C #: uint variable name // C ++: Unsigned long variable name ---- C #: ulong variable name // C ++: Char variable name ---- C #: byte variable name // a character in C ++ is represented by one byte, and a character in C # is represented by two bytes/C ++: char array name [array size] ---- C #: marshalas (unmanagedtype. byvaltstr, sizeconst = array size)] Public String Array name; ushort // C ++: char * ---- C #: String // input parameter // C ++: char * ---- C #: stringbuilder // output parameter // C ++: char * variable name ---- C #: ref string variable name // C ++: char * input variable name ---- C #: String input variable name // C ++: char * output variable name ---- C #: [marshalas (unmanagedtype. lpstr)] stringbuilder output variable name // C ++: Char ** ---- C #: String // C ++: Char ** variable name ---- C #: ref string variable name // C ++: const char * ---- C #: String // C ++: Char [] ---- C #: String // C ++: char variable name [array size] ---- C #: [marshalas (unmanagedtype. byvaltstr, sizeconst = array size)] public string variable name; // C ++: struct name * variable name ---- C #: ref struct name variable name/C ++: delegate variable name ---- C #: Delegate variable name // C ++: int ---- C #: int // C ++: int ---- C #: ref int // C ++: Int & ---- C #: ref int // C ++: int * ---- C #: before calling ref int // C #, you must define the int variable name = 0; // C ++: * int ---- C #: intptr // C ++: int32 piptr * ---- C #: int32 [] // C ++: Float piptr * ---- C #: float [] // C ++: double ** array name ---- C #: ref double array name/C ++: Double * [] array name ---- C #: ref double array name // C ++: long ---- C #: int/C ++: ulong ---- C #: int/C ++: uint8 * ---- C #: before calling ref byte // C #, you must define the BYTE variable name = new byte (); // C ++: handle ---- C #: intptr/C ++: hwnd ---- C #: intptr // C ++: void * user_obj_param ---- C #: intptr user_obj_param/C ++: void * Object Name ---- C # :( [financialas (unmanagedtype. asany)] Object Name // C ++: Char, int8, sbyte, char ---- C #: system. sbyte // C ++: short, short int, int16, short ---- C #: system. int16 // C ++: int, long, long int, int32, long32, bool, int ---- C #: system. int32 // C ++ :__ int64, int64, Longlong ---- C #: system. int64 // C ++: unsigned char, uint8, uchar, byte ---- C #: system. byte // C ++: Unsigned short, uint16, ushort, word, Atom, wchar, _ wchar_t ---- C #: system. uint16 // C ++: unsigned, unsigned int, uint32, ulong32, dword32, ulong, DWORD, uint ---- C #: system. uint32/C ++: Unsigned _ int64, uint64, dwordlong, ulonglong ---- C #: system. uint64 // C ++: float, float ---- C #: system. single // C ++: Double, Long Double, double ---- C #: system. double // Win32 types ---- CLR type // struct needs to be redefined in C # A struct // callback function needs to be encapsulated in a delegate, delegate static extern int funcallback (string Str); // unsigned char ** replace ppimage with intptr ppimage // Int & nwidth with ref int nwidth // int *, Int &, you can use ref int corresponding to // double-needle type parameters. You can use ref intptr // function pointer to use C ++: typedef double (* fun_type1) (double); corresponding to C #: public Delegate double fun_type1 (double); // char * operation C ++: char *; corresponding to C #: stringbuilder; // Use Pointer in C: add unsafe // unsigned char to public byte/** typedef void (* callbackfun1w) (wchar_t *, void * parg); * typedef void (* callbackfun1a) (char *, void * parg); * bool bioprint_sensor_api dllfun1 (callbackfun1 pcallbackfun1, void * parg); * The call method is * [unmanagedfunctionpointer (callingconvention. cdecl)] * Public Delegate void callbackfunc1 ([financialas (unmanagedtype. lpwstr)] stringbuilder strname, intptr parg );***/
C ++ C #=============================================== = word ushortdword uintuchar int/byte can be replaced by INT in most cases, if strict alignment is required, use bytebyteuchar * string/intptrunsigned char * [externalas (unmanagedtype. lparray)] Byte []/? (Intptr) char * stringlpctstr stringlptstr [financialas (unmanagedtype. lptstr)] stringlong intulong uinthandle intptrhwnd intptrvoid * intptrint intint * ref int * int intptrunsigned int uintcolorref uintapi and C # data type ing table API data type description C # type API data type description C # type word 16-bit unsigned integer ushort char character charlong 32-bit unsigned integer int dwordlong 64-bit long integer longdword 32-bit unsigned integer uint HDC device description table handle inthandle handle, 32-bit integer int hgdiobj GDI object handle intuint 32-bit Unsigned integer uint hinstance instance handle intbool 32-bit Boolean integer bool hwm window handle intlpstr pointing to character 32-bit pointer string hparam 32-bit Message Parameter intlpcstr pointing to common character 32-bit pointer string lparam 32-bit Message Parameter intbyte byte wparam 32-bit Message Parameter intbool = system. int32boolean = system. int32byte = system. uint16char = system. int16colorref = system. uint32dword = system. uint32dword32 = system. uint32dword64 = system. uint64float = system. floathaccel = system. intptrhandle = system. intptrhbitmap = SY Stem. intptrhbrush = system. intptrhconv = system. intptrhconvlist = system. intptrhcursor = system. intptrhdc = system. intptrhddedata = system. intptrhdesk = system. intptrhdrop = system. intptrhdwp = system. intptrhenhmetafile = system. intptrhfile = system. intptrhfont = system. intptrhgdiobj = system. intptrhglobal = system. intptrhhook = system. intptrhicon = system. intptrhimagelist = system. intptrhimc = system. intptrhinstance = system. intptrhk Ey = system. intptrhlocal = system. intptrhmenu = system. intptrhmetafile = system. intptrhmodule = system. intptrhmonitor = system. intptrhpalette = system. intptrhpen = system. intptrhrgn = system. intptrhrsrc = system. intptrhsz = system. intptrhwinsta = system. intptrhwnd = system. intptrint = system. int32int32 = system. int32int64 = system. int64long = system. int32long32 = system. int32long64 = system. int64longlong = system. int64lparam = system. INTP Trlpbool = system. int16 [] lpbyte = system. uint16 [] lpcolorref = system. uint32 [] lpcstr = system. stringlpctstr = system. stringlpcvoid = system. uint32lpcwstr = system. stringlpdword = system. uint32 [] lphandle = system. uint32lpint = system. int32 [] lplong = system. int32 [] lpstr = system. stringlptstr = system. stringlpvoid = system. uint32lpword = system. int32 [] lpwstr = system. stringlresult = system. intptrpbool = system. int16 [] pboolean = system. int 16 [] pbyte = system. uint16 [] pchar = system. char [] pcstr = system. stringpctstr = system. stringpcwch = system. uint32pcwstr = system. uint32pdword = system. int32 [] pfloat = system. float [] phandle = system. uint32phkey = system. uint32pint = system. int32 [] plcid = system. uint32plong = system. int32 [] pluid = system. uint32pshort = system. int16 [] pstr = system. stringptbyte = system. char [] ptchar = system. char [] ptstr = system. stringpuchar = system. char [] P Uint = system. uint32 [] Pulong = system. uint32 [] pushort = system. uint16 [] pvoid = system. uint32pwchar = system. char [] pword = system. int16 [] pwstr = system. stringregsam = system. uint32sc_handle = system. intptrsc_lock = system. intptrshort = system. int16size_t = system. uint32ssize _ = system. uint32tbyte = system. chartchar = system. charuchar = system. byteuint = system. uint32uint32 = system. uint32uint64 = system. uint64ulong = system. uint32ulong32 = System. uint32ulong64 = system. uint64ulonglong = system. uint64ushort = system. uint16word = system. uint16wparam = system. intptrwtypes. handle void * system. intptr 32-bit byte unsigned char system. byte 8-bit 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 system. int32 32-bit bool long system. int32 32-bit dwo Rd unsigned long system. uint32 32-bit ulong unsigned long system. uint32 32-bit 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 system. the structure data in double 64-bit C/C ++ is converted under C #. During project migration, data type conversion is often encountered, what I encountered this time is 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 B Oolean fcontainssubdirectories; Public int32 ireserved;} it seems that there is no problem. hehahaha, in fact, this is not the case. We have to give 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 ("bytesbuffer parameter and 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:/ja Gged 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 following two sentences, the structure code [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 fcontainssubdirec Tories; Public int32 ireserved;} Other code can be used without modification.

Related Article

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.