从.Net 6 DLL注册和检索COM对象(Typelib导出:类型库未注册。(HRESULT异常:0x80131165))

问题描述 投票:0回答:1

我使用以下脚本从.Net 6 DLL 注册 COM 对象

.\regsvr32.exe .\MyLibrary.comhost.dll
另外,在DLL中,它通过
[DllImport("oleaut32.dll")] internal extern static int RegisterActiveObject([MarshalAs(UnmanagedType.IUnknown)] object punk, ref Guid rclsid, uint dwFlags, ref int pdwRegister);
将自身注册到ROT中 当我可以使用
Marshal.GetActiveObject("MyLibrary.MyClassName")
获取这个对象后,我可以获取该对象,但是 当我尝试从此对象调用方法时,会引发
System.Runtime.InteropServices.COMException: 'Typelib export: Type library is not registered. (Exception from HRESULT: 0x80131165)'
异常。请您帮忙解决这个问题吗?

c# .net visual-studio com typelib
1个回答
0
投票

@SimonMourier

库1.DLL

using System.Runtime.InteropServices;
namespace Library1
{
    [ComVisible(true)]
    [Guid("AAAAAAAC-5716-4635-9954-94F1333CAE92")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ICalculator
    {
        [DispId(1)] int myadd(int a, int b);

        [DispId(2)] int contest();
    }


    [ComVisible(true)]
    [Guid("AAAAAAAB-5716-4635-9954-94F1333CAE92")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Library1.Calculator")]
    public class Calculator : ICalculator
    {

        private int hreg;


        public Calculator()   // so this is the class opening
        {
            Guid clsidApp = new Guid("AAAAAAAB-5716-4635-9954-94F1333CAE92");
            native.RegisterActiveObject(this, ref clsidApp, 0, ref hreg);
        }


        public int myadd(int a, int b)
        {
            return a + b;
        }

        public int contest()
        {
            return 1;
        }
    }
}
using System.Runtime.InteropServices;

namespace Library1
{

    internal static class native
    {
        [DllImport("oleaut32.dll")]
        internal extern static int RegisterActiveObject([MarshalAs(UnmanagedType.IUnknown)] object punk, ref Guid rclsid, uint dwFlags, ref int pdwRegister);
        [DllImport("oleaut32.dll")]
        internal extern static int RevokeActiveObject(int dwRegister, object pvReserved);

    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <EnableComHosting>true</EnableComHosting>
     <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>disable</Nullable>
    <Platforms>AnyCPU;x86</Platforms>
  </PropertyGroup>


<PropertyGroup>
    <OutputType>library</OutputType>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <UseWPF>false</UseWPF>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <ApplicationIcon />
    <SignAssembly>False</SignAssembly>
    <DelaySign>False</DelaySign>
</PropertyGroup>

</Project>

HOST核心项目

namespace HostCoreDll
{
    public partial class Form1 : Form
    {
        public Library1.Calculator mydll;

        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            mydll = new Library1.Calculator();

            label1.Text = mydll.contest().ToString();
        }
    }
}

调用COM对象项目:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace callingprog
{
    public partial class Form1
    {

        public Form1()
        {
            InitializeComponent();
        }

        public dynamic td;

        private void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                td = Marshal.GetActiveObject("Library1.Calculator");

                int res = td.myadd(5, 6);

                TextBox1.Text = res.ToString();

            }

            catch (Exception ex)
            {
                MessageBox.Show("problem: " + ex.Message);
            }

        }

        private static Form1 _DefaultInstance;
        public static Form1 DefaultInstance
        {
            get
            {
                if (_DefaultInstance == null || _DefaultInstance.IsDisposed)
                    _DefaultInstance = new Form1();

                return _DefaultInstance;
            }
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.