c#或调用c中的dll函数

在c、c++中有typedef定义。当要把c/c++中的程序移植到c#或调用c中的dll函数时,涉及到一些数据结构的重新定义。为了方便,可使用c#中的using 别名;对于指针,需加unsafe
如:

using boolean_T = System.Byte;

unsafe public struct emxArray_real_T
{
double* data;
int* size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
}

C#调用DLL文件时参数对应表(每一行的(1)(2)(3)(4)与列名前序号相符合)

(1)Wtypes.h中的非托管类型 (2)非托管 C语言类型(3)托管类名 (4)说明
(1)HANDLE (2)void* (3) System.IntPtr (4)32 位
(1)BYTE (2) unsigned char (3)System.Byte (4)8 位
(1)SHORT (2)short (3)System.Int16 (4)16 位
(1)WORD (2) unsigned short (3)System.UInt16 (4)16 位
(1)INT (2)int (3) System.Int32 (4) 32 位
(1)UINT (2)unsigned int (3)System.UInt32 (4)32 位
(1)LONG (2) long (3) System.Int32 (4) 32 位
(1)BOOL (2) long (3)System.Int32 (4)32 位
(1)DWORD (2) unsigned long (3) System.UInt32 (4)32 位
(1)ULONG (2) unsigned long (3) System.UInt32 (4) 32 位
(1)CHAR (2) char (3) System.Char (4) 用 ANSI 修饰。
(1)LPSTR (2) char* (3)System.String 或 System.StringBuilder (4)用 ANSI 修饰。
(1)LPCSTR (2) Const char* (3)System.String 或System.StringBuilder (4)用 ANSI 修饰。
(1)LPWSTR (2) wchar_t* (3)System.String 或System.StringBuilder (4)用 Unicode 修饰。
(1)LPCWSTR(2) Const wchar_t* (3)System.String 或System.StringBuilder (4)用 Unicode 修饰。
(1)FLOAT (2) Float (3)System.Single (4)32 位
(1)DOUBLE (2) Double (3)System.Double (4)64 位

猜你喜欢

转载自blog.csdn.net/qq_25528267/article/details/87916092