How to properly register COM components from .NET48 to .NET8.0

How to properly register COM components from .NET48 to .NET8.0

18636681122
Observer Observer
205 Views
3 Replies
Message 1 of 4

How to properly register COM components from .NET48 to .NET8.0

18636681122
Observer
Observer

This is my .NET4.8 reg method:

cd <cadPath>
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm <MyNET48Program>.dll /tlb

 

But in NET8.0 tlb can't be created, so I try a simple com demo:

using System.Runtime.InteropServices;
namespace MyCom;

[ComVisible(true)]
[Guid("4FB4A212-AE3B-4D3B-9262-FC2DE2D11098")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IServer
{
    double Add(double a, double b);
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("MyComApp.Server")]
[Guid("648D8C69-C5B2-45AF-9F22-E6AD2E869B20")]
public class Server : IServer
{
    public double Add(double a, double b) => a + b;
}

.csproj:

<EnableComHosting>true</EnableComHosting>

then I get one `MyCom.comhost.dll` and move all files(include MyCom.dll) to acad.exe path(D:\Software\AutoCAD\AutoCAD 2026)

In this path reg:

regsvr32 MyCom.comhost.dll

MessageBox say reg success!

 

In VB6.0 I write a test:

 

Private Sub Form_Load()
    Dim cad As Variant
    Set cad = CreateObject("AutoCAD.Application.25")
    
    Dim o As Variant
    Set o = cad.GetInterfaceObject("MyComApp.Server")
    
    Dim res As Double
    
    res = o.Add(1.2, 3.4)
End Sub

 

 

Error in "cad.GetInterfaceObject("MyComApp.Server"), com components can't be find, C# has same problems.So how can i do?

0 Likes
206 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

Have you read this post in Autodesk ADN's "AutoCAD DevBlog":

 

https://adndevblog.typepad.com/autocad/2024/12/bridging-the-net-core-and-autocad-2025-exposing-and-u... 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

18636681122
Observer
Observer

Hello, thank you for your attention to this issue.

I read this blog post and tried it, but his situation was slightly different from mine; I needed to call the COM component in the exe programme, which is the way of plugins.I registered as the blog post did, using VB6 and C# to find my components and they all failed:

Dim cad As Variant
    Set cad = CreateObject("AutoCAD.Application.25")
    
    Dim o As Variant
    Set o = cad.GetInterfaceObject("AutoCADComDemo.Server")
        dynamic acadApp = null;
        try
        {
            Type t = Type.GetTypeFromProgID("AutoCAD.Application.25");
            acadApp = Activator.CreateInstance(t);

            if (acadApp != null) 
            {
                string progId = "MyComApp.Server";
                dynamic server = acadApp.GetInterfaceObject(progId);
                var ret = server.Add(3.1, 4.2);
                Console.WriteLine(ret);
            }

        }
        catch (Exception ex)
        {


        }

 I don't know what's wrong, can you help me take a look?

0 Likes
Message 4 of 4

18636681122
Observer
Observer

C# code should be:

 

string progId = "AutoCADComDemo.Server";
                dynamic server = acadApp.GetInterfaceObject(progId);
                var ret = server.ComputePi();
                Console.WriteLine(ret);

 

But it doesn't really matter, the general direction is like this.

0 Likes