1. 错误现象或描述
采用仪表编程一直采用自己的驱动和DLL文件,在Visual Studio 2022版本采用自己的类库文件出现下面错误:
System.Runtime.InteropServices.COMException HResult=0x80040011 Message=HRESULT = 80040011 Source=<无法计算异常源> StackTrace:<无法计算异常堆栈跟踪>
2. 原因分析
网上搜索了很多ComException的相关误差,基本的原因就是Window10安全性增加需要对DLL类库文件进行注册。
具体的操作请参考:
模块已加载,但找不到入口点DLLRegisterServer_longHARDEN的博客-CSDN博客
但是,经过上面一番操作还是不行。总是出现DLLRegisterServer找不到入口,所以又对该问题一顿操作。可以参考下面:
How to Fix “DLLRegisterserver Was Not Found” Error on Windows 10
经过上面所有操作后还是没有解决问题。重新对思路进行了梳理:利用了外部DLL文件——>Windows安全注册——>利用Windows自带的COM类重新创建DLL类库。问题解决。
3. 具体作法
在引用到搜索VISA COM库进行添加,直接using。要注意编译生成时所采用的.net架构,CPU设置必须统一。
4. 最后测试结果
生成创建后的DLL类库引用测试结果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyDrive;
namespace MyDriveTest
{
internal class Program
{
static InstrumentDriver instr = new InstrumentDriver();
static string visaAddress = "TCPIP0::10.**.**.**::inst0::INSTR";
static int timeOut = 10000;
static void Main(string[] args)
{
instr.ConnectInstrument(visaAddress, timeOut);
string idn = instr.GetIDN();
Console.WriteLine("The instrument ID is '{0}'", idn);
Console.ReadLine();
}
}
}