首页 > 解决方案 > 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

查不出原因!

标签: c#.netcomvisual-studio-2019

解决方案


将变量更改为 Dynamic 确实很神奇。

谢谢约瑟夫!——</p>


推荐阅读