Mapping type between C++ and C#

 

API Data Type

Type Description

C# Type

API Data Type

Type Description

C# Type

WORD

16 bit unsigned int

ushort

CHAR

char

char

LONG

32 bit unsigned int

int

DWORDLONG

64 bit long int

long

DWORD

32 bit unsigned int

uint

HDC

Device decription table handle

int

HANDLE

handle,32 bit int

int

HGDIOBJ

GDI object handle

int

UINT

32 bit unsigned int

uint

HINSTANCE

Instance handle

int

BOOL

32bit bool

bool

HWM

Window handle

int

LPSTR

32-bit pointer pointing to the character

string

HPARAM

32 bit message parameters

int

LPCSTR

32-bit pointer pointing to the  const character

String

LPARAM

32 bit message parameters

int

BYTE

byte

byte

WPARAM

32 bit message parameters

int

 

Unmanaged Types in Wtypes.h

Unmanaged C Types

 Managed class name

Comment

HANDLE

void* 

System.IntPtr

32 bit

BYTE

unsigned char

System.Byte

8 bit

SHORT

short

System.Int16

16 bit

WORD

unsigned short

System.UInt16

16 bit

INT

 int

System.Int32

32 bit

UINT

unsigned int

System.UInt32

32 bit

LONG

long

System.Int32

32 bit

BOOL

long

System.Int32

32 bit

DWORD

unsigned long

System.UInt32

32

ULONG

unsigned long

System.UInt32

32

CHAR

char

System.Char

use ANSI

LPSTR

char*

System.String System.StringBuilder

use ANSI

LPCSTR

Const char*

System.String System.StringBuilder

use ANSI

LPWSTR 

wchar_t*

System.String System.StringBuilder

use Unicode

LPCWSTR

Const wchar_t*

System.String System.StringBuilder

use Unicode

FLOAT 

Float

System.Single

32 bit

DOUBLE 

Double

System.Double

64 bit

 

 

Types map to System namespace in C++

 

 

 

 

BOOL=System.Int32

HGDIOBJ=System.IntPtr

LONG32=System.Int32

PCHAR=System.Char[]

PWORD=System.Int16[]

BOOLEAN=System.Int32

HGLOBAL=System.IntPtr

LONG64=System.Int64

PCSTR=System.String

PWSTR=System.String

BYTE=System.UInt16

HHOOK=System.IntPtr

LONGLONG=System.Int64

PCTSTR=System.String

REGSAM=System.UInt32

CHAR=System.Int16

HICON=System.IntPtr

LPARAM=System.IntPtr

PCWCH=System.UInt32

SC_HANDLE=System.IntPtr

COLORREF=System.UInt32

HIMAGELIST=System.IntPtr

LPBOOL=System.Int16[]

PCWSTR=System.UInt32

SC_LOCK=System.IntPtr

DWORD=System.UInt32

HIMC=System.IntPtr

LPBYTE=System.UInt16[]

PDWORD=System.Int32[]

SHORT=System.Int16

DWORD32=System.UInt32

HINSTANCE=System.IntPtr

LPCOLORREF=System.UInt32[]

PFLOAT=System.Float[]

SIZE_T=System.UInt32

DWORD64=System.UInt64

HKEY=System.IntPtr

LPCSTR=System.String

PHANDLE=System.UInt32

SSIZE_=System.UInt32

FLOAT=System.Float

HLOCAL=System.IntPtr

LPCTSTR=System.String

PHKEY=System.UInt32

TBYTE=System.Char

HACCEL=System.IntPtr

HMENU=System.IntPtr

LPCVOID=System.UInt32

PINT=System.Int32[]

TCHAR=System.Char

HANDLE=System.IntPtr

HMETAFILE=System.IntPtr

LPCWSTR=System.String

PLCID=System.UInt32

UCHAR=System.Byte

HBITMAP=System.IntPtr

HMODULE=System.IntPtr

LPDWORD=System.UInt32[]

PLONG=System.Int32[]

UINT=System.UInt32

HBRUSH=System.IntPtr

HMONITOR=System.IntPtr

LPHANDLE=System.UInt32

PLUID=System.UInt32

UINT32=System.UInt32

HCONV=System.IntPtr

HPALETTE=System.IntPtr

LPINT=System.Int32[]

PSHORT=System.Int16[]

UINT64=System.UInt64

HCONVLIST=System.IntPtr

HPEN=System.IntPtr

LPLONG=System.Int32[]

PSTR=System.String

ULONG=System.UInt32

HCURSOR=System.IntPtr

HRGN=System.IntPtr

LPSTR=System.String

PTBYTE=System.Char[]

