Jalalabad
Jalalabad

Reputation: 23

How to use an unmanaged COM DLL in C# without referencing

I am looking into writing a DLL in C# which as part of its functionality will utilise methods within a third party DLL.

The third party COM DLL, written in C++, is registered on client machines as part of a third party app.

Now, I can include (by referencing the tlb file) the DLL in my solution and interact with it without any problem. However, the DLL name (not the method signatures etc) changes each year as part of some version control incorporated by the 3rd party. Additionally, I cannot distribute the 3rd party DLL (licensing etc).

What I basically want to do is use the unmanaged COM DLL in my DLL without referencing or including the DLL in my solution.

And what I am looking for is some good articles/examples for this specific requirement. I have seen plenty for using unmanaged DLL by filename, managed COM etc but these do not fit in with what I am trying to achieve.

Even if someone could give me a basic example using the below elements that would be a great starting point:

Upvotes: 2

Views: 2147

Answers (2)

Hans Passant
Hans Passant

Reputation: 942197

Most COM servers support late binding. Pretty similar to using Reflection in .NET. You'd use Type.GetTypeFromProgID() and Activator.CreateInstance(Type) to create the COM object. Chief disadvantages to late binding:

  • You don't get IntelliSense
  • Any mistakes you make produce runtime errors instead of compile errors
  • It is much slower
  • C# syntax is painful unless you can use the C# version 4 dynamic keyword or use a VB.NET wrapper
  • The debugger can't show you properties of the COM object. You only see an opaque __ComObject

But with the advantage that you don't have to re-run Tlbimp.exe when the vendor updates the DLL. If that update is breaking then you'll get a runtime error instead of a compiler diagnostic. Always painful.

Upvotes: 2

Igor Korkhov
Igor Korkhov

Reputation: 8558

There are many resources in the Internet, this blog post should give you a good starting point.

Upvotes: 1

Related Questions