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

在使用.Net互操作时,经常涉及到一些C++到C#的类型转换问题,故此整理了一份类型对照表方便大家查阅。
注意:请使用Ctrl + F查找您要找的类型即可!

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

C++数据类型

C#数据类型

WORDushort
DWORDuint
UCHARint/byte
UCHAR*string/InPtr
unsigned char*[MarshalAs(UnmanagedType.LPArray)]byte[]/(IntPtr)
char*string
LPCTSTR  string
LPTSTR [MarshalAs(UnmanagedType.LPTStr)] string
longint
ulonguint
Handle  IntPtr
HWNDIntPtr
void*IntPtr
int  int  
int* ref int
*int  IntPtr
unsigned intuint
COLORREF uint

Windows(C++)与C#数据类型对照表

Windows数据类型

.NET数据类型

BOOL, BOOLEANBoolean or Int32
BSTRString
BYTEByte
CHARChar
DOUBLEDouble
DWORDInt32 or UInt32
FLOATSingle
HANDLE (and all other handle types, such as HFONT and HMENU)IntPtr, UintPtr or HandleRef
HRESULTInt32 or UInt32
INTInt32
LANGIDInt16 or UInt16
LCIDInt32 or UInt32
LONGInt32
LPARAMIntPtr, UintPtr or Object
LPCSTRString
LPCTSTRString
LPCWSTRString
LPSTRString or StringBuilder*
LPTSTRString or StringBuilder
LPWSTRString or StringBuilder
LPVOIDIntPtr, UintPtr or Object
LRESULTIntPtr
SAFEARRAY.NET array type
SHORTInt16
TCHARChar
UCHARSByte
UINTInt32 or UInt32
ULONGInt32 or UInt32
VARIANTObject
VARIANT_BOOLBoolean
WCHARChar
WORDInt16 or UInt16
WPARAMIntPtr, UintPtr or Object

API与C#数据类型对照表

API数据类型

类型描述

C#数据类型

WORD16位无符号整数ushort
LONG32位无符号整数int
DWORD32位无符号整数uint
HANDLE句柄,32位整数int
UINT32位无符号整数uint
BOOL32位布尔型整数bool
LPSTR之乡字符的32位指针string
LPCSTR指向常字符的32位指针string
BYTE字节byte
CHAR字符char
DWORDLONG64位长度整数long
HDC设备描述表句柄int
HGDIOBJGDI对象句柄int
HINSTANCE实例句柄int
HWM窗口句柄int
HPARAM32位消息参数int
LPARAM32位消息参数int
WPARAM32位消息参数int

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

C++数据类型

C#数据类型

BOOLInt32
BOOLEANInt32
BYTEUInt16
CHARUInt16
COLORREFUInt32
DWORDUInt32
DWORD32UInt32
DWORD64UInt64
FLOATFloat
HACCELIntPtr
HANDLEIntPtr
HBITMAPIntPtr
HBRUSHIntPtr
HCONVIntPtr
HCONVLISTIntPtr
HCURSORIntPtr
HDCIntPtr
HDDEDATAIntPtr
HDESKIntPtr
HDROPIntPtr
HDWPIntPtr
HENHMETAFILEIntPtr
HFILEIntPtr
HFONTIntPtr
HGDIOBJIntPtr
HGLOBALIntPtr
HHOOKIntPtr
HICONIntPtr
HIMAGELISTIntPtr
HIMCIntPtr
HINSTANCEIntPtr
HKEYIntPtr
HLOCALIntPtr
HMENUIntPtr
HMETAFILEIntPtr
HMODULEIntPtr
HMONITORIntPtr
HPALETTEIntPtr
HPENIntPtr
HRGNIntPtr
HRSRCIntPtr
HSZIntPtr
HWINSTAIntPtr
HWNDIntPtr
INTInt32
INT32Int32
INT64Int64
LONGInt32
LONG32Int32
LONG64Int64
LONGLONGInt64
LPARAMIntPtr
LPBOOLInt16[]
LPBYTEUInt16[]
LPCOLORREFUInt32[]
LPCSTRString
LPCTSTRString
LPCVOIDUInt32
LPCWSTRString
LPDWORDUInt32
LPHANDLEUInt32
LPINTUInt32
LPLONGUInt32
LPSTRString
LPTSTRString
LPVOIDUInt32
LPWORDUInt32[]
LPWSTRString
LRESULTIntPtr
PBOOLInt16[]
PBOOLEANInt16[]
PBYTEUInt16[]
PCHARChar[]
PCSTRString
PCTSTRString
PCWCHUInt32
PCWSTRUInt32
PDWORDInt32[]
FPFLOATFloat[]
PHANDLEUInt32
PHKEYUInt32
PINTInt32[]
PLCIDUInt32
PLONGInt32[]
PLUIDUint32
PSHORTInt16[]
PSTRString
PTBYTEChar[]
PTCHARChar[]
PTSTRString
PUCHARChar[]
PUINTUInt32[]
PULONGUInt32[]
PUSHORTUInt16[]
PVOIDUInt32
PWCHARChar[]
PWORDInt16[]
PWSTRString
REGSAMUInt32
SC_HANDLEIntPtr
SC_LOCKIntPtr
SHORTInt16
SIZE_TUInt32
SSIZEUInt32
TBYTEChar
TCHARChar
UCHARByte
UINTUInt32
UINT32UInt32
UINT64UInt64
ULONGUInt32
ULONG32UInt32
ULONG64UInt64
ULONGLONGUInt64
USHORTUInt16
WORDUInt16
WPARAMIntPtr

Wtypes.h与C#数据类型对照表

Wtypes.h中的非托管类型非托管C语言类型托管类名位数
HANDLEvoid*IntPtr32
BYTEunsigned charByte8
SHORTshortInt1616
WORDunsigned shortUInt1616
INTintInt3232
UINTunsigned intUInt3232
LONGlongInt3232
BOOLlongInt3232
DWORDunsigned longUInt3232
ULONGunsigned longUInt3232
CHARcharChar 
LPSTRchar*StringStringBuilder用ANSI修饰 
LPCSTRConst char*StringStringBuilder用ANSI修饰 
LPWSTRwchar_t*StringStringBuilder用ANSI修饰 
LPCWSTRConstwchar_t*StringStringBuilder用ANSI修饰 
FLOATFloatSingle32
DOUBLEDoubleDouble64