Mapping type between C++ and C#

简介:   API Data Type Type Description C# Type API Data Type Type Description C# Type WORD ...

 

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>

 

目录
打赏
0
0
0
0
20
分享
相关文章
|
10月前
|
【C/C++ 泛型编程 应用篇】C++ 如何通过Type traits 判断 Lambda表达式类型?
【C/C++ 泛型编程 应用篇】C++ 如何通过Type traits 判断 Lambda表达式类型?
168 4
|
10月前
|
【C/C++ 泛型编程 应用篇】C++ 如何通过Type traits处理弱枚举和强枚举
【C/C++ 泛型编程 应用篇】C++ 如何通过Type traits处理弱枚举和强枚举
180 3
如何使用C#和C++结构体实现Socket通信
如何使用C#和C++结构体实现Socket通信
401 0
物联网开发中C、C++和C#哪个更好用
在物联网(IoT)开发中,C、C++和C#各有优缺点,适用场景不同。C语言性能高、资源占用低,适合内存和计算能力有限的嵌入式系统,但开发复杂度高,易出错。C++支持面向对象编程,性能优秀,适用于复杂应用,但学习曲线陡峭,编译时间长。C#易于学习,与.NET框架结合紧密,适合快速开发Windows应用,但性能略低,平台支持有限。选择语言需根据具体项目需求、复杂性和团队技术栈综合考虑。
【C/C++ 泛型编程 进阶篇 Type traits 】C++类型特征探究:编译时类型判断的艺术
【C/C++ 泛型编程 进阶篇 Type traits 】C++类型特征探究:编译时类型判断的艺术
687 1
|
9月前
|
编程语言C#、C++、Java、Python、go 选择哪个好?
我想说的是,不论选择哪种编程语言,决定选择的都是你最终的目的,做选择之前,先充分调研每一个选择项,再做选择思路就会非常清晰了。
183 3
lpszBlogName C#开发多年中途被迫改行C++但工作中又经常偷偷使用C#的C++程序员
通过AUMID解析出packageFamily,再根据PackageManager解析出安装目录 PackageManager是WinRT的类型,如何在c++中使用WinRT,请参考C++/WinRT 以下代码需要管理员权限才能运行。
浅谈c和c++和c#之间的关系
浅谈c和c++和c#之间的关系
163 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等