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?

3
  • Does this answer your question? Why can't a .NET COM library be used via COM in .NET?
    – GSerg
    Oct 26, 2021 at 7:59
  • Really? Now is 2021 Oct 26, 2021 at 9: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, 2021 at 10:31

1 Answer 1

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.

5
  • I can't add reference to dll for .Net4.0 from project for .Net3.5. It's the primary problem Nov 13, 2013 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. Nov 13, 2013 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. Nov 13, 2013 at 12:21
  • As I said, an app.exe.config file is required. The <supportedRuntime> element must be "v4.0". Still no clue what any of this has to do with COM. Nov 13, 2013 at 13:03
  • Ok, I have it in config file <configuration> <supportedRuntime version="v4.0"></supportedRuntime> </configuration> omg. What I should to do to create instance of TestClass? Can I do it in C# as simple as in JavaScript??? Nov 14, 2013 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.