“灾难性失败”从 C# 访问 OCX 库时
我目前正在尝试使用我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为我解决了!
我有同样的问题大约两周了!
我试图在 C# 中从我的 ocx 创建一个实例。该对象已成功创建,但我无法通过该死的灾难性异常调用其方法。
正如您所看到的,我试图从主对象创建一个实例并调用其方法,但这不是正确的做法!
创建 MFC 项目时,会自动生成两个接口,您可以使用它们来声明入站或出站调用的方法。一种带有 _D 前缀(在我的例子中为 _DxPKTB),另一种带有 _D 前缀和事件后修复(_DxPKTBEvents)。
当你想在 C# 中调用 Activex 方法时,你必须从它的接口创建一个实例,然后调用这些方法,而不是直接调用基础对象的方法!
这个技巧对我有用。希望对你也有用...
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.
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!
this trick worked for me. Hope to be useful for you too...
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.