如何注册.NET程序集来代替现有的COM服务器? [英] How to register a .NET assembly in place of an existing COM server?

查看:379
本文介绍了如何注册.NET程序集来代替现有的COM服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须处理一个COM类,它或多或少是一个更大的系统的黑盒组件。此类来自COM EXE,并在系统中注册了已知的AppID和Typelib ID。



为了调试和记录的目的,我创建了一个mock类,在C#中实现这个接口。问题是:如何注册我的程序集,使得当调用应用程序尝试实例化接口时,它将实例化我的类?



编辑更多信息: p>

我有以下:类型库的GUID,实现类的GUID,类的ProgID GUID。我在导入类型库之后执行了以下操作:

  [ComVisible(true)] 
[ProgId .ProgId.Of.The.Old.Class)]
[Guid({旧类的guid})]
public class MockImplementation:ISomething
{
// ...
}



我试着这样注册我的类: p>

 
regasm / codebase myassembly.exe



, 什么都没发生。应用程序仍然实例化旧类。我做错了什么?

解决方案


这个类来自COM EXE


请记住,您不是为一个进程外COM服务器编写一个真正的替代品。您的替换是一个进程内服务器。一个DLL。它们的位置存储在不同的注册表项中,out-of-proc使用 LocalServer32 键,in-proc使用 InProcServer32 键。我看不到你的测试代码来判断哪一个将被使用。但显然这是错误的:)



Regasm.exe也是一个可能的故障点。请记住,它有两个版本。 c:\windows \microsoft.net\framework的32位版本写入32位注册表项,来自framework64的64位版本写入64位注册表项。两个都很好,.NET代码可以在任一模式下运行,但你的测试应用程序将只使用其中之一。取决于配置,VS测试运行器默认为32位模式afaik。



您的基本故障排除工具是SysInternals的进程监视器。很高兴看到现有的COM服务器和您的替换使用的注册表项。作为起点,请观察现有服务器注册自身,以便您确切了解什么注册表项很重要。通常通过使用/ regserver命令行选项运行EXE来完成。写下您看到的CLSID,Interface和TypeLib键。



然后,当您运行Regasm.exe时,再次执行,将您看到的内容与您记录的内容进行比较。请务必使用/ codebase选项,以便不依赖于GAC。是否使用/ tlb选项是一个判断调用。首先使用它来比较键。随着期望当然,你现在看到InProcServer32被写而不是LocalServer32,这是不可避免的。



然后再做一次,现在运行你的测试应用程序。将此次查看的内容与您的记事比较。 您可能必须敲击LocalServer32密钥才能防止其被使用。或者更好的是,完全取消注册旧的服务器,所以它不能阻碍的方式,通常使用/ unregserver选项。您必须重新运行Regasm.exe,然后把键。


I have to deal with a COM class which is more or less a black-box component of a bigger system. This class comes from a COM EXE and is registered in the system with a known AppID and Typelib ID.

For debugging and logging purposes, I created a mock class which implements this same interface in C#. The question is: how can I register my assembly so that when the calling application tries to instantiate the interface it will instantiate my class instead?

Edit with more information:

I have the following: GUID of the type library, GUID of the implementing class, ProgID GUID of the class. I did the following, after importing the type library:

[ComVisible(true)]
[ProgId("The.ProgId.Of.The.Old.Class")]
[Guid("{the guid of the old class}")]
public class MockImplementation : ISomething 
{
    // ...
}

I try to register my class like this:

regasm /codebase myassembly.exe

And yet, nothing happens. The application still instantiates the old class. What am I doing wrong?

解决方案

This class comes from a COM EXE

Do keep in mind that you did not write a true substitute for an out-of-process COM server. Your replacement is an in-process server. A DLL. Their location is stored in different registry keys, out-of-proc uses the LocalServer32 key, in-proc uses the InProcServer32 key. I can't see your test code to judge which one will be used. But clearly it is the wrong one :)

Regasm.exe is a possible trouble spot as well. Do keep in mind that there are two versions of it. The 32-bit version from c:\windows\microsoft.net\framework writes the 32-bit registry keys, the 64-bit version from framework64 writes the 64-bit registry keys. Both work fine, .NET code can run in either mode, but your test app is going to use only one of them. Depends on configuration, the VS test runner defaults to 32-bit mode afaik.

Your basic trouble-shooting tool here is SysInternals' Process Monitor. It is great to see what registry keys are used both by the existing COM server and your replacement. As a starting point, observe the existing server registering itself so you know exactly what registry keys are important. Normally done by running the EXE with the /regserver command line option. Write down the CLSID, Interface and TypeLib keys you see being written.

Then just do it again when you run Regasm.exe, compare what you see with what you wrote down. Be sure to use the /codebase option so you don't depend on the GAC. Whether or not you use the /tlb option is a judgement call. Do use it at first to compare keys. With the expectation of course that you now see InProcServer32 being written instead of LocalServer32, that's inevitable.

Then do it again one more time, now running your test app. Compare what you see being read this time against your notes. You may have to whack the LocalServer32 key to prevent it from being used. Or better yet, completely unregister the old server so it cannot get in the way, typically done with /unregserver option. You have to rerun Regasm.exe to put the keys back after that.

这篇关于如何注册.NET程序集来代替现有的COM服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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