【发布时间】:2021-03-14 04:02:43
【问题描述】:
我在 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
无法破译原因!
【问题讨论】:
-
你有这个 NSF 的链接吗?
-
“链接”是什么意思?
-
如果我们不知道这是什么,我们无法帮助调试您的 NSF 东西。链接到项目页面?
-
对于微笑,在您的第一个示例中,flexApp 的类型是什么?您将其声明为“var”而不是“Application”。分配后在第一个函数中设置断点并查看类型。如果没有其他方法,您可以尝试使类中的变量动态化...
标签: c# .net com visual-studio-2019