COM Interop Sample

Miguel Angel Mora Paterna 0 Reputation points
2023-01-17T20:09:21.8133333+00:00

I have no idea how to create a simple dll with COM+ interoperability in c#.

Can you detail me the steps in both compiler and class to get a simple example ?

I am using Vs 2022 .

Thanks

Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,311 Reputation points Volunteer Moderator
    2023-01-18T00:47:58.42+00:00

    it is not real simple. basically you define interfaces for com and add com attributes.

    [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/example-com-class

    then you need to build an assembly library package and maybe a typelib for the package, then you need an installer.

    see docs:

    [https://learn.microsoft.com/en-us/dotnet/framework/interop/exposing-dotnet-components-to-com

    1 person found this answer helpful.
    0 comments No comments

  2. Jack J Jun 25,296 Reputation points
    2023-01-18T09:24:42.1933333+00:00

    @Miguel Angel Mora Paterna, Welcome to Microsoft Q&A,

    Please refer to the following steps to create a com dll and how to call it from C++.

    First, please do some preparations for the c# class.

    a. please create COM-callable C# classes contained in a class library.

    b. please use the ComVisibleAttribute (with a parameter value of true) for the c# classes.

    c. please must at least expose a public default constructor.

    d. we also could use other attributes which are also useful include the GuidAttribute, ProgIdAttribute and the ClassInterfaceAttribute. But the ComVisibleAttribute and the public default constructor are mandatory requirements.

    e. please declared properties and methods of the C# classes as public which are to be callable by the VC++ code.

    Second, we need to register for the COM usage.

    a. please use REGASM.EXE to register the com dll.

    b. we have to use c++ code to reference the com type dll to create the COM classes which have been exposed from your class library.

    c. please note that the COM objects, which are actually C# objects, operate in unmanaged code via COM-callable-wrappers.

    Third, please locate your c# class library at runtime.

    a. please copy the C# class library and all dependencies into the same directory as the client application.

    If you want to view the code example, you could look at Lim's answer from the question.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our [documentation][3] to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.