C # invoking C + + dynamic libraries when type conversion

Source: Internet
Author: User
Tags function prototype

Because I am mainly engaged in C # development, but in the work often need to use the C + + written DLL, so you need to know the type in C + + and how the type in C # is converted. The collection and collation are as follows.

The DLL function prototype in C + + is
extern "C" __declspec (dllexport) bool method name one (const char* variable name 1, unsigned char* variable name 2)
extern "C" __declspec (dllexport) bool Method name two (const unsigned char* variable name 1, char* variable name 2)

C # calls C + + DLLs to compile all the data type conversions, there may be duplicate or multiple scenarios, self-test
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 + +:D Word (unsigned long)----C#:system.uint32
C + +:D ecimal----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 + +:P Cahr (char *)----c#:system.string
C++:BSTR----c#:system.string
C++:float (FLOAT)----C#:system.single
C + +:D ouble (DOUBLE)----c#:system.double
C++:variant----C#:system.object
C + +:P byte (byte *)----c#:system.byte[]

 //C++:BSTR----c#:stringbuilder
  //c++:lpctstr----c#:stringbuilder
  //c++: LPCTSTR----c#:string
  //c++:lptstr----C#:[marshalas (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& nbsp
 //c++: struct----C #:p ublic struct struct {}; 
 //c++: struct * * variable name----c#:out variable name//c# Advance declaration of a struct-instantiated variable name br>  //c++: struct & variable name----c#:ref struct variable name
    

C++:word----C#:ushort
C + +:D Word----c#:uint
C + +:D Word----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 + +:D Word----c#:int
C++:colorref----C#:uint

c++:unsigned char----c#:byte
c++:unsigned char *----C#:REF byte
c++:unsigned char *----c#:[marshalas (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//c++ a character in 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//Incoming parameters
C++:char *----c#:stringbuilder//Outgoing parameters
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 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#:ref int//c# before calling to define int variable name = 0;

C + +:* int----c#:intptr
C++:int32 piptr *----c#:int32[]
C++:float piptr *----c#:float[]

C + +:d ouble** array name----C#:REF Double array name
C + +:d ouble*[] Array name----C#:REF Double array name
C++:long----C#:int
C++:ulong----C#:int

C++:uint8 *----C#:ref byte//c# before calling to define the byte variable name = new byte ();


 //c++:handle----c#:intptr
  //c++:hwnd----c#:intptr
    
& nbsp;  
 //c++:void *----c#:intptr  
 //c++:void * User_obj_param----C#:INTPT R User_obj_param
  //c++:void * Object name----C #:([MarshalAs (unmanagedtype.asany)]object 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, W ORD, ATOM, WCHAR, __wchar_t----c#:system.uint16 
  //c++:unsigned, unsigned int, UINT32, ULONG32, D WORD32, ULONG, DWORD, UINT----c#:system.uint32 
  //c++:unsigned __int64, UINT64, Dwordlong, Ulongl ONG----c#:system.uint64  
 //c++:float, float----c#:system.single  
 //c++: Double, long double, double----c#:system.double  

Win32 Types----CLR Type

Structs need to redefine a struct in C #
The callback callback function needs to be encapsulated in a delegate, delegate static extern int Funcallback (string str);

unsigned char** ppimage replaced with IntPtr ppimage
int& nwidth replaced with ref int nwidth
int*, Int&, then all can be ref int corresponding
Double needle finger type parameter, can be used ref INTPTR
The function pointer uses C + +: TypeDef double (*FUN_TYPE1) (double); Corresponds to C #:p ublic delegate Double fun_type1 (double);
char* Operation C + +: char*; Correspondence C#:stringbuilder;
Use pointers in C #: Add unsafe where you need to use pointers

C # invoking C + + dynamic libraries when type conversion

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.