C#与C++之间类型的对应表

 

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

C++数据类型

C#数据类型

WORD

ushort

DWORD

uint

UCHAR

int/byte

UCHAR*

string/InPtr

unsigned char*

[MarshalAs(UnmanagedType.LPArray)]byte[]/(IntPtr)

char*

string

LPCTSTR  

string

LPTSTR 

[MarshalAs(UnmanagedType.LPTStr)] string

long

int

ulong

uint

Handle  

IntPtr

HWND

IntPtr

void*

IntPtr

int  

int  

int* 

ref int

*int  

IntPtr

unsigned int

uint

COLORREF 

uint

 

 

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

Windows数据类型

.NET数据类型

BOOLBOOLEAN

Boolean or Int32

BSTR

String

BYTE

Byte

CHAR

Char

DOUBLE

Double

DWORD

Int32 or UInt32

FLOAT

Single

HANDLE (and all other handle types, such as HFONT and HMENU)

IntPtr, UintPtr or HandleRef

HRESULT

Int32 or UInt32

INT

Int32

LANGID

Int16 or UInt16

LCID

Int32 or UInt32

LONG

Int32

LPARAM

IntPtr, UintPtr or Object

LPCSTR

String

LPCTSTR

String

LPCWSTR

String

LPSTR

String or StringBuilder*

LPTSTR

String or StringBuilder

LPWSTR

String or StringBuilder

LPVOID

IntPtr, UintPtr or Object

LRESULT

IntPtr

SAFEARRAY

.NET array type

SHORT

Int16

TCHAR

Char

UCHAR

SByte

UINT

Int32 or UInt32

ULONG

Int32 or UInt32

VARIANT

Object

VARIANT_BOOL

Boolean

WCHAR

Char

WORD

Int16 or UInt16

WPARAM

IntPtr, UintPtr or Object

 

API与C#数据类型对照表

API数据类型

类型描述

C#数据类型

WORD

16位无符号整数

ushort

LONG

32位无符号整数

int

DWORD

32位无符号整数

uint

HANDLE

句柄,32位整数

int

UINT

32位无符号整数

uint

BOOL

32位布尔型整数

bool

LPSTR

之乡字符的32位指针

string

LPCSTR

指向常字符的32位指针

string

BYTE

字节

byte

CHAR

字符

char

DWORDLONG

64位长度整数

long

HDC

设备描述表句柄

int

HGDIOBJ

GDI对象句柄

int

HINSTANCE

实例句柄

int

HWM

窗口句柄

int

HPARAM

32位消息参数

int

LPARAM

32位消息参数

int

WPARAM

32位消息参数

int

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

 

Wtypes.h中的非托管类型

非托管C语言类型

托管类名

位数

 

HANDLE

void*

IntPtr

32

 

BYTE

unsigned char

Byte

8

 

SHORT

short

Int16

16

 

WORD

unsigned short

UInt16

16

 

INT

int

Int32

32

 

UINT

unsigned int

UInt32

32

 

LONG

long

Int32

32

 

BOOL

long

Int32

32

 

DWORD

unsigned long

UInt32

32

 

ULONG

unsigned long

UInt32

32

 

CHAR

char

Char

 

 

LPSTR

char*

StringStringBuilder用ANSI修饰

 

 

LPCSTR

Const char*

StringStringBuilder用ANSI修饰

 

 

LPWSTR

wchar_t*

StringStringBuilder用ANSI修饰

 

 

LPCWSTR

Constwchar_t*

StringStringBuilder用ANSI修饰

 

 

FLOAT

Float

Single

32

 

DOUBLE

Double

Double

64

 

 

 

 

 

 

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

 

C++数据类型

C#数据类型

 

BOOL

Int32

 

BOOLEAN

Int32

 

BYTE

UInt16

 

CHAR

UInt16

 

COLORREF

UInt32

 

DWORD

UInt32

 

DWORD32

UInt32

 

DWORD64

UInt64

 

FLOAT

Float

 

HACCEL

IntPtr

 

HANDLE

IntPtr

 

HBITMAP

