C#: Regasm generating registry entries for every c

2019-07-01 10:45发布

I'm writing a class-library (IE BHO) in C# and currently wrangling with the large volume of what I think is junk output coming from REGASM's generated registry keys.

The short version is this: I only want to expose a handful of classes (currently: ONE class) to IE (and the rest of COM). Only one class has the ClassInterfaceAttribute and GUID stuff set, and I can test that the add-on only requires the COM registry keys for this class -- and yet, REGASM generates GUIDs and registry keys for every class in the entire project.

This is annoying and somewhat disturbing as I do not want my class names sitting in users' registry unless they absolutely have to be there.

To be fair, many of those other classes are marked public because I use them in a driver app from another project in the same solution, to work around IE's debugging black hole...

I'm still very green to COM in general (especially relating to .Net) and I was wondering what is the best way to hide all my other classes from regasm? Or, at least, why these classes that -- even though they are marked public -- are showing up when I haven't set any of the COM flags for them?

Thanks!

4条回答
放荡不羁爱自由
2楼-- · 2019-07-01 11:11

Try using the /regfile switch - this will output a reg file rather than directly writing all your class names to the registry.

When you have the .reg file you can remove any entries you dont want to be added to the target systems registry, and deploy only those values to the target machine. Depending on how you choose to deploy your software, this might be easier and you would not have to change the code for any of your types.

In fact if you dont have access to the sourcecode, this would be the only way to achieve this.

查看更多
不美不萌又怎样
3楼-- · 2019-07-01 11:16

Set ComVisible attribute to false in AssemblyInfo file and set apply ComVisible for only the desired classes.

查看更多
做个烂人
4楼-- · 2019-07-01 11:25

Use internal access modifier for stuff that doesn't need to have public modifier. For stuff that really needs public access use ComVisible attribute to partially hide it.

For example:

[ComVisible(false)]
public class ClassToHide {
    //whatever
};

public class ClassToExpose {

     public void MethodToExpose() {}

     [ComVisible(false)]
     public void MethodToHide() {}
};

All public member functions and member variables of all public classes are COM-visible by default. So first think of making them internal and if you really need them public hide them from COM with ComVisible.

查看更多
趁早两清
5楼-- · 2019-07-01 11:30

I wrote a COM component with the same problems.

I separated all the non-COM functions into a separate helper DLL. The COM DLL only contains the code that needs to be registered. Everything else is accessed through the helper DLL.

I found that an easy way to make sure that future code wouldn't accidentally be marked as public rather than internal and show up in the registry.

查看更多
登录 后发表回答