C#添加引用不成功,提示:未能添加对xxx.dll的引用。请确保此文件可访问并且是一个有效的程序集或COM组件。
查了一下网上的资料,原因是需要引用Dll文件中的接口函数非.NET的dll,需要DllImport。C#添加引用需要这个dll本身是.net的程序集,或者是一个COM组件。如果是一个标准的C导出Dll,那么需要用DllImport导入函数,参考互操作内容
class EcComm
{
[DllImport("EcComm.dll", EntryPoint = "EC_OpenPort", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern uint EC_OpenPort(String port,String param );
[DllImport("EcComm.dll", EntryPoint = "EC_ClosePort", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void EC_ClosePort(uint hdl_port);
[DllImport("EcComm.dll", EntryPoint = "EC_RunCmd", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool EC_RunCmd(uint hdl_port, byte[] cmd);
}