c#调用c++ dll的几种类型(转)
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的几种类型(转)的更多相关文章
- go 调用windows dll 的三种方法
参考:https://blog.csdn.net/qq_39584315/article/details/81287669 大部分代码参考:https://studygolang.com/articl ...
- C# 调用 C++ dll的两种方式
目录: 1.非托管方式 2.托管方式 3.介绍 extern "C" 4.介绍 DllImport 1.非托管方式 第一种,非托管方式:调用类和方法https://www.co ...
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 (转载)
C#调用C++编写的DLL函数, 以及各种类型的参数传递 1. 如果函数只有传入参数,比如: C/C++ Code Copy Code To Clipboard //C++中的输出函数 int ...
- 转【C#调用DLL的几种方法,包括C#调用C\C++\C#DLL】
C#中dll调用方法 一. DLL与应用程序 动态链接库(也称为DLL,即为“Dynamic Link Library”的缩写)是Microsoft Windows最重要的组成要素之一, ...
- C++调用DLL有两种方法——静态调用和动态调用
C++调用DLL有两种方法——静态调用和动态调用 标签: dllc++winapinullc 2011-09-09 09:49 11609人阅读 评论(0) 收藏 举报 分类: cpp(30) [ ...
- C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法
使用反射(Reflect)获取dll文件中的类型并调用方法 需引用:System.Reflection; 1. 使用反射(Reflect)获取dll文件中的类型并调用方法(入门案例) static v ...
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
XML序列化 #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...
- 调用DLL的2种方式
[调用DLL的2种方式] DLL在生成的时候会有dll.lib2个文件,另外包含相应的.h. 1.静态方式,通过lib来引用dll,以及引入.h. 2.只通过dll来使用,前提是知道内部的函数符号.
- C#调用C++ DLL动态库的两种方式
第一种方式:C++导出函数, c#dllimport 的方式 在很多地方都看到过,如[dllimport "user32.dll"]这种代码,调用windows API,就是通过这 ...
随机推荐
- python os.stat() 和 stat模块详解
stat 系统调用时用来返回相关文件的系统状态信息的. 首先我们看一下stat中有哪些属性: >>> import os >>> print os.stat(&qu ...
- ArcEngine中打开各种数据源(WorkSpace)的连接(转)
ArcEngine中打开各种数据源(WorkSpace)的连接 (SDE.personal/File.ShapeFile.CAD数据.影像图.影像数据集) ArcEngine 可以接受多种数据源.在开 ...
- Android 对话框弹出位置和透明度的设置
在Android中 我们经常会用AlertDialog来显示对话框.通过这个对话框是显示在屏幕中心的.但在某些程序中,要求对话框可以显示在不同的位置.例如,屏幕的上 方或下方.要实现这种效果.就需要获 ...
- RAPIDXML 中文手册,根据官方文档完整翻译!
简介:这个号称是最快的DOM模型XML分析器,在使用它之前我都是用TinyXML的,因为它小巧和容易上手,但真正在项目中使用时才发现如果分析一个比较大的XML时TinyXML还是表现一般,所以我们决定 ...
- bzoj3747
经典题,记录每个位置对应数下次出现的位置next[i] 每个位置维护当前左端点下到这个位置的和 随着左端点的右移一位到i+1,对[i+1,next[i]-1] 的影响是-a[i], [next[i], ...
- SharePoint的安装配置
安装环境 1. Window server 2008 r2(sp2) OS.2. MS SQL Server 2008 r2.3. Office2010.4. IIS7以上.5. 确认服务器已经加入域 ...
- css,html命名规则
css,html命名规则 页头: header 登录条: loginBar 标志: logo 侧栏: sideBar 广告: banner 导航: nav 子导航: subNav 菜单: menu 子 ...
- codevs 1197 Vigenère密码
开始做NOIP啦.. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- ti processor sdk linux am335x evm /bin/setup-host-check.sh hacking
#!/bin/sh # # ti processor sdk linux am335x evm /bin/setup-host-check.sh hacking # 说明: # 本文主要对TI的sdk ...
- LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)
题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...