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

后端 未结 2 1814
闹比i
闹比i 2020-11-28 15:18

I have a VB6 application that uses a COM DLL. The DLL is written in C#. In the C# project properties I have the \"Register for COM interop\" option checked. The VB6 app work

相关标签:
2条回答
  • 2020-11-28 15:44

    You can make a simple Windows application and use the code below to register 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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 15:48

    You can't. Managed [ComVisible] class libraries need to be registered with Regasm.exe.

    You can do it from the IDE with Project + Properties, Build tab, Register for COM interop checkbox. 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. Yet another option is to let Regasm.exe generate a .reg file with the /regfile option. 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. You do have to pick the right version on 64-bit machines. There are two. And you need an elevated command prompt so that UAC don't put a stop to it.

    0 讨论(0)
提交回复
热议问题