c# - Visual Studio 2019 - COM 异常:找不到成员 0x80020003
问题描述
我在 Visual Studio 2019 中使用 Interop 库作为参考,它能够执行以下代码而不会出现任何错误:
using someInterop;
private void button1_Click(object sender, EventArgs e)
{
var flexApp = new NSF.Application();
var proxyScan = flexApp.Scan;
proxyScan.StartPrescan();
do
{
Console.WriteLine("Hello");
System.Threading.Thread.Sleep(100);
}
while (proxyScan.IsScanningPrescan());
}
但是,当我声明一个类并定义属性来表示变量时(使它们成为全局变量):
public class NSFApp
{
public Application someApp { get; set; }
public ProxyScan someScan { get; set; }
public NSFApp()
{
someApp = new NSF.Application();
someScan = someApp.Scan;
}
}
然后在应用程序中使用它,它会引发 COM Exception - Member not found 0x80020003
private void btnConnect_Click(object sender, EventArgs e)
{
var nsf = new NSFApp();
if (chkSimulation.Checked) { nsf.someApp.Simulation = true; }
nsf.someScan.StartPrescan();
do
{
Console.WriteLine("Hello");
System.Threading.Thread.Sleep(100);
}
while (nsf.someScan.IsScanningPrescan());
}
错误详情:
System.Runtime.InteropServices.COMException
HResult=0x80020003
Message=Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))
Source=mscorlib
StackTrace:
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Nanosurf_C3000.IProxyScan.IsScanningPrescan()
at _AFMTestStub.Form1.btnConnect_Click(Object sender, EventArgs e) in C:\Form1.cs:line 83
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at _AFMTestStub.Program.Main() in C:\Program.cs:line 19
查不出原因!
解决方案
将变量更改为 Dynamic 确实很神奇。
谢谢约瑟夫!——</p>
推荐阅读
- coq - 在 Coq 中使用我自己的 == 运算符使用重写策略
- c++ - 如何读取 .txt 文件以在 C++ 中继承对象?
- python - Azure Linux Web 应用程序(startup.sh:未找到)
- compilation - 如何编译过时的内核(2.4.5)
- html - 我将网页的标题固定在顶部。那么为什么内容会在标题下进行呢?
- javascript - 无法在 React-native 元素中呈现 ListItem
- tomcat - 设置 Tomcat 9.0.19 时未正确定义 CATALINA_HOME 环境变量
- amazon-web-services - 如何在启动时为 ubuntu server 18.04 运行命令?
- python - 将一个字典中的键关联到另一字典中的值
- haskell - 转换元组