Search code examples
c#.netvisual-studiocom

How to make COM hello world from Framework to Framework


My boss wants me to make our product available as a COM reference. I am trying to do a simplified version to get the concepts in my head, and I've run into an error.

Reproduction steps:

  1. Create new class library in visual studio (Create New Project -> Choose "Class Library (.NET Framework) Visual C#"
  2. Add public string to Class1, add ComVisible, Guid and ProgId attributes.
  3. Add new interface, which Class1 implements, which also gets Comvisible and Guid attributes.
  4. Open project properties, go to Build section and check 'Register for COM interop'.
  5. Rebuild.
  6. Open second instance of visual studio
  7. Create second project, once again choose 'Class Library (.NET Framework) Visual C#'.
  8. In second project click 'Add Reference', and then select the COM section of the Reference Manager.
  9. The project we built in step #5 will be present, attempt to add it.

An error message appears saying:

"A reference to 'Project1' could not be added.

The ActiveX type library "[path]\Project1.tlb' was exported from a .NET assembly and cannot be added as a reference.

Add a reference to the .NET assembly instead."

I've googled this error and checked around, and it appears to be visual studio's way of being 'helpful', and informing me that there exists a superior way to reference Project1 from Project2, namely a direct reference, since they are both part of the Framework.

My question, is there any way to get around this? That is, any way to tell visual studio 'I know you want to tell me a better way, but this is the requirement, just let me use COM?'.

If not, is there any way to get around this, maybe by creating another project which is not in the Framework, which can use COM to reference Project1, and itself be referenced through COM by Project2?

Thanks for your assistance.


Solution

  • "The answer is in your error message. Add the. NET assembly as a .NET assembly; no need to go through COM"

    Read first Calling Managed C# COM Objects from C#

    A pretty good description of this scenario in: Why can't a .NET COM library be used via COM in .NET?

    Also: Is it possible to test a COM-exposed assembly from .NET?