项目中需要与com端口进行通信,第三方的DLL里有一个方法,原型如下:
public extern static int OpenComm(ref IntPtr hCom, ref byte com_port);
第一个参数是com的句柄,第二个参数是com端口的名称。如果我想与com1端口进行通信应该如何给两个参数赋值?
public extern static int OpenComm(ref IntPtr hCom, ref byte com_port);
第一个参数是com的句柄,第二个参数是com端口的名称。如果我想与com1端口进行通信应该如何给两个参数赋值?
程序集:System(在 system.dll 中)
这是原型?
第三方的DLL是.net写的?
调用这个函数的话:
IntPtr hCom;
byte cport = 1;
int Result = OpenComm(ref hCom, ref cport);
它的定义是这样的
class IcDev_ECP
{
const string sDllFile = "EPCDemoDll.dll"; //extern "C" __declspec(dllexport) int OpenComm(HANDLE *hCom,char *com_port);
[DllImport(sDllFile)]
public extern static int OpenComm(ref IntPtr hCom, ref byte com_port); //extern "C" __declspec(dllexport) void CloseComm(HANDLE comm);
[DllImport(sDllFile)]
public extern static void CloseComm(IntPtr comm);
......
}
注释掉的部分好像是在c++下调用的方法。
不过 我怀疑你的OpenComm是否与原型一致,你最好贴上C++原型
这样就可以CreateFile得到句柄。
可以它给的C#的定义是:public extern static int OpenComm(ref IntPtr hCom, ref byte com_port);
就不知道怎么用了
public extern static int OpenComm(ref IntPtr hCom, StringBuilder com_port);
StringBuilder strB = new StringBuilder("COM1"); HANDLE h = CreateFile("COM1",............);
OpenComm(ref h, strB);
试试