C#呼び出しC++DLLのタイプ変換

20373 ワード

   //C#  C++ DLL               ,            ,     
   //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#:[MarshalAs(UnmanagedType.LPTStr)] string  
   //c++:LPTSTR       ---- c#:StringBuilder      
   //c++:LPCWSTR ---- c#:IntPtr
   //c++:BOOL ---- c#:bool   
   //c++:HMODULE ---- c#:IntPtr   
   //c++:HINSTANCE ---- c#:IntPtr  
   //c++:    ---- c#:public struct    {};  
   //c++:    **    ---- c#:out     //C#                  
   //c++:    &    ---- c#:ref        
     
 

   //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#:[MarshalAs(UnmanagedType.LPArray)] byte[]
   //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)] Intptr
 

   //c++:unsigned char & ---- c#:ref byte
   //c++:unsigned char     ---- c#:byte    
   //c++:unsigned short     ---- c#:ushort    
   //c++:unsigned int     ---- c#:uint    
   //c++:unsigned long     ---- c#:ulong    
 

   //c++:char     ---- c#:byte     //C++            ,C#            
   //c++:char    [    ] ---- c#:MarshalAs(UnmanagedType.ByValTStr, SizeConst =     )] public string    ; ushort
 

   //c++:char * ---- c#:string //    
   //c++:char * ---- c#:StringBuilder//    
   //c++:char *    ---- c#:ref string    
   //c++:char *      ---- c#:string      
   //c++:char *      ---- c#:[MarshalAs(UnmanagedType.LPStr)] StringBuilder      
 

   //c++:char ** ---- c#:string
   //c++:char **    ---- c#:ref string    
   //c++:const char * ---- c#:string
   //c++:char[] ---- c#:string
   //c++:char    [    ] ---- c#:[MarshalAs(UnmanagedType.ByValTStr,SizeConst=    )] public string    ;  
 

   //c++:struct      *    ---- c#:ref         
   //c++:       ---- c#:      
 

   //c++:int ---- c#:int
   //c++:int ---- c#:ref int
   //c++:int & ---- c#:ref int
   //c++:int * ---- c#:ref int //C#       int     = 0;
 

   //c++:*int ---- c#:IntPtr
   //c++:int32 PIPTR * ---- c#:int32[]
   //c++:float PIPTR * ---- c#:float[]
     
 

   //c++:double**     ---- c#:ref double    
   //c++:double*[]     ---- c#:ref double    
   //c++:long ---- c#:int
   //c++:ulong ---- c#:int
     
   //c++:UINT8 * ---- c#:ref byte //C#       byte     = new byte();   
 



   //c++:handle ---- c#:IntPtr
   //c++:hwnd ---- c#:IntPtr
     
     
   //c++:void * ---- c#:IntPtr   
   //c++:void * user_obj_param ---- c#:IntPtr user_obj_param
   //c++:void *      ---- c#:([MarshalAs(UnmanagedType.AsAny)]Object     
 



    
   //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   C#       Struct
   //CallBack              ,delegate static extern int FunCallBack(string str);
 

   //unsigned char** ppImage   IntPtr ppImage
   //int& nWidth   ref int nWidth
   //int*, int&,      ref int   
   //       ,    ref IntPtr
   //      c++: typedef double (*fun_type1)(double);    c#:public delegate double fun_type1(double);
   //char*    c++: char*;    c#:StringBuilder;
   //c#     :             unsafe
 



   //unsigned char  public byte
   /*
   * typedef void (*CALLBACKFUN1W)(wchar_t*, void* pArg);
   * typedef void (*CALLBACKFUN1A)(char*, void* pArg);
   * bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg);
   *      
   * [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   * public delegate void CallbackFunc1([MarshalAs(UnmanagedType.LPWStr)] StringBuilder strName, IntPtr pArg);
   *  
   *  
   */

 
//C#はC++のDLLを呼び出して整理するすべてのデータ型の変換方式を収集して、繰り返しあるいは多種の方案があるかもしれなくて、自分で多く//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#:[MarshalAs(UnmanagedType.LPTStr)]string//c++:LPTSTR出力変数名----c#:StringBuilder出力変数名//c+:LPCWSTR----c#:IntPtr//c+:BOOL----c#:bool//c+:HMODULE----c#:IntPtr//c+:HINSTANCE----c#:IntPtr//c+:構造体----c#:public struct構造体{};//c++:構造体**変数名----c#:out変数名//C#で構造体をインスタンス化した変数名を事前に明記//c+:構造体&変数名----c#:ref構造体変数名//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#:[MarshalAs(UnmanagedType.LPArray)] byte[]   //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)]Intpr//c+:unsigned char&----c#:refbyte//c+:unsigned char変数名----c#:byte変数名//c+:unsigned short変数名----c#:ushort変数名//c+::unsigned int変数名----c#:uint変数名//c+:unsigned long変数名----c#:ulong変数名//c+:char変数名----c#:byte変数名//C++の1文字符号は1バイトで、C#の1文字は//c++:char配列名[配列サイズ]----c#:MarshalAs(UnmanagedType.ByValTStr,SizeConst=配列サイズ)]public string配列名;ushort//c+:char*----c#:string//入力パラメータ//c+:char*----c#:StringBuilder//出力パラメータ//c++::char*変数名----c#:ref string変数名//c+:char*入力変数名----c#:string入力変数名//c++:char*出力変数名----c#:[MarshalAs(UnmanagedType.LPStr)]StringBuilder出力変数名//c++:char*----c#:string//c+:char**変数名----c#:ref string変数名//c++:const char*----c#:string//c+:char[]----c#:string//c+:char変数名[配列サイズ]----c#:[MarshalAs(UnmanagedType.ByValTStr,SizeConst=配列サイズ)]public string変数名;//c++:struct構造体名*変数名----c#:ref構造体名変数名//c+:委任変数名----c#:委任変数名//c+::int----c#:int//c+::int----c#:refint//c+::int&----c#:refint//c+::int*----c#:refint//C+:int*----c#:refint//C#で呼び出す前にint変数名=0を定義する必要があります.//c++:*int----c#:IntPtr//c+:int 32 PIPTR*----c#:int 32[]//c+::float PIPTR*----c#:float[]//c+:double**配列名----c#:ref double配列名//c+:double*[]配列名----c#:ref double配列名//c+:long----c#:int//c+:ulong----c#:int//c+:UINT 8*----c#:refbyte//C#で呼び出す前にbyte変数名=new byte()を定義する必要があります.     //c++:handle ---- c#:IntPtr   //c++:hwnd ---- c#:IntPtr             //c++:void * ---- c#:IntPtr      //c++:void * user_obj_param ---- c#:IntPtr user_obj_param//c+::void*オブジェクト名----c#:([MarshalAs(UnmanagedType.AsAny)]Objectオブジェクト名//c++:char,INT 8,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//Win 32 Type----CLR Type//struct C#でStruct//CallBackコールバック関数を再定義するには、依頼にカプセル化する必要があります.delegate static extern int FunCallBack(string str);//unsigned char**ppImageをIntPtr ppImage//int&nWidthをrefint nWidth//int*、int&に置き換えると、refint対応//ダブルポインタタイプパラメータが使用でき、ref IntPtr//関数ポインタでc++:typedef double(*fun_type 1)(double)を使用できます.対応c#:public delegate double fun_type1(double);//char*の操作c+::char*;対応c#:StringBuilder;//c#でポインタを使う:ポインタを使う必要があるところにunsafe//unsigned char対応public byte/**typedef void(*CALLBACKFUN 1 W)(wchar_t*,void*pArg);   * typedef void (*CALLBACKFUN1A)(char*, void* pArg);   * bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg);*呼び出し方式は*[UnmanagedFunctionPointer(CallingConvention.Cdecl)]*public delegate void CallbackFunc 1([MarshalAs(UnmanagedType.LPWstr)]StringBuilder strName,IntPtr pArg);   *     *     */