1
votes

I am trying to develop a calculation engine and expose the implemented classes as COM objects following this Article. The exposed DLL (Com Object) is going to be consumed by a third-party application. Some older DLLs implemented by VB6 are now using and working properly.

While the final DLL is registered via RegAsm command in the Command Prompt, the COM object becomes visible in the target application but I receive the error messages of

"Failed to create object", "Object has no properties or methods".

What I have tried so far, in addition to the code below, is listed below :

  • ComVisible is set to true in AssemblyIfo.cs
  • Checked "Register for COM interop" in Build Options
  • Set [ComVisible(true)] attribute on classe/interface and/or method
  • Set [DispId(0)] attribute on classe/interface and/or method with different values !!
  • Set [ProgId] attribute on classe/interface and/or method
  • All methods and classes are defined as public members
  • Tried all the above actions to the class with/without interface
  • Tried all the above actions to the class with/without event interface

I have created a sample code as an example here, any further help would be appreciated :)

using System;
using System.Runtime.InteropServices;

namespace project_name
{
    [Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
    [ComVisible(true)]
    public interface ComClass1Interface
    {
        [DispId(0)]
        [ComVisible(true)]
        double calc();
    }

    [Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
        InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ComClass1Events
    {
    }

    [Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
        ClassInterface(ClassInterfaceType.None),
        ComSourceInterfaces(typeof(ComClass1Events))]
    public class ComClass1 : ComClass1Interface
    {
        public double calc()
        {
            return 13;
        }
    }
}

The DLL is registered using RegAsm command to be listed on COM Objects. DLL registration using RegASM

The Com Object becomes visible to the target application.

The object is now visible as COM Objects

When trying to select the implemented method I face these errors :-/:

enter image description here

I am using Visual Studio 2019, C#, .NET Framework 4.0 and the target application is running on Windows Server 2008 R2 and .NET Frameworks 3.5, 4.6 are installed.

1
You didn't actually select the COM object that you created. Hard to guess what it was, probably an old attempt. The one you're talking about here does not have a [ProgId] so can't show up in that list and does not have its type library registered (/tlb option). Doesn't look anything like OPC either, maybe you've posted way too much fake info.Hans Passant
I have tried setting [ProgId] attribute either, but I removed it after getting no response for the sake of clean codes. [I updated the previous attempts and added this] By the way, where should [ProgId] attribute be added to the methods? signatures or implementation or both?Mahyar Mottaghi Zadeh
[ProgId] attribute would go on your ComClass1. It is recommended to use one. My experience has been if you don't specifically use one, then one will be generated for you in the form of "NamespaceName.ClassName" -- just like you saw with project_name.ComClass1...but gee, you want something better than that...Joseph Willcoxson

1 Answers

3
votes

The issue solved by These actions:

  1. The assembly should be signed with a strong name (I used Visual Studio Signing tool)
  2. The COM Object should be registered with regasm path/dll-name.dll /codebase /tlb /nologo