日期:2014-05-17  浏览次数:20806 次

句柄 HANDLE 该如何处理?用IntPtr如何替换?
近日,要导入别人编的DLL文件,里面有两个函数用到串口句柄 HANDLE。

26:打开串口
extern "C"   HANDLE __declspec(dllexport) OpenComPort(char * com)//设置COM
参数1:Com 串口号:Com=”COM1”;返回串口句柄。
27:关闭串口
extern "C" bool __declspec(dllexport) CloseComPort(HANDLE hComm)//关闭COM

把其改成C#,如下:
[DllImport("YSDKIO.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr _OpenComPort(string com);//打开COM
[DllImport("YSDKIO.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool _CloseComPort(IntPtr hComm);//关闭COM

在调用时,如下:
IntPtr intptrcom = YSDKIO._OpenComPort(duankouhao);
bool successclosecom = YSDKIO._CloseComPort(intptrcom);

不知如此处理是否正确?
句柄? HANDLE ?IntPtr

------解决方案--------------------
其实用int就可以了。