Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFCreator COM interop with different versions

Tags:

c#

pdf

interop

I need to connect to PDFCreator COM interface from C# application. For this purpose, I use VS generated Interop wrappers. But some clients have different version of PDFCreator (I have 1.2.1, some have 1.4.3) and if I use Interop generated for 1.2.1 I get exception when using the wrapper. Is there any way around this? So my app can support multiple versions of PDFCreator?

Also, I am not sure wheather PDFCreator interface actually changed. Isn´t there some lock on exact version of COM object, I could simple turn off while generating Interop libraries? Thank you.

like image 307
B.Gen.Jack.O.Neill Avatar asked Feb 18 '23 00:02

B.Gen.Jack.O.Neill


1 Answers

So, based on yms answer, I got in the right direction :)

.NET 4.0 supports late binding COM objects similiarly to VB, using System.Type and the magic dynamic keyword.

Simple example how to run PDFCreator using dynamic binding:

    System.Type pdfCreatorType = System.Type.GetTypeFromProgID("PDFCreator.clsPDFCreator");
    dynamic comPdfCreator = System.Activator.CreateInstance(pdfCreatorType);

    comPdfCreator.cStart("", true);

This works like a charm, and you don´t need to recompile your Interop Assemblies for each new PDFCreator version.

like image 157
B.Gen.Jack.O.Neill Avatar answered Feb 24 '23 13:02

B.Gen.Jack.O.Neill