c#调用c++ dll的几种类型(转)

时间:2021-02-03 02:57:07

http://www.sosuo8.com/article-2012/dllleixingzhuanhuan.htm

 

在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问题,尤其是传递和返回字符串是,现总结一下,分享给大家:

VC++中主要字符串类型为:LPSTR,LPCSTR, LPCTSTR, string, CString, LPCWSTR, LPWSTR等

但转为C#类型却不完全相同。

主要有如下几种转换:

将string转为IntPtr:IntPtr System.Runtime.InteropServices.Marshal.StringToCoTaskMemAuto(string)
将IntPtr转为string:string System.Runtime.InteropServices.MarshalPtrToStringAuto(IntPtr)

类型对照:

BSTR --------- StringBuilder
LPCTSTR --------- StringBuilder
LPCWSTR --------- IntPtr
handle---------IntPtr
hwnd-----------IntPtr
char *----------string
int * -----------ref int
int &-----------ref int
void *----------IntPtr
unsigned char *-----ref byte
Struct需要在C#里重新定义一个Struct
CallBack回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str);
注意在每个函数的前面加上public static extern +返回的数据类型,如果不加public ,函数默认为私有函数,调用就会出错。

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

 
 

 
 

API

C#数据类型

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

UCHAR

System.Byte

UINT

System.UInt32

UINT32

System.UInt32

UINT64

System.UInt64

  

  

ULONG

System.UInt32

ULONG32

System.UInt32

ULONG64

System.UInt64

ULONGLONG

System.UInt64

USHORT

System.UInt16

  

  

WORD

System.Short

WPARAM

System.IntPtr