2

I created a test COM project for .Net 4.0. Then I register it with the regasm:

RegAsm /codebase  TestCom.dll /TLB

And it works correctly in JavaScript:

var app = new ActiveXObject("TestCom.TestClass");
app.Message1("123");

I want to use TestCom.TestClass from another C# project for .Net 3.5, but when I try to add the reference to this project, I get an error about higher framework version. The "Add Reference" dialog(section COM) only shows reference to a tlb file, not dll.

Is this the way it should be? When I try to add a reference to the tlb file I get the error:

"Add a reference to the .NET assembly instead"

How can I create an instance of TestCom.TestClass from another C# project for .Net 3.5?

Stefan
  • 17,448
  • 11
  • 60
  • 79
RomanKovalev
  • 852
  • 3
  • 10
  • 29
  • Does this answer your question? [Why can't a .NET COM library be used via COM in .NET?](https://stackoverflow.com/questions/15014266/why-cant-a-net-com-library-be-used-via-com-in-net) – GSerg Oct 26 '21 at 07:59
  • Really? Now is 2021 – RomanKovalev Oct 26 '21 at 09:42
  • Yes, it is 2021. I was not trying to answer your question per se, just to link several related questions together. Just recently another question was posted on the same subject, and the potential duplicates are all unconnected to each other. – GSerg Oct 26 '21 at 10:31

1 Answers1

7

The IDE can tell from the type library that you selected that the COM server is implemented in .NET. And it just refuses to let you add it, doing this doesn't make sense. It will work just as well, in fact better, when you add a reference to the .NET assembly instead.

It's not like you can't fool the machine, you can use late-binding with the dynamic keyword. No way for the IDE to interfere with that. But you are not actually testing the COM server the way an unmanaged client is going to use the server. The CLR will discover at runtime that the COM server is in fact a .NET server and shortcuts the plumbing. In other words, it will not create an RCW for the server. And the server won't create a CCW, this now works the exact same way it would have worked if you had added the assembly reference.

So it really does make no sense to do it this way. Just add the assembly reference and enjoy the many advantages you get from having IntelliSense support and static type checking. And test your COM server code the way you test any .NET library. What you don't test, and fundamentally cannot test, is the COM interop layers. Which is okay, it's not like it ever really is a problem. And if it is some for some mysterious reason then it is not like you can ever do something about it, the COM interop inside the CLR is a black box anyway.

All you need to do in addition to testing the server code is to perform an integration test. Just make sure that the actual unmanaged client can in fact use the server. You did, you already know it works well from Javascript.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I can't add reference to dll for .Net4.0 from project for .Net3.5. It's the primary problem – RomanKovalev Nov 13 '13 at 11:52
  • That's not the primary problem. The real problem is no matter what you'll do, the code in that server can never ever run. It will just fail to load, whatever way you use to call it. You really do have a dependency on .NET 4.0, you cannot hide that under a floor mat. You can craft an app.exe.config file that forces .NET 4.0 to be used, even though the exe targets 3.5. But that's just utterly pointless in a test project. – Hans Passant Nov 13 '13 at 11:59
  • ok, whether there is a way to use library for .NET 4.0 from project for 3.5? System where it will be run haves both .NET 3.5 and .NET 4.0. – RomanKovalev Nov 13 '13 at 12:21
  • As I said, an app.exe.config file is required. The `` element must be "v4.0". Still no clue what any of this has to do with COM. – Hans Passant Nov 13 '13 at 13:03
  • Ok, I have it in config file omg. What I should to do to create instance of TestClass? Can I do it in C# as simple as in JavaScript??? – RomanKovalev Nov 14 '13 at 07:48