[C#]C++与C#数据类型总结

原创文章,欢迎转载。转载请注明:转载自 祥的博客

原文链接:http://blog.csdn.net/humanking7/article/details/50981922


C++与C#数据类型总结

作用C++类型位数[Bytes]C#类型别名取值范围后缀
字符char1sbyteSystem.SByte-128~127
字符(u)unsigned char1byteSystem.Byte0~255
宽字符wchar_t2charSystem.Char
宽字符(无符号)unsigned wchar_t2
逻辑值bool1boolSystem.Booleantrue,false
短整数short2shortSystem.Int16-32,768 ~ 32,767
短整数(无符号)unsigned short2ushortSystem.UInt160~65535(2的16次方)
整数int4intSystem.Int32-2,147,483,648 ~ 2,147,483,647
整数(无符号)unsigned int4uintSystem.UInt320 ~ 4,294,967,295
长整型long8longSystem.Int64-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807L (最好用大写,因为小写l容易与1混淆)
长整型(无符号)unsigned long8ulongSystem.UInt640 ~ 18,446,744,073,709,551,615
单精度实数float4floatSystem.Single-3.402823e38 ~ 3.402823e38
双精度实数double8doubleSystem.Double-1.79769313486232e308 ~ 1.79769313486232e308d
长双精度实数long double10decimalSystem.Decimal-79228162514264337593543950335 ~ 79228162514264337593543950335m
字符串stringstringSystem.String
对象objectSystem.Object

C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试

C++C#
HANDLE(void *)System.IntPtr
Byte(unsigned char)System.Byte
SHORT(short)System.Int16
WORD(unsigned short)System.UInt16
INT(int)System.Int16
INT(int)System.Int32
UINT(unsigned int)System.UInt16
UINT(unsigned int)System.UInt32
LONG(long)System.Int32
ULONG(unsigned long)System.UInt32
DWORD(unsigned long)System.UInt32
DECIMALSystem.Decimal
BOOL(long)System.Boolean
CHAR(char)System.Char
LPSTR(char *)System.String
LPWSTR(wchar_t *)System.String
LPCSTR(const char *)System.String
LPCWSTR(const wchar_t *)System.String
PCAHR(char *)System.String
BSTRSystem.String
FLOAT(float)System.Single
DOUBLE(double)System.Double
VARIANTSystem.Object
PBYTE(byte *)System.Byte[]
BSTRStringBuilder
LPCTSTRStringBuilder
LPCTSTRstring
LPTSTR[MarshalAs(UnmanagedType.LPTStr)] string
LPTSTR 输出变量名StringBuilder 输出变量名
LPCWSTRIntPtr
BOOLbool
HMODULEIntPtr
HINSTANCEIntPtr
结构体public struct 结构体{};
结构体 **变量名out 变量名 //C#中提前申明一个结构体实例化后的变量名
结构体 &变量名ref 结构体 变量名
WORDushort
DWORDuint
DWORDint
UCHARint
UCHARbyte
UCHAR*string
UCHAR*IntPtr
GUIDGuid
HandleIntPtr
HWNDIntPtr
DWORDint
COLORREFuint
unsigned charbyte
unsigned char *ref byte
unsigned char *[MarshalAs(UnmanagedType.LPArray)] byte[]
unsigned char *[MarshalAs(UnmanagedType.LPArray)] Intptr
unsigned char &ref byte
unsigned char 变量名byte 变量名
unsigned short 变量名ushort 变量名
unsigned int 变量名uint 变量名
unsigned long 变量名ulong 变量名
char 变量名byte 变量名 //C++中一个字符用一个字节表示,C#中一个字符用两个字节表示
char 数组名[数组大小]MarshalAs(UnmanagedType.ByValTStr, SizeConst = 数组大小)] public string 数组名; ushort
char *string //传入参数
char *StringBuilder//传出参数
char *变量名ref string 变量名
char *输入变量名string 输入变量名
char *输出变量名[MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名
char **string
char **变量名ref string 变量名
const char *string
char[]string
char 变量名[数组大小][MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)] public string 变量名
struct 结构体名 *变量名ref 结构体名 变量名
委托 变量名委托 变量名
intint
intref int
int &ref int
int *ref int //C#中调用前需定义int 变量名 = 0;
*intIntPtr
int32 PIPTR *int32[]
float PIPTR *float[]
double** 数组名ref double 数组名
double*[] 数组名ref double 数组名
longint
ulongint
UINT8 *ref byte //C#中调用前需定义byte 变量名 = new byte();
handleIntPtr
hwndIntPtr
void *IntPtr
void * user_obj_paramIntPtr user_obj_param
void * 对象名称([MarshalAs(UnmanagedType.AsAny)]Object 对象名称
char, INT8, SBYTE, CHARSystem.SByte
short, short int, INT16, SHORTSystem.Int16
int, long, long int, INT32, LONG32, BOOL , INTSystem.Int32
__int64, INT64, LONGLONGSystem.Int64
unsigned char, UINT8, UCHAR , BYTESystem.Byte
unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_tSystem.UInt16
unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINTSystem.UInt32
unsigned __int64, UINT64, DWORDLONG, ULONGLONGSystem.UInt64
float, FLOATSystem.Single
double, long double, DOUBLESystem.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);

donate

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值