IntPtr

 

HBRUSH

IntPtr

 

HCONV

IntPtr

 

HCONVLIST

IntPtr

 

HCURSOR

IntPtr

 

HDC

IntPtr

 

HDDEDATA

IntPtr

 

HDESK

IntPtr

 

HDROP

IntPtr

 

HDWP

IntPtr

 

HENHMETAFILE

IntPtr

 

HFILE

IntPtr

 

HFONT

IntPtr

 

HGDIOBJ

IntPtr

 

HGLOBAL

IntPtr

 

HHOOK

IntPtr

 

HICON

IntPtr

 

HIMAGELIST

IntPtr

 

HIMC

IntPtr

 

HINSTANCE

IntPtr

 

HKEY

IntPtr

 

HLOCAL

IntPtr

 

HMENU

IntPtr

 

HMETAFILE

IntPtr

 

HMODULE

IntPtr

 

HMONITOR

IntPtr

 

HPALETTE

IntPtr

 

HPEN

IntPtr

 

HRGN

IntPtr

 

HRSRC

IntPtr

 

HSZ

IntPtr

 

HWINSTA

IntPtr

 

HWND

IntPtr

 

INT

Int32

 

INT32

Int32

 

INT64

Int64

 

LONG

Int32

 

LONG32

Int32

 

LONG64

Int64

 

LONGLONG

Int64

 

LPARAM

IntPtr

 

LPBOOL

Int16[]

 

LPBYTE

UInt16[]

 

LPCOLORREF

UInt32[]

 

LPCSTR

String

 

LPCTSTR

String

 

LPCVOID

UInt32

 

LPCWSTR

String

 

LPDWORD

UInt32

 

LPHANDLE

UInt32

 

LPINT

UInt32

 

LPLONG

UInt32

 

LPSTR

String

 

LPTSTR

String

 

LPVOID

UInt32

 

LPWORD

UInt32[]

 

LPWSTR

String

 

LRESULT

IntPtr

 

PBOOL

Int16[]

 

PBOOLEAN

Int16[]

 

PBYTE

UInt16[]

 

PCHAR

Char[]

 

PCSTR

String

 

PCTSTR

String

 

PCWCH

UInt32

 

PCWSTR

UInt32

 

PDWORD

Int32[]

 

FPFLOAT

Float[]

 

PHANDLE

UInt32

 

PHKEY

UInt32

 

PINT

Int32[]

 

PLCID

UInt32

 

PLONG

Int32[]

 

PLUID

Uint32

 

PSHORT

Int16[]

 

PSTR

String

 

PTBYTE

Char[]

 

PTCHAR

Char[]

 

PTSTR

String

 

PUCHAR

Char[]

 

PUINT

UInt32[]

 

PULONG

UInt32[]

 

PUSHORT

UInt16[]

 

PVOID

UInt32

 

PWCHAR

Char[]

 

PWORD

Int16[]

 

PWSTR

String

 

REGSAM

UInt32

 

SC_HANDLE

IntPtr

 

SC_LOCK

IntPtr

 

SHORT

Int16

 

SIZE_T

UInt32

 

SSIZE

UInt32

 

TBYTE

Char

 

TCHAR

Char

 

UCHAR

Byte

 

UINT

UInt32

 

UINT32

UInt32

 

UINT64

UInt64

 

ULONG

UInt32

 

ULONG32

UInt32

 

ULONG64

UInt64

 

ULONGLONG

UInt64

 

USHORT

UInt16

 

WORD

UInt16

 

WPARAM

IntPtr

 

           

习惯用C#写东西,但平时又会碰到很多要用win32 API的地方,所以经常要用DllImport,但win32函数的类型写法是很庞杂的,相信为之困扰的不止我一个,现在我整理一份我个人的理解如下,希望高人不吝赐教。
我的基本原则有如下几点:
1、下面都是针对32位系统的,所以int是32位.long也是32位;
2、各种句柄类的(H开头),我认为一律是System.IntPtr,到目前为止没发现出错;如果哪位在使用中出错,请指出;
3、LP和P,我实在不懂(对C 不太了解),对于LP和P开头的函数,如果是和STR有关的,一律写为System.String,像PLCID这样指向什么东西的,写为System.UInt32(因为指向另一个地址,那就是指针,指针是32位吧),int之类的数值型,那我就写为int[],以方便.net程序引用(写成System.UInt32的话,给API调用应该也不会出错,但.net程序就不好引用了)

