范例为考勤系统中调用jbc2900的dll;
原函数说明: extern "C" __declspec(dllimport ) __stdcall
long Open_ComX(char * ComPort,long Baud);
功能:打开串口
参数: ComPort
端口号 如"COM1" "COM2"
Baud 波特率 9600
返回:
0 成功 非0失败
.net中调用:[DllImport("jbc2900.dll", EntryPoint="Open_ComX",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.Cdecl)]
static extern int Open_ComX(StringBuilder ComPort,int Baud);
使用DllImportAttribute的优点:当声明了extern方法后,就可以向纯托管代码一样来调用方法,编组约定的建立也只需要一次。
有关平台调用的类型转换请查阅msdn库。