C# 调用C++ DLL 的类型转换(转载版)
最近在做视频监控相关的demo开发,实现语言是C#,但视频监控的SDK是C++开发的,所以涉及到C#调用C++的dll库。很多结构体、参数在使用时都要先进行转换,由非托管类型转换成托管类型后才能使用。以下是查到的比较好用的类型转换总结。
转载地址:http://www.cnblogs.com/blackice/archive/2013/05/23/3094653.html
- //C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试
- //c++:HANDLE(void *) ---- c#:System.IntPtr
- //c++:Byte(unsigned char) ---- c#:System.Byte
- //c++:SHORT(short) ---- c#:System.Int16
- //c++:WORD(unsigned short) ---- c#:System.UInt16
- //c++:INT(int) ---- c#:System.Int16
- //c++:INT(int) ---- c#:System.Int32
- //c++:UINT(unsigned int) ---- c#:System.UInt16
- //c++:UINT(unsigned int) ---- c#:System.UInt32
- //c++:LONG(long) ---- c#:System.Int32
- //c++:ULONG(unsigned long) ---- c#:System.UInt32
- //c++:DWORD(unsigned long) ---- c#:System.UInt32
- //c++:DECIMAL ---- c#:System.Decimal
- //c++:BOOL(long) ---- c#:System.Boolean
- //c++:CHAR(char) ---- c#:System.Char
- //c++:LPSTR(char *) ---- c#:System.String
- //c++:LPWSTR(wchar_t *) ---- c#:System.String
- //c++:LPCSTR(const char *) ---- c#:System.String
- //c++:LPCWSTR(const wchar_t *) ---- c#:System.String
- //c++:PCAHR(char *) ---- c#:System.String
- //c++:BSTR ---- c#:System.String
- //c++:FLOAT(float) ---- c#:System.Single
- //c++:DOUBLE(double) ---- c#:System.Double
- //c++:VARIANT ---- c#:System.Object
- //c++:PBYTE(byte *) ---- c#:System.Byte[]
- //c++:BSTR ---- c#:StringBuilder
- //c++:LPCTSTR ---- c#:StringBuilder
- //c++:LPCTSTR ---- c#:string
- //c++:LPTSTR ---- c#:[MarshalAs(UnmanagedType.LPTStr)] string
- //c++:LPTSTR 输出变量名 ---- c#:StringBuilder 输出变量名
- //c++:LPCWSTR ---- c#:IntPtr
- //c++:BOOL ---- c#:bool
- //c++:HMODULE ---- c#:IntPtr
- //c++:HINSTANCE ---- c#:IntPtr
- //c++:结构体 ---- c#:public struct 结构体{};
- //c++:结构体 **变量名 ---- c#:out 变量名 //C#中提前申明一个结构体实例化后的变量名
- //c++:结构体 &变量名 ---- c#:ref 结构体 变量名
- //c++:WORD ---- c#:ushort
- //c++:DWORD ---- c#:uint
- //c++:DWORD ---- c#:int
- //c++:UCHAR ---- c#:int
- //c++:UCHAR ---- c#:byte
- //c++:UCHAR* ---- c#:string
- //c++:UCHAR* ---- c#:IntPtr
- //c++:GUID ---- c#:Guid
- //c++:Handle ---- c#:IntPtr
- //c++:HWND ---- c#:IntPtr
- //c++:DWORD ---- c#:int
- //c++:COLORREF ---- c#:uint
- //c++:unsigned char ---- c#:byte
- //c++:unsigned char * ---- c#:ref byte
- //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)] byte[]
- //c++:unsigned char * ---- c#:[MarshalAs(UnmanagedType.LPArray)] Intptr
- //c++:unsigned char & ---- c#:ref byte
- //c++:unsigned char 变量名 ---- c#:byte 变量名
- //c++:unsigned short 变量名 ---- c#:ushort 变量名
- //c++:unsigned int 变量名 ---- c#:uint 变量名
- //c++:unsigned long 变量名 ---- c#:ulong 变量名
- //c++:char 变量名 ---- c#:byte 变量名 //C++中一个字符用一个字节表示,C#中一个字符用两个字节表示
- //c++:char 数组名[数组大小] ---- c#:MarshalAs(UnmanagedType.ByValTStr, SizeConst = 数组大小)] public string 数组名; ushort
- //c++:char * ---- c#:string //传入参数
- //c++:char * ---- c#:StringBuilder//传出参数
- //c++:char *变量名 ---- c#:ref string 变量名
- //c++:char *输入变量名 ---- c#:string 输入变量名
- //c++:char *输出变量名 ---- c#:[MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名
- //c++:char ** ---- c#:string
- //c++:char **变量名 ---- c#:ref string 变量名
- //c++:const char * ---- c#:string
- //c++:char[] ---- c#:string
- //c++:char 变量名[数组大小] ---- c#:[MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)] public string 变量名;
- //c++:struct 结构体名 *变量名 ---- c#:ref 结构体名 变量名
- //c++:委托 变量名 ---- c#:委托 变量名
- //c++:int ---- c#:int
- //c++:int ---- c#:ref int
- //c++:int & ---- c#:ref int
- //c++:int * ---- c#:ref int //C#中调用前需定义int 变量名 = 0;
- //c++:*int ---- c#:IntPtr
- //c++:int32 PIPTR * ---- c#:int32[]
- //c++:float PIPTR * ---- c#:float[]
- //c++:double** 数组名 ---- c#:ref double 数组名
- //c++:double*[] 数组名 ---- c#:ref double 数组名
- //c++:long ---- c#:int
- //c++:ulong ---- c#:int
- //c++:UINT8 * ---- c#:ref byte //C#中调用前需定义byte 变量名 = new byte();
- //c++:handle ---- c#:IntPtr
- //c++:hwnd ---- c#:IntPtr
- //c++:void * ---- c#:IntPtr
- //c++:void * user_obj_param ---- c#:IntPtr user_obj_param
- //c++:void * 对象名称 ---- c#:([MarshalAs(UnmanagedType.AsAny)]Object 对象名称
- //c++:char, INT8, SBYTE, CHAR ---- c#:System.SByte
- //c++:short, short int, INT16, SHORT ---- c#:System.Int16
- //c++:int, long, long int, INT32, LONG32, BOOL , INT ---- c#:System.Int32
- //c++:__int64, INT64, LONGLONG ---- c#:System.Int64
- //c++:unsigned char, UINT8, UCHAR , BYTE ---- c#:System.Byte
- //c++:unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t ---- c#:System.UInt16
- //c++:unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT ---- c#:System.UInt32
- //c++:unsigned __int64, UINT64, DWORDLONG, ULONGLONG ---- c#:System.UInt64
- //c++:float, FLOAT ---- c#:System.Single
- //c++:double, long double, DOUBLE ---- c#:System.Double
- //Win32 Types ---- CLR Type
- //Struct需要在C#里重新定义一个Struct
- //CallBack回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str);
- //unsigned char** ppImage替换成IntPtr ppImage
- //int& nWidth替换成ref int nWidth
- //int*, int&, 则都可用 ref int 对应
- //双针指类型参数,可以用 ref IntPtr
- //函数指针使用c++: typedef double (*fun_type1)(double); 对应 c#:public delegate double fun_type1(double);
- //char* 的操作c++: char*; 对应 c#:StringBuilder;
- //c#中使用指针:在需要使用指针的地方 加 unsafe
- //unsigned char对应public byte
- /*
- * typedef void (*CALLBACKFUN1W)(wchar_t*, void* pArg);
- * typedef void (*CALLBACKFUN1A)(char*, void* pArg);
- * bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg);
- * 调用方式为
- * [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- * public delegate void CallbackFunc1([MarshalAs(UnmanagedType.LPWStr)] StringBuilder strName, IntPtr pArg);
- *
- *
- */
C# 调用C++ DLL 的类型转换(转载版)的更多相关文章
- [转载] C# 调用C++ DLL 的类型转换
//C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试 //c++:HANDLE(void *) ---- c#:System.IntPtr //c++:Byt ...
- C# 调用C++ DLL 的类型转换
//C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试 //c++:HANDLE(void *) ---- c#:System.IntPtr //c++:Byt ...
- C# 调用 C++ Dll 类型转换的方式 全
摘要:C#引用C++ Dll 所有类型转换的方式 //C++中的DLL函数原型为 //extern "C" __declspec(dllexport ...
- Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)
文章目录: 1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...
- [JNI] Java 调用 C++ dll
首先介绍一下JNI吧! JNI 是Java提供的一个用于调用本地接口的接口层,位于Java代码 和 本地代码之间的一层:主要功能是 数据类型的转换,还有就是通过这一层来调用本地代码! 下面就说说Jav ...
- [转]C#调用C++dll
本文转载至http://www.cnblogs.com/ysharp/archive/2012/05/25/2517803.html 在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问 ...
- c# 调用 C++ dll 传入传出 字符串
c# 调用 C++ dll 传入传出 字符串 2013-07-02 09:30 7898人阅读 评论(2) 收藏 举报 本文章已收录于: 分类: windows 版权声明:随便转载,随便使用. C ...
- C# 调用第三方DLL完整实例
C# 调用第三方DLL完整实例 分类: C/C++ 以下代码为本人在实际项目中编写的调用第三方DLL接口程序的完整代码. public class ExecuteDLL : Form { ...//忽 ...
- 非托管C++通过C++/CLI包装调用C# DLL
项目中要给其它客户程序提供DLL做为接口,该项目是在.Net4.0平台下开发.终所周知.Net的各个版本之间存在着兼容性的问题,但是为了使用高版本运行平台的新特性,又不得不兼顾其它低版本平台客户程序的 ...
随机推荐
- Spark Shuffle(一)ShuffleWrite:Executor如何将Shuffle的结果进行归并写到数据文件中去(转载)
转载自:https://blog.csdn.net/raintungli/article/details/70807376 当Executor进行reduce运算的时候,生成运算结果的临时Shuffl ...
- Selenium IDE的一些操作
1.运行速度过快时,可能出现找不到元素的情况,影响运行结果,将速度调慢慢一些,就可以运行成功. 如果为其他情况找不到元素,则需要另外找原因,有可能元素定位有问题,有可能无该元素. 2.导出录制的脚本为 ...
- 基于struts2框架-自定义身份证号验证器
自定义拦截器的步骤: 1.定义一个验证器的类: > 自定义的验证器都需要实现 Validator接口. > 可以选择继承 ValidatorSupport 或 FieldValidato ...
- VS Code 终端窗口无法输入命令的解决方案
问题 今天打开vs code,打开终端窗口,发现不能输入命令了 解决方法 邮件桌面 vscode的快捷键,打开“兼容性”标签,勾选"以管理员身份运行此程序" 结果 修改之后重启vs ...
- mongodb安装及副本集搭建
mongodb下载地址:https://www.mongodb.com/dr/fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.7.tg ...
- 5.1 Components — Introduction
1. HTML被设计的时候,浏览器是一个简单的文件浏览器.开发构建大的Web应用程序需要更多的东西. 2. 不是试图取代HTML,然而,Ember.js拥抱它,然后增加了许多新功能使得构建web应用程 ...
- chrome 关闭安全模式
chrome.exe --disable-web-security --user-data-dir
- 团队 作业6--展示(alpha阶段)
团队作业6--展示博客(alpha阶段) 一.团队信息 团队码云地址: https://gitee.com/kezhiqing/soft_team_blog 成员介绍: 个人博客地址 团队成员 个人博 ...
- 2016ACM/ICPC亚洲区沈阳站 Solution
A - Thickest Burger 水. #include <bits/stdc++.h> using namespace std; int t; int a, b; int main ...
- ubuntu常用命令解释
1.seq 用于产生一个整数到另一个整数之间所有的整数,如:seq 3 5 输出:3 4 5 注:如果参数为一个整数,则输出1至这个数之间的所有整数 2.tee [-a] file 从标准输入设备读取 ...