2

我目前正在尝试使用我的 C# 应用程序中的第三方 DLL。我已经注册了 DLL 并将其添加为 COM 组件列表中的引用。据我了解,这应该创建必要的互操作类来从 C# 访问这个 DLL。

在尝试从 DLL 中调用任何方法时,我得到以下异常:-

System.Runtime.InteropServices.COMException was unhandled
 Message=Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
 Source=mscorlib
 ErrorCode=-2147418113
 StackTrace:
  at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
  at OCXDLL._OCXDLL.MyMethod(Int32 param0, String param1, String param2, String param3, Int32 param4)
  at MyApplication.Program.Main(String[] args) in C:\MyApplication\Program.cs:line 29
  at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
  at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  at System.Threading.ThreadHelper.ThreadStart()
 InnerException: 

我用来调用库的代码:-

using OCXDLL;

namespace MyApplication
{
 class Program
 {
  static void Main(string[] args)
  {
   OCXDLL libObj = new OCXDLL();
   libObj.MyMethod(....); 
  }
 }
}

请注意,这不是一个 ActiveX 控件(我已经看到了许多解决此问题的解决方案)。

有人对我哪里出错有任何想法吗?

谢谢

4

2 回答 2

2

为我解决!

我有同样的问题大约两个星期!!!!

我试图在 c# 中从我的 ocx 创建一个实例。该对象已成功创建,但我无法使用该死的灾难性异常调用其方法。

 xPKTBLib.xPKTB p = new xPKTBLib.xPKTB()

如您所见,我试图从主对象创建一个实例并调用它的方法,但这不是正确的做法!创建 MFC 项目时,会自动生成两个接口,您可以使用它们来声明入站或出站调用的方法。一种带有 _D 前缀(在我的情况下为 _DxPKTB),另一种带有 _D 前缀和事件后缀(_DxPKTBEvents)。

当你想在 c# 中调用 Activex 方法时,你必须从它的接口创建一个实例,然后调用方法而不是直接调用基对象的方法!

  xPKTBLib._DxPKTB p = new xPKTBLib.xPKTB();

这个技巧对我有用。希望对你也有用...

于 2013-01-21T12:25:53.007 回答
1

视觉工作室

点击工具->选项->调试

仅禁用我的代码(仅限托管)

如果在启动时没有用户代码,则在此警告下

这对我有用。

于 2014-09-12T04:56:01.290 回答