如有意见,欢迎指教。谢谢

BOOL=System.Int32
BOOLEAN=System.Int32
BYTE=System.UInt16
CHAR=System.Int16
COLORREF=System.UInt32
DWORD=System.UInt32
DWORD32=System.UInt32
DWORD64=System.UInt64
FLOAT=System.Float
HACCEL=System.IntPtr
HANDLE=System.IntPtr
HBITMAP=System.IntPtr
HBRUSH=System.IntPtr
HCONV=System.IntPtr
HCONVLIST=System.IntPtr
HCURSOR=System.IntPtr
HDC=System.IntPtr
HDDEDATA=System.IntPtr
HDESK=System.IntPtr
HDROP=System.IntPtr
HDWP=System.IntPtr
HENHMETAFILE=System.IntPtr
HFILE=System.IntPtr
HFONT=System.IntPtr
HGDIOBJ=System.IntPtr
HGLOBAL=System.IntPtr
HHOOK=System.IntPtr
HICON=System.IntPtr
HIMAGELIST=System.IntPtr
HIMC=System.IntPtr
HINSTANCE=System.IntPtr
HKEY=System.IntPtr
HLOCAL=System.IntPtr
HMENU=System.IntPtr
HMETAFILE=System.IntPtr
HMODULE=System.IntPtr
HMONITOR=System.IntPtr
HPALETTE=System.IntPtr
HPEN=System.IntPtr
HRGN=System.IntPtr
HRSRC=System.IntPtr
HSZ=System.IntPtr
HWINSTA=System.IntPtr
HWND=System.IntPtr
INT=System.Int32
INT32=System.Int32
INT64=System.Int64
LONG=System.Int32
LONG32=System.Int32
LONG64=System.Int64
LONGLONG=System.Int64
LPARAM=System.IntPtr
LPBOOL=System.Int16[]
LPBYTE=System.UInt16[]
LPCOLORREF=System.UInt32[]
LPCSTR=System.String
LPCTSTR=System.String
LPCVOID=System.UInt32
LPCWSTR=System.String
LPDWORD=System.UInt32[]
LPHANDLE=System.UInt32
LPINT=System.Int32[]
LPLONG=System.Int32[]
LPSTR=System.String
LPTSTR=System.String
LPVOID=System.UInt32
LPWORD=System.Int32[]
LPWSTR=System.String
LRESULT=System.IntPtr
PBOOL=System.Int16[]
PBOOLEAN=System.Int16[]
PBYTE=System.UInt16[]
PCHAR=System.Char[]
PCSTR=System.String
PCTSTR=System.String
PCWCH=System.UInt32
PCWSTR=System.UInt32
PDWORD=System.Int32[]
PFLOAT=System.Float[]
PHANDLE=System.UInt32
PHKEY=System.UInt32
PINT=System.Int32[]
PLCID=System.UInt32
PLONG=System.Int32[]
PLUID=System.UInt32
PSHORT=System.Int16[]
PSTR=System.String
PTBYTE=System.Char[]
PTCHAR=System.Char[]
PTSTR=System.String
PUCHAR=System.Char[]
PUINT=System.UInt32[]
PULONG=System.UInt32[]
PUSHORT=System.UInt16[]
PVOID=System.UInt32
PWCHAR=System.Char[]
PWORD=System.Int16[]
PWSTR=System.String
REGSAM=System.UInt32
SC_HANDLE=System.IntPtr
SC_LOCK=System.IntPtr
SHORT=System.Int16
SIZE_T=System.UInt32
SSIZE_=System.UInt32
TBYTE=System.Char
TCHAR=System.Char

 

博主设置当前文章不允许评论。

没有更多推荐了,返回首页