“灾难性失败”从 C# 访问 OCX 库时

发布于 2024-11-01 05:22:46 字数 1643 浏览 3 评论 0原文

我目前正在尝试使用我的 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 控件(对此我已经看到了许多解决方案来修复)这个问题)。

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

谢谢

I'm currently trying to use a third-party DLL from my C# application. I've registered the DLL and added it as a reference from the list of COM component. As I understand, this should create the necessary interop classes to access this DLL from C#.

On attempting to call any methods from within the DLL I get the following exception:-

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: 

And the code I'm using to call the library:-

using OCXDLL;

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

Note that this is not an ActiveX control (for which I have seen a number of solutions to fix this problem).

Does anyone have any ideas as to where I'm going wrong?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不寐倦长更 2024-11-08 05:22:46

为我解决了!

我有同样的问题大约两周了!

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

 xPKTBLib.xPKTB p = new xPKTBLib.xPKTB()

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

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

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

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

Solved For Me!

I had the same problem for about two weeks!!!

I was trying to create an instance from my ocx in c#. the object was successfully created but I was unable to call its methods with the damn Catastrophic Exception.

 xPKTBLib.xPKTB p = new xPKTBLib.xPKTB()

As you see I was trying to create an instance from the main object and call its methods but its Not the right thing to do!
When you create a MFC project, two interfaces are generated automatically and you use them to declare your methods for inbound or outbound calls. One with _D prefix (in my case as _DxPKTB) and one with _D prefix and Events post-fix (_DxPKTBEvents).

When you want to call the Activex methods in c# you have to create an instance from it's interface and then call the methods instead of calling the base object's methods directly!

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

this trick worked for me. Hope to be useful for you too...

似最初 2024-11-08 05:22:46

Visual Studio

单击“工具”->“选项”->“调试”

在此警告下仅禁用我的代码(仅限托管)

如果启动时没有用户代码

这对我有用。

Visual Studio

Click on TOOLs->OPTIONS->DEBUGGING

Disable just my code(Managed only)

under this Warn if no user code on launch

It's work for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文