C# - 钩入现有的COM对象 [英] C# - Hook into existing COM object

查看:208
本文介绍了C# - 钩入现有的COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个现有的进程(或应用程序)从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#代码而不是原来的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?

当然,你必须使用与原始ocx相同的CLSID和ProgID。假设没有涉及签名,例如.Net世界中的SNK。

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,不使用。净的。他们很可能加载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 - 我们很可能只是返回成功错误代码的所有方法,只使用方法/我们的情况需要的事件。

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.

编辑:你会认为这不会太难完成。我们可以创建一个可以替代旧ocx的VB6 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对象。我没有看到一个方法来钩在一个类/接口上的实例方法或类的构造函数与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 / library / 8ccdh774(VS.80).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(VS.80).aspx Basically, what you need to do is execute the following on the commandprompt:

c:/>AxImp MyControl.ocx

result是一个MyControl.dll和AxMyControl.dll。第一个你可以在你的项目中使用作为一个普通的.NET DLL(即没有图形用户界面),第二个可以用于绘制在窗体上,你通常会用任何其他控件,如 TextBox 标签

result is a MyControl.dll and an AxMyControl.dll. The first you can use as a normal .NET DLL in your projects (i.e., without a graphical user interface), the second can be used to be drawn on a form as you normally would with any other control like a TextBox or a Label.

要使用它,右键单击您的项目,然后选择添加引用。浏览到新创建的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.

这篇关于C# - 钩入现有的COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