简体   繁体   English

如何使用 Regsvr32 注册 .NET COM DLL?

[英]How do I register a .NET COM DLL with Regsvr32?

I have a VB6 application that uses a COM DLL.我有一个使用 COM DLL 的 VB6 应用程序。 The DLL is written in C#. DLL 是用 C# 编写的。 In the C# project properties I have the "Register for COM interop" option checked.在 C# 项目属性中,我选中了“注册 COM 互操作”选项。 The VB6 app works fine on my development machine. VB6 应用程序在我的开发机器上运行良好。 The C# code follows this format exactly: CodeProject C# COM Example C# 代码完全遵循以下格式: CodeProject C# COM 示例

When deploying to other machines, Regsvr32.exe gives me the following error when I try to register the DLL:部署到其他机器时,Regsvr32.exe 在我尝试注册 DLL 时出现以下错误:

The module "MyCOM.dll" was loaded but the entry-point DLLRegisterServer was not found.

What does this mean?这是什么意思? No tutorials/documentation I've read about COM DLLs say anything about "entry-point DLLRegisterServer".我读过的有关 COM DLL 的教程/文档中没有提到“入口点 DLLRegisterServer”。

We have had MAJOR problems using RegAsm.exe on different machines, so we really need a solution where we can run regsvr32.exe instead that will work for any machine that we deploy to (ie XP, Vista, Windows 7, x86 machines, x64 machines, etc.)我们在不同的机器上使用 RegAsm.exe 遇到了重大问题,所以我们真的需要一个解决方案,我们可以运行 regsvr32.exe 而不是我们部署到的任何机器(即 XP、Vista、Windows 7、x86 机器、x64机器等)

What do I need to add to my C# code to make it register-able with regsvr32.exe?我需要在我的 C# 代码中添加什么才能使其可通过 regsvr32.exe 注册?

You can't.你不能。 Managed [ComVisible] class libraries need to be registered with Regasm.exe.托管 [ComVisible] 类库需要使用 Regasm.exe 进行注册。

You can do it from the IDE with Project + Properties, Build tab, Register for COM interop checkbox.您可以在 IDE 中使用 Project + Properties、Build 选项卡、Register for COM interop 复选框执行此操作。 If you run Regasm.exe you usually want the /codebase command line option so you don't have to put the assembly in the GAC.如果您运行 Regasm.exe,您通常需要 /codebase 命令行选项,这样您就不必将程序集放在 GAC 中。 Yet another option is to let Regasm.exe generate a .reg file with the /regfile option.另一种选择是让 Regasm.exe 使用 /regfile 选项生成一个 .reg 文件。 You'd just run that on the target machine to get the registry updated.您只需在目标机器上运行它即可更新注册表。

Edit: just saw the "major problems" remark.编辑:刚刚看到“主要问题”的评论。 Note sure what they are, short from /codebase.请注意它们是什么,来自 /codebase 的缩写。 You do have to pick the right version on 64-bit machines.您必须在 64 位机器上选择正确的版本。 There are two.那里有两个。 And you need an elevated command prompt so that UAC don't put a stop to it.而且您需要一个提升的命令提示符,以便 UAC 不会阻止它。

You can make a simple Windows application and use the code below to register COM DLL.您可以制作一个简单的 Windows 应用程序并使用下面的代码来注册 COM DLL。 Make sure to add the manifest file to run as Administrator:确保添加清单文件以管理员身份运行:

...

namespace comregister
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string framework = Environment.GetEnvironmentVariable("SystemRoot") + @"\Microsoft.NET\Framework\v2.0.50727\";

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
                button2.Enabled = true;
                button3.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FileInfo fi = new FileInfo(textBox1.Text);
            string fn = fi.FullName.Substring(0, fi.FullName.Length - 4);
            string dll = "\"" + fi.FullName + "\"";
            string tlb = "\"" + fn + ".tlb\"";

            Process p = new Process();
            p.StartInfo.FileName = framework + "regasm.exe";
            p.StartInfo.Arguments = dll + " /tlb:" + tlb + " /codebase";
            p.Start();
            p.WaitForExit();
            label2.Text = "registered";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FileInfo fi = new FileInfo(textBox1.Text);
            string dll = "\"" + fi.FullName + "\"";

            Process p = new Process();
            p.StartInfo.FileName = framework + "regasm.exe";
            p.StartInfo.Arguments = dll + " /unregister";
            p.Start();
            p.WaitForExit();
            label2.Text = "unregistered";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM