C# - 挂钩到现有的COM对象对象、COM

2023-09-04 02:19:27 作者:骨歌

假设我们有一个现有的流程(或应用程序),从一个OCX文件,如MyCOMLibrary.ocx调用COM对象。

Say we have an existing process (or application) that calls a COM object from an ocx file such as "MyCOMLibrary.ocx".

有没有一种方法来写一个C​​#库精确复制OCX文件?使原来的应用程序可以调用你的C#code,而不是原来的COM对象?

Is there a way to write a C# library to exactly replicate the ocx file? So that the original application can call your C# code rather than the original COM object?

您会,当然,必须使用相同的CLSID的ProgID,并与原OCX。而假设没有签约参与,如SNK的.NET世界中。

You would, of course, have to use identical CLSID and ProgIDs as the original ocx. And assuming there is no signing involved, such as a SNK in the .Net world.

此外,是否有存在自动执行此任何工具?它带一个OCX,并吐出了与实现方法C#文件。

Also, are there any tools that exist to automate this? Something that takes in an ocx and spits out a C# file with methods to implement.

编辑:我想补充的是,原来的应用程序是VB6,和所有不使用.NET。他们最有可能加载OCX作为一个VB6应用程序将(或的ProgId GUID)。这是否造成任何问题?

I want to add that the original application is VB6, and does not use .Net at all. They are most likely loading the ocx as a VB6 app would (ProgId or Guid). Does this cause any issues?

我们还必须具有完全重写OCX没问题 - 我们将最有可能只返回成功错误codeS通过我们的情况,要求所有的方法,只有使用方法/事件

We also have no problem with completely rewriting the ocx--we will most likely just return success error codes for all methods and only use methods/events required by our situation.

编辑:你可能会认为这不会是太难实现。我们可以做一个VB6的OCX文件,就可能取代旧的OCX,只是所有的呼叫传递到.NET程序集?

You would think this would not be too difficult to accomplish. Can we make a VB6 ocx file that could replace the old ocx, and just pass all calls to a .Net assembly?

编辑:我试着用下面的开源库: EasyHook

I tried using the following open source library: EasyHook

但好像这个问题应该还是可行的。 VB6似乎加载COM对象在prevents挂钩的方式。我不明白的方式来勾上一个类/接口或类的构造函数EasyHook实例的方法。

But it seems like this question should still be viable. VB6 seems to load COM objects in a way that prevents hooking. I don't see a way to hook instance methods on a class/interface or a class's constructor with EasyHook.

推荐答案

您可以使用ActiveX导入AxImp导入OCX,创建一个包装类,然后调用。该方案的描述如下: http://msdn.microsoft .COM / EN-US /库/ 8ccdh774%28VS.80%29.aspx 基本上,你需要做的是执行上的commandprompt如下:

You can use ActiveX Import AxImp to import the OCX, create a wrapper class and then call that. The program is described here: http://msdn.microsoft.com/en-us/library/8ccdh774%28VS.80%29.aspx Basically, what you need to do is execute the following on the commandprompt:

c:/>AxImp MyControl.ocx

结果是MyControl.dll和AxMyControl.dll。第一,你可以在你的项目中一个正常的.NET的DLL使用(也就是说,没有图形用户界面),第二个可以使用的形式来绘制,你通常会与任何其他控制像文本框标签

要使用它,去到Visual Studio中,单击鼠标右键项目,选择添加引用。浏览到新创建的DLL和添加。这一切。

To use it, go to Visual Studio, rightclick your project and select Add Reference. Browse to the newly created DLL and add it. That's all.