您的位置:首页 > 编程语言 > C语言/C++

C#与VC++数据类型对应关系

2012-06-18 12:01 323 查看
那是在C++的代码中编写,类似于:

void MyFunc(ANSI LPSTR param1)

具体的你要参考下相关文档。

到了.NET中,如果参数是只读的(输入参数),使用STRING就足够了,如果是需要有返回值的(引用、输出参数)则必须使用STRINGBUILDER。

现在有个问题,是,输入参数在C++中 是LPStr 定义的,微软官方说用 string,并用 ANSI修饰,所以,我用ANSI修饰了,但是传入参数进去,还是不行。

我用C++去调用这个dll,就没有问题。郁闷了,主要是因为参数字符编码的问题,但是 在C#中,怎么也无法转换成Ansi的编码,总是 Unicode的。

[MarshalAs(UnmanagedType.LPStr)]

[DllImport(@"e:\A.dll", CharSet = CharSet.Ansi)]
public static extern int a([MarshalAs(UnmanagedType.LPStr)] ref string contentOfLicense, [MarshalAs(UnmanagedType.LPStr)] ref StringBuilder outputBuffOfLicense, ref int lengthOfOutputBuff);

ref是引用。在被调用方修改变量的内容(应该说是修改变量指向的数据地址)。ref不要带
虽然在C++中定义的参数是指针,但在.NET中,只需要直接使用string就好(string本身就是等同于一个lpstr的)。
只是,在C++定义中,把参数定义成LPCSTR和LPSTR是不同的(一个常量,一个变量),对应的.NET中,应该使用string和StringBuilder。


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 ,函数默认为私有函数,调用就会出错。
在C#调用C++ DLL封装库时会出现两个问题:
1. 数据类型转换问题
2. 指针或地址参数传送问题
首先是数据类型转换问题。因为C#是.NET语言,利用的是.NET的基本数据类型,所以实际上是将C++的数据类型与.NET的基本数据类型进行对应。
例如C++的原有函数是:
int __stdcall FunctionName(unsigned char param1, unsigned short param2)
其中的参数数据类型在C#中,必须转为对应的数据类型。如:
[DllImport(“ COM DLL path/file ”)]
extern static int FunctionName(byte param1, ushort param2)
因为调用的是__stdcall函数,所以使用了P/Invoke的调用方法。其中的方法FunctionName必须声明为静态外部函数,即加上extern static声明头。我们可以看到,在调用的过程中,unsigned char变为了byte,unsigned short变为了ushort。变换后,参数的数据类型不变,只是声明方式必须改为.NET语言的规范。
我们可以通过下表来进行这种转换:

Win32 Types
CLR Type
char, INT8, SBYTE, CHAR
System.SByte
short, short int, INT16, SHORT
System.Int16
int, long, long int, INT32, LONG32, BOOL , INT
System.Int32
__int64, INT64, LONGLONG
System.Int64
unsigned char, UINT8, UCHAR , BYTE
System.Byte
unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t
System.UInt16
unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT
System.UInt32
unsigned __int64, UINT64, DWORDLONG, ULONGLONG
System.UInt64
float, FLOAT
System.Single
double, long double, DOUBLE
System.Double
之后再将CLR的数据类型表示方式转换为C#的表示方式。这样一来,函数的参数类型问题就可以解决了。
现在,我们再来考虑下一个问题,如果要调用的函数参数是指针或是地址变量,怎么办?
对于这种情况可以使用C#提供的非安全代码来进行解决,但是,毕竟是非托管代码,垃圾资源处理不好的话对应用程序是很不利的。所以还是使用C#提供的ref以及out修饰字比较好。
同上面一样,我们也举一个例子:
int __stdcall FunctionName(unsigned char ¶m1, unsigned char *param2)
在C#中对其进行调用的方法是:
[DllImport(“ file ”)]
extern static int FunctionName(ref byte param1, ref byte param2)
看到这,可能有人会问,&是取地址,*是传送指针,为何都只用ref就可以了呢?一种可能的解释是ref是一个具有重载特性的修饰符,会自动识别是取地址还是传送指针。
在实际的情况中,我们利用参数传递地址更多还是用在传送数组首地址上。
如:byte[] param1 = new param1(6);
在这里我们声明了一个数组,现在要将其的首地址传送过去,只要将param1数组的第一个元素用ref修饰。具体如下:
[DllImport(“ file ”)]
extern static int FunctionName(ref byte param1[1], ref byte param2)

Wtypes.h 中的非托管类型
非托管 C语言类型
托管类名
说明
HANDLE
void*
System.IntPtr
32 位
BYTE
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.StringBuilder
用 ANSI 修饰。
LPCSTR
Const char*
System.String 或System.StringBuilder
用 ANSI 修饰。
LPWSTR
wchar_t*
System.String 或System.StringBuilder
用 Unicode 修饰。
LPCWSTR
Const wchar_t*
System.String 或System.StringBuilder
用 Unicode 修饰。
FLOAT
Float
System.Single
32 位
DOUBLE
Double
System.Double
64 位
C++ C#

=====================================

WORD ushort

DWORD uint

UCHAR int/byte 大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte

UCHAR* string/IntPtr

unsigned char* [MarshalAs(UnmanagedType.LPArray)]byte[]/?(Intptr)

char* string

LPCTSTR string

LPTSTR [MarshalAs(UnmanagedType.LPTStr)] string

long int

ulong uint

Handle IntPtr

HWND IntPtr

void* IntPtr

int int

int* ref int

*int IntPtr

unsigned int uint

COLORREF uint

API与C#的数据类型对应关系表

API数据类型 类型描述 C#类型 API数据类型 类型描述 C#类型

WORD 16位无符号整数 ushort CHAR 字符 char

LONG 32位无符号整数 int DWORDLONG 64位长整数 long

DWORD 32位无符号整数 uint HDC 设备描述表句柄 int

HANDLE 句柄,32位整数 int HGDIOBJ GDI对象句柄 int

UINT 32位无符号整数 uint HINSTANCE 实例句柄 int

BOOL 32位布尔型整数 bool HWM 窗口句柄 int

LPSTR 指向字符的32位指针 string HPARAM 32位消息参数 int

LPCSTR 指向常字符的32位指针 String LPARAM 32位消息参数 int

BYTE 字节 byte WPARAM 32位消息参数 int

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.UInt16

WPARAM=System.IntPtr

<---------补充----------->

Wtypes.h 中的非托管类型 非托管C 语言类型 托管类名 说明

HANDLE void* System.IntPtr 32 位

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.StringBuilder 用 ANSI 修饰。

LPCSTR Const char* System.String 或 System.StringBuilder 用 ANSI 修饰。

LPWSTR wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。

LPCWSTR Const wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。

FLOAT Float System.Single 32 位

DOUBLE Double System.Double 64 位
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: