C++和C#变量对应表

C++ 强制类型转换

static_cast

static_cast<type-id>(expression)

expression 转换为 type-id 类型。

static_cast 是静态类型转换,发生在编译期。这种转换不会进行运行时的动态检查(RTTI),因而这种转换可能是不安全的。

dynamic_cast

dynamic_cast<type-id>(expression)

expression 转换为 type-id 类型,type-id 必须是类的指针、类的引用或者是 void *;如果 type-id 是指针类型,那么 expression 也必须是一个指针;如果 type-id 是一个引用,那么 expression 也必须是一个引用。

dynamic_cast 提供了运行时的检查。对于指针类型,在运行时会检查 expression 是否真正的指向一个 type-id 类型的对象,如果是,则能进行正确的转换;否则返回 nullptr。对于引用类型,若是无效转换,则在运行时会抛出异常 std::bad_cast

const_cast

const_cast 用来去掉表达式的 const 修饰或 volatile 修饰,也就是将 constvolatile 类型转换为非 const 或 非 volatile 类型。

reinterpret_cast

reinterpret_cast 转换直接对二进制位按照目标类型重新解释,非常粗暴,所以风险很高,慎重使用。

C++中的DLL函数原型为

  extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsigned char* 变量名2)
  extern "C" __declspec(dllexport) bool 方法名二(const unsigned char* 变量名1, char* 变量名2)

C++ 与 C# 数据类型对照表

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
*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
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值