dotnet / dotnet/samples

how to make a com dll by .net10 aot?

Open
#7,081 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
5.1k
Avg merge
9h 11m
Merged PRs (30d)
1

Description

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`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.