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

c#调用c++ dll的几种类型(转)的更多相关文章

  1. go 调用windows dll 的三种方法

    参考:https://blog.csdn.net/qq_39584315/article/details/81287669 大部分代码参考:https://studygolang.com/articl ...

  2. C# 调用 C++ dll的两种方式

    目录: 1.非托管方式 2.托管方式 3.介绍 extern "C" 4.介绍   DllImport 1.非托管方式 第一种,非托管方式:调用类和方法https://www.co ...

  3. C#调用C++编写的DLL函数, 以及各种类型的参数传递 (转载)

        C#调用C++编写的DLL函数, 以及各种类型的参数传递 1. 如果函数只有传入参数,比如: C/C++ Code Copy Code To Clipboard //C++中的输出函数 int ...

  4. 转【C#调用DLL的几种方法,包括C#调用C\C++\C#DLL】

    C#中dll调用方法   一.      DLL与应用程序 动态链接库(也称为DLL,即为“Dynamic Link Library”的缩写)是Microsoft Windows最重要的组成要素之一, ...

  5. C++调用DLL有两种方法——静态调用和动态调用

    C++调用DLL有两种方法——静态调用和动态调用 标签: dllc++winapinullc 2011-09-09 09:49 11609人阅读 评论(0) 收藏 举报  分类: cpp(30)  [ ...

  6. C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法

    使用反射(Reflect)获取dll文件中的类型并调用方法 需引用:System.Reflection; 1. 使用反射(Reflect)获取dll文件中的类型并调用方法(入门案例) static v ...

  7. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  8. 调用DLL的2种方式

    [调用DLL的2种方式] DLL在生成的时候会有dll.lib2个文件,另外包含相应的.h. 1.静态方式,通过lib来引用dll,以及引入.h. 2.只通过dll来使用,前提是知道内部的函数符号.

  9. C#调用C++ DLL动态库的两种方式

    第一种方式:C++导出函数, c#dllimport 的方式 在很多地方都看到过,如[dllimport "user32.dll"]这种代码,调用windows API,就是通过这 ...

随机推荐

  1. 360云盘、百度云、微云……为什么不出 OS X(Mac 端)应用呢?(用户少,开发成本高)(百度网盘Mac版2016.10.18横空出世)

    已经说的很好了,现有的云盘所谓的 OS X 版只有云同步功能,不过 115 是个例外,不过 115 的现状……不言自明.接下来说点和本题答案无关的,其实在官方客户端流氓 + 限速的大背景下 OS X ...

  2. sonar runner 2.4

    https://www.versioneye.com/java/org.codehaus.sonar.runner:sonar-runner-dist/2.4

  3. 删除单链表的倒数第k个结点

    策略 直接遍历总数为len,再次遍历第len-k+1个就是答案,但是这样遍历了O(N+k)个,可以在O在更短的时间内找到 图示 参考代码 #include <iostream> using ...

  4. Intellij Idea 创建Web项目入门(一)

    相关软件: Intellij Idea14:http://pan.baidu.com/s/1nu16VyD JDK7:http://pan.baidu.com/s/1dEstJ5f Tomcat(ap ...

  5. libmysqlclient.so.15: cannot open shared object file: No such file or directory

    错误: ./mafsInRegion: error while loading shared libraries: libmysqlclient.so.15: cannot open shared o ...

  6. 1208. Legendary Teams Contest(dfs)

    1208 简单dfs 对于每个数 两种情况 取还是不取 #include <iostream> #include<cstdio> #include<cstring> ...

  7. bzoj2829

    裸题,直接上凸包,然后加上一个圆周即可 只是在这之前没写过旋转而已 const pi=3.14159265358979323; eps=1e-8; type point=record x,y:doub ...

  8. BZOJ2086: [Poi2010]Blocks

    题解: 想了想发现只需要求出最长的一段平均值>k即可. 平均值的问题给每个数减去k,判断是否连续的一段>0即可. 然后我们发现如果i<j 且 s[i]<s[j],那么 j 对于 ...

  9. Jquery 模板插件 jquery.tmpl.js 的使用方法(2):嵌套each循环,temp调用(使用预编译的模板缓存)

    直接上代码吧 一:主窗口 /*#region SendChooseTargetTemplate 发送候选人主窗口模板*/ var SendChooseTargetTemplate = ''; Send ...

  10. tomcat 默认项目设置

    正常情况下,我们启动tomcat后,直接输入“http://localhost:端口/“ 后,默认访问道是webapp目录下的ROOT应用. 我们要通过上述方式访问自己的应用,有俩种方式. 第一:把自 ...