Skip to content

how to make a com dll by .net10 aot? #7081

@xiaoyaocode163

Description

@xiaoyaocode163

https://github.com/dotnet/samples/tree/main/core/interop/comwrappers/IDispatch

how to make a com dll by .net10 aot?
my code can't createobject on vb6,vba
`using Microsoft.Win32;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Windows.UI.Popups;

[assembly: Guid("A0D8A4B2-E9F7-44B6-9665-D561A78C3D43")]

namespace MyDotNetCom
{
[ComVisible(false)] //
public static class ComRegistration
{

    //  [UnmanagedCallersOnly(EntryPoint = "DllRegisterServer", CallingConvention = CallingConvention.StdCall)]
    [UnmanagedCallersOnly(
    EntryPoint = "DllRegisterServer"
)]

    public static int DllRegisterServer()
    {
        try
        {
          
            string dllPath = "d:\\AOT_ComDLL.dll";

            Type comClassType = typeof(MyComObject);
            Guid clsid = comClassType.GUID;
            string clsidStr = clsid.ToString("B");  
            string progId = ((ProgIdAttribute)Attribute.GetCustomAttribute(comClassType, typeof(ProgIdAttribute)))?.Value
                            ?? "MyDotNetComObject.v10";
            //string assemblyPath = dllPath;
           
            string assemblyPath = dllPath;// GetAotDllPathInRegsvr32();


            //System.Reflection.Assembly.GetExecutingAssembly().Location;
            //Win32API.MessageBox(0, assemblyPath, "DLL", 0);
           
            using (RegistryKey clsidKey = Registry.ClassesRoot.CreateSubKey($"CLSID\\{clsidStr}"))
            {
                clsidKey.SetValue("", comClassType.FullName); 
                                                            
                using (RegistryKey inprocKey = clsidKey.CreateSubKey("InprocServer32"))
                {
                    inprocKey.SetValue("", assemblyPath); 
                    inprocKey.SetValue("ThreadingModel", "Apartment");
                    inprocKey.SetValue("CodeBase", assemblyPath);
                }
          
                using (RegistryKey progIdKey = clsidKey.CreateSubKey("ProgID"))
                {
                    progIdKey.SetValue("", progId);
                }
               
                using (RegistryKey implementedKey = clsidKey.CreateSubKey("Implemented Categories\\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}"))
                {
                    //
                }
            }

        
            using (RegistryKey progIdKey = Registry.ClassesRoot.CreateSubKey(progId))
            {
                progIdKey.SetValue("", $"My .NET 10 AOT COM Object");
                using (RegistryKey clsidSubKey = progIdKey.CreateSubKey("CLSID"))
                {
                    clsidSubKey.SetValue("", clsidStr);
                }
            }

            return 0; // S_OK
        }
        catch (Exception ex)
        {

            Marshal.ThrowExceptionForHR(-2147467259);
            return -2147467259;
        }
    }

    // COM标准注销函数
    [UnmanagedCallersOnly(EntryPoint = "DllUnregisterServer")]
    public static int UnregisterServer()
    {
        try
        {

            Type comClassType = typeof(MyComObject);
            Guid clsid = comClassType.GUID;
            string clsidStr = clsid.ToString("B");
            string progId = ((ProgIdAttribute)Attribute.GetCustomAttribute(comClassType, typeof(ProgIdAttribute)))?.Value
                            ?? "MyDotNetComObject.v10";

     
            Registry.ClassesRoot.DeleteSubKeyTree($"CLSID\\{clsidStr}", false); // false:


            Registry.ClassesRoot.DeleteSubKeyTree(progId, false);

            return 0; // S_OK:
        }
        catch (Exception ex)
        {
            
            Marshal.ThrowExceptionForHR(-2147467259);
            return -2147467259;
        }
    }
}


[ComVisible(true)]
[Guid("A1D8A4B2-E9F7-44B6-9665-D561A78C3D43")] 
[ProgId("MyDotNetComObject.v10")]
public class MyComObject : IMyComObject
{                         
    public int Calculate(int x, int y) => x * y + 100;
    public string GetVersion() => ".NET 10 AOT COM";
}

[ComVisible(true)]
[Guid("A2D8A4B2-E9F7-44B6-9665-D561A78C3D43")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMyComObject
{
    int Calculate(int x, int y);
    string GetVersion();
}


public class Win32API
{
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
}

}//end namespace MyDotNetCom`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions