(转)C#与C++之间类型的对应
C#与C++之间类型的对应
|
Windows Data Type |
.NET Data Type |
|
BOOL, BOOLEAN |
Boolean or Int32 |
|
BSTR |
String |
|
BYTE |
Byte |
|
CHAR |
Char |
|
DOUBLE |
Double |
|
DWORD |
Int32 or UInt32 |
|
FLOAT |
Single |
|
HANDLE (and all other handle types, such as HFONT and HMENU) |
IntPtr, UintPtr or HandleRef |
|
HRESULT |
Int32 or UInt32 |
|
INT |
Int32 |
|
LANGID |
Int16 or UInt16 |
|
LCID |
Int32 or UInt32 |
|
LONG |
Int32 |
|
LPARAM |
IntPtr, UintPtr or Object |
|
LPCSTR |
String |
|
LPCTSTR |
String |
|
LPCWSTR |
String |
|
LPSTR |
String or StringBuilder* |
|
LPTSTR |
String or StringBuilder |
|
LPWSTR |
String or StringBuilder |
|
LPVOID |
IntPtr, UintPtr or Object |
|
LRESULT |
IntPtr |
|
SAFEARRAY |
.NET array type |
|
SHORT |
Int16 |
|
TCHAR |
Char |
|
UCHAR |
SByte |
|
UINT |
Int32 or UInt32 |
|
ULONG |
Int32 or UInt32 |
|
VARIANT |
Object |
|
VARIANT_BOOL |
Boolean |
|
WCHAR |
Char |
|
WORD |
Int16 or UInt16 |
|
WPARAM |
IntPtr, UintPtr or Object |
另: 在进行string转换时,需要加入前缀[MarshalAs(UnmanagedType.LPStr)]lpdword 对应于 ref int
|
C/C++ |
C# |
|
HANDLE, LPDWORD, LPVOID, void* |
IntPtr |
|
LPCTSTR, LPCTSTR, LPSTR, char*, const char*, Wchar_t*, LPWSTR |
String [in], StringBuilder [in, out] |
|
DWORD, unsigned long, Ulong |
UInt32, [MarshalAs(UnmanagedType.U4)] |
|
bool |
bool |
|
LP<struct> |
[In] ref <struct> |
|
SIZE_T |
uint |
|
LPDWORD |
out uint |
|
LPTSTR |
[Out] StringBuilder |
|
PULARGE_INTEGER |
out ulong |
|
WORD |
uInt16 |
|
Byte, unsigned char |
byte |
|
Short |
Int16 |
|
Long, int |
Int32 |
|
float |
single |
|
double |
double |
|
NULL pointer |
IntPtr.Zero |
|
Uint |
Uint32 |
习惯用C#写东西,但平时又会碰到很多要用win32 API的地方,所以经常要用DllImport,但win32函数的类型写法是很庞杂的,相信为之困扰的不止我一个,现在我整理一份我个人的理解如下,希望高人不吝赐教。
我的基本原则有如下几点:
1、下面都是针对32位系统的,所以int是32位.long也是32位;
2、各种句柄类的(H开头),我认为一律是System.IntPtr,到目前为止没发现出错;如果哪位在使用中出错,请指出;
3、LP和P,我实在不懂(对C 不太了解),对于LP和P开头的函数,如果是和STR有关的,一律写为System.String,像PLCID这样指向什么东西的,写为System.UInt32(因为指向另一个地址,那就是指针,指针是32位吧),int之类的数值型,那我就写为int[],以方便.net程序引用(写成System.UInt32的话,给API调用应该也不会出错,但.net程序就不好引用了)
如有意见,欢迎指教。谢谢
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.
|
Wtypes.h 中的非托管类型 |
非托管 C 语言类型 |
托管类名 |
说明 |
|
HANDLE |
void* |
System.IntPtr |
在 32 位 Windows 操作系统上为 32 位,在 64 位 Windows 操作系统上为 64 位。 |
|
BYTE |
unsigned char |
System.Byte |
8 位 |
|
SHORT |
short |
System.Int16 |
16 位 |
|
WORD |
unsigned short |
System.UInt16 |
16 位 |
|
INT |
int |
System.Int32 |
32 位 |
|
UINT |
unsigned int |
System.UInt32 |
32 位 |
|
LONG |
long |
System.Int32 |
32 位 |
|
BOOL |
long |
System.Int32 |
32 位 |
|
DWORD |
unsigned long |
System.UInt32 |
32 位 |
|
ULONG |
unsigned long |
System.UInt32 |
32 位 |
|
CHAR |
char |
System.Char |
用 ANSI 修饰。 |
|
LPSTR |
char* |
System.String 或 System.Text.StringBuilder |
用 ANSI 修饰。 |
|
LPCSTR |
Const char* |
System.String 或 System.Text.StringBuilder |
用 ANSI 修饰。 |
|
LPWSTR |
wchar_t* |
System.String 或 System.Text.StringBuilder |
用 Unicode 修饰。 |
|
LPCWSTR |
Const wchar_t* |
System.String 或 System.Text.StringBuilder |
用 Unicode 修饰。 |
|
FLOAT |
Float |
System.Single |
32 位 |
|
DOUBLE |
Double |
System.Double |
64 位 |
C++ dll 类型与 C#类型对应关系
[DllImport("devwdm.dll")]
public static extern int devwdm_GetImageBuffer(IntPtr pImageMem);
于是报错:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
IntPtr ptrImage = Marshal.AllocHGlobal(m_strWideth*m_strHeight*);
2,构建文件名,szFile是用户输入的字符串?
string filename = "XXX";
IntPtr ptrFileName = Marshal.AllocHGlobal(filename.Length+);
Marshal.Copy(s.ToCharArray(), , ptrFileName, s.Length);
3,获取图像数据:
devwdm_GetImageBuffer(ptrImage);
4,保存BMP
CT_SaveBmp(ptrFileName,ptrImage,m_strWideth,m_strHeight,);
托管数组向非托管代码封送:
devwdm_GetImageBuffer([In, MarshalAs( UnmanagedType.LPArray)] data);
或者手工转换成非托管数组:
IntPtr ptr = Marshal.AllocHGlobal(data.Length);//申请非托管内存块(与data大小一样)
Marshal.Copy(data,,ptr,data.Length);//将托管数据复制到非托管数据
devwdm_GetImageBuffer(ptr);//直接以非托管内存块地址为参数
Marshal.FreeHGlobal(ptr);//处理完后记得释放内存
发生错误的原因是devwdm_GetImageBuffer的参数的指针没有正确指到数据内存块,当指向受保护的系统内存块并且发生读写时,就会提示上述错误,与内存大小一点关系没有
(转)C#与C++之间类型的对应的更多相关文章
- Go语言string,int,int64 ,float之间类型转换方法
(1)int转string ? 1 2 s := strconv.Itoa(i) 等价于s := strconv.FormatInt(int64(i), 10) (2)int64转string ? 1 ...
- C/C++与C#之间类型的对应
最近在研究pos打印机相关功能, 调用winapi以及跨进程通信等,都涉及到类型之间的转换. C/C++ C# HANDLE, LPDWORD, LPVOID, void* IntPtr LPCTST ...
- C#与C++之间类型的对应
Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR Char DOUB ...
- C#与C++之间类型的对应{转}
Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR ...
- [转]Go语言string,int,int64 ,float之间类型转换方法
1 正文 (1)int转string s := strconv.Itoa(i) 等价于s := strconv.FormatInt(int64(i), 10) (2)int64转string i := ...
- C#与C++之间类型对应关系
//C++中的DLL函数原型为 //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsigned c ...
- 【opencv基础】opencv和dlib库中rectangle类型之间的转换
前言 最近使用dlib库的同时也会用到opencv,特别是由于对dlib库的画图函数不熟悉,都想着转换到opencv进行show.本文介绍一下两种开源库中rectangle类型之间的转换. 类型说明 ...
- Oracle中的数据类型和数据类型之间的转换
Oracle中的数据类型 /* ORACLE 中的数据类型: char 长度固定 范围:1-2000 VARCHAR2 长度可变 范围:1-4000 LONG 长度可变 最大的范围2gb 长字符类型 ...
- 浩哥解析MyBatis源码(十)——Type类型模块之类型处理器
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6715063.html 1.回顾 之前的两篇分别解析了类型别名注册器和类型处理器注册器,此二 ...
随机推荐
- 【思路题】【多校第一场】【1001.OO’s Sequence】
题目大意: 给你一个序列A,f(l,r) 表示 在[l,r]中 的Ai 对于每一个数Aj 都有 Ai%Aj!=0 的数目( i!=j ) 卡了一段时间..... 题解 简单题 定义两个数组L[i ...
- 教训:TOJ[4081] God Le wants to know the directory
以前的字符串题本来就弱..2年不写就更弱了.嗯.留作教训 God Le is the most talented ACMer in the TJU-ACM team. When he wants to ...
- uploadify按钮中文乱码问题
uploadify是一款基于jQuery库的上传插件,但很可惜的是无论你怎么设置参数buttonText ,它的中文按钮都会出现乱码的情况,现把出现原因及解决方法总结如下. 那么出现这种的 ...
- jqGrid插件的重载表格的解决方案
jqGrid插件的重载表格的解决方案 $("#table_list_1").empty();// 清空表格内容 var parent=$("#gbox_table_lis ...
- ASP.NET 开发框架汇总
先简单记录一下,以后慢慢添加 1.ASP.NET Aries 2.ASP.NET DevExpress
- <runtime> 的 <assemblyBinding> 元素
一.<assemblyBinding> 元素 包含有关程序集版本重定向和程序集位置的信息. <assemblyBinding xmlns="urn:schemas-micr ...
- js取整数、取余数的方法
1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.f ...
- HNOI2004 宠物收养所 解题报告
首先读完这题第一印象,是个裸题,很高兴.其次在打完代码之后,第二印象,很恶心,Treap的代码太长了,我今天下午敲了三遍,手都麻了. 废话不多说,正题.其实这个题不难,有几个点是很好的,首先,他的a值 ...
- Java三大特征之继承(二)
在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句 ...
- 快速解决js开发下拉框中blur与click冲突
在开发中我们会经常遇到blur和click冲突的情况.下面叙述了开发中常遇到的"下拉框"的问题,并提供了两种解决方案. 一.blur和click事件简述 blur事件:当元素失去焦 ...