ULONG32=System.UInt32

HDC=System.IntPtr

HRSRC=System.IntPtr

LPTSTR=System.String

PTCHAR=System.Char[]

ULONG64=System.UInt64

HDDEDATA=System.IntPtr

HSZ=System.IntPtr

LPVOID=System.UInt32

PTSTR=System.String

ULONGLONG=System.UInt64

HDESK=System.IntPtr

HWINSTA=System.IntPtr

LPWORD=System.Int32[]

PUCHAR=System.Char[]

USHORT=System.UInt16

HDROP=System.IntPtr

HWND=System.IntPtr

LPWSTR=System.String

PUINT=System.UInt32[]

WORD=System.UInt16

HDWP=System.IntPtr

INT=System.Int32

LRESULT=System.IntPtr

PULONG=System.UInt32[]

WPARAM=System.IntPtr

HENHMETAFILE=System.IntPtr

INT32=System.Int32

PBOOL=System.Int16[]

PUSHORT=System.UInt16[]

 

HFILE=System.IntPtr

INT64=System.Int64

PBOOLEAN=System.Int16[]

PVOID=System.UInt32

 

HFONT=System.IntPtr

LONG=System.Int32

PBYTE=System.UInt16[]

PWCHAR=System.Char[]

 

 

An example:

C# Method

IDL Equivalent

Calling semantics in C#

public void Method(String strInput);

HRESULT Method([in] BSTR strInput);

obj.Method("Hello There");

public String Method();

HRESULT Method([out, retval] BSTR* pRetVal);

String strOutput = obj.Method();

public String Method(ref String strPassAndModify);

HRESULT Method([in, out] BSTR* strPassAndModify, [out, retval] BSTR* pRetVal);

String strHello = "Hello There";

String strOutput = obj.Method(ref strHello);

public String Method(out String strReturn);

HRESULT Method([out] BSTR* strReturn, [out, retval] BSTR* pRetVal);

//Need not initialize strHello

String strHello;

String strOutput = obj.Method(out strHello);

public String Method(String strFirst, out String strSecond, ref String strThird);

HRESULT Method([in] BSTR bstrFirst, [out] BSTR* strSecond, [in, out] BSTR* strThird, [out, retval] BSTR* pRetVal);

String strFirst = "Hi There";

String strSecond;

String strThird = "Hello World";

String strOutput = obj.Method(strFirst,out strSecond, ref strThird);

 

COM Data Types From Microsoft

 

Pasted from <http://msdn.microsoft.com/en-us/library/sak564ww(v=VS.71).aspx>

 

COM value type

COM reference type

System type

bool

bool *

System.Int32

char, small

char *, small *

System.SByte

short

short *

System.Int16

long, int

long *, int *

System.Int32

Hyper

hyper *

System.Int64

unsigned char, byte

unsigned char *, byte *

System.Byte

wchar_t, unsigned short

wchar_t *, unsigned short *

System.UInt16

unsigned long, unsigned int

unsigned long *, unsigned int *

System.UInt32

unsigned hyper

unsigned hyper *

System.UInt64

float

float *

System.Single

double

double *

System.Double

VARIANT_BOOL

VARIANT_BOOL *

System.Boolean

void *

void **

System.IntPtr

HRESULT

HRESULT *

System.Int16 or System.IntPtr

SCODE

SCODE *

System.Int32

BSTR

BSTR *

System.String

LPSTR or [string, ...] char *

LPSTR *

System.String

LPWSTR or [string, ...] wchar_t *

LPWSTR *

System.String

VARIANT

VARIANT *

System.Object

DECIMAL

DECIMAL *

System.Decimal

DATE

DATE *

System.DateTime

GUID

GUID *

System.Guid

CURRENCY

CURRENCY *

System.Decimal

IUnknown *

IUnknown **

System.Object

IDispatch *

IDispatch **

System.Object

SAFEARRAY(type)

SAFEARRAY(type) *

type[]

The following table lists COM value and reference types that convert to corresponding element types. For example, a COM coclass automatically maps to a managed class with the same name.

COM value type

COM reference type

Element type

typedef BaseType MyType

ByRef BaseType

BaseType

MyStruct

ByRef VALUETYPE<MyStruct>

valuetype<MyStruct>

MyEnum

ByRef VALUETYPE<MyEnum>

valuetype<MyEnum>

MyInterface *

ByRef CLASS <MyInterface>

Class <MyInterface>

MyCoClass

ByRef CLASS <_Class>

class <_Class>

 

Pasted from <http://msdn.microsoft.com/en-us/library/sak564ww(v=VS.71).aspx>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值