基础格式转换
数据类型转换
最简单的,在C#和C/C++可以直接转换的数据格式,即数据名称上的变化。
C/C++ | C# |
---|---|
HANDLE(void *) | IntPtr |
Byte(unsigned char) | Byte |
SHORT(short) | Int16 |
WORD(unsigned short) | UInt16 |
INT(int) | Int16/Int32 |
UINT(unsigned int) | UInt16/UInt32 |
LONG(long) | Int32 |
ULONG(unsigned long) | UInt32 |
DWORD(unsigned long) | UInt32 |
DECIMAL | Decimal |
BOOL(long) | Boolean |
CHAR(char) | Char |
LPSTR(char *) | String |
LPWSTR(wchar_t *) | String |
LPCSTR(const char *) | String |
LPCWSTR(const wchar_t *) | String |
PCAHR(char *) | String |
BSTR | String |
FLOAT(float) | Single |
DOUBLE(double) | Double |
VARIANT | Object |
PBYTE(byte *) | Byte[] |
BSTR | StringBuilder |
LPCTSTR | StringBuilder |
LPCTSTR | string |
LPTSTR | [MarshalAs(UnmanagedType.LPTStr)] string |
LPTSTR 输出变量名 | StringBuilder 输出变量名 |
LPCWSTR | IntPtr |
BOOL | bool |
HMODULE | IntPtr |
HINSTANCE | IntPtr |
结构体 | public struct 结构体{} |
结构体 **变量名 | out 结构体 变量名 |
结构体 &变量名 | ref 结构体 变量名 |
WORD | ushort |
DWORD | uint/int |
UCHAR | int/byte |
UCHAR* | string/IntPtr |
GUID | Guid |
Handle | IntPtr |
HWND | IntPtr |
COLORREF | uint |
unsigned char | byte |
unsigned char * | ref byte/ [MarshalAs(UnmanagedType.LPArray)] byte[]/ [MarshalAs(UnmanagedType.LPArray)] Intptr |
unsigned char & | ref byte |
unsigned char 变量名 | byte 变量名 |
unsigned short 变量名 | ushort 变量名 |
unsigned int 变量名 | uint 变量名 |
unsigned long 变量名 | ulong 变量名 |
char 变量名 | byte 变量名 |
char 数组名[数组大小] | MarshalAs(UnmanagedType.ByValTStr, SizeConst = 数组大小)] public string 数组名 |
char * | string(传入)/StringBuilder(传出) |
char *变量名 | ref string 变量名 |
char *输入变量名 | string 输入变量名 |
char *输出变量名 | [MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名 |
char ** | string |
char **变量名 | ref string 变量名 |
const char * | string |
char[] | string |
char 变量名[数组大小] | [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 数组大小)] public string 变量名 |
struct 结构体名 *变量名 | ref 结构体名 变量名 |
委托 变量名 | 委托变量名 |
int | int/ref int |
int & | ref int |
int * | ref int |
*int | IntPtr |
int32 PIPTR * | int32[] |
float PIPTR * | float[] |
double** 数组名 | ref double 数组名 |
double*[] 数组名 | ref double 数组名 |
long | int |
ulong | int |
UINT8 * | ref byte |
void * 对象名称 | [MarshalAs(UnmanagedType.AsAny)] Object 对象名称 |
char/INT8/SBYTE/CHAR | SByte |
short/short int/INT16/SHORT | Int16 |
int/long/long int/INT32/LONG32/BOOL/INT | Int32 |
__int64/INT64/LONGLONG | Int64 |
unsigned char/UINT8/UCHAR/BYTE | Byte |
unsigned short/UINT16/USHORT/WORD/ATOM/WCHAR/ __wchar_t | UInt16 |
unsigned/unsigned int/UINT32/ULONG32/DWORD32/ULONG/ DWORD/UINT | UInt32 |
unsigned __int64/UINT64/DWORDLONG/ULONGLONG | UInt64 |
float/FLOAT | Single |
double, long double, DOUBLE | Double |
Win32 Types | CLR Type |
结构体转换
C#转换的结构体需要用LayoutKind.Sequential这个属性修饰,因为在C/C++的结构体内存是顺序布局的,所以需要C#转换的结构体内存也按照顺序布局,这样传递指针时dll内部不会出错。
例如:
C的结构体声明为
struct demobuf
C#中的结构体声明为
[StructLayout(LayoutKind.Sequential)]
public struct DemoBuf
C#转换的结构体成员需要用public修饰符,如果不添加public修饰符,C#成员变量默认是保护的,在其它方法内定义这个结构体就不能随便访问修改其成员变量。并且在C的结构体中对其内部成员变量的访问权限只能是public,C++中允许public/protected/private。
C的结构体为
struct demobuf
{
int a;
int b;
bool c;
}
C#的结构体为
[StructLayout(LayoutKind.Sequential