Referencing different ActiveX versions from C# as one

158 Views Asked by At

I'm quite struggling when trying to to what follows.

I'm currently using an ActiveX, through an interop dll generated by Visual Studio (more precisely tlbimp, implicitly).

Different versions of this ActiveX exist, that sometimes add, remove, properties in the interfaces. If I want to change the version, I have to change the reference, recompile and so on.

I'd like to have a single version of my code. I remember that to drive Word for example, one can instantiate a word object via CreateInstance on Word.Application, and call methods, whatever the version. Calls being linked only at runtime. I'd like to do the same with the activeX I use, but I don't really know how to do that (btw, it's sage objets100c dll). I don't find it in the list of ActiveX, thus I'm not even sure I can do like for Word.Application.

Has someone a clue about how I could do that ? (might be a completely different solution, as my need is: having one code with no need to recompile).

Thank you in advance

1

There are 1 best solutions below

1
Olivier Jacot-Descombes On BEST ANSWER

If you have a corresponding *.tlb file, reference this one, not the dll. In the properties window of the reference, you can set the Specific Version property to False and EmbedInterop Types to True (makes it more stable).

If this does not help, you can try to create and access the object dynamically instead of adding a reference. You need either the ProgID of the COM type …

Type comType = Type.GetTypeFromProgID("MyProg.ProgId");

… or the CLSID of the COM type (you may find them in the registry)

Type comType = Type.GetTypeFromCLSID(new Guid("EA2BACD6-9D0F-4819-98BC-88E8173D3D16"));

Then you can create and object with Activator.CreateInstance and assign it to a dynamic to do late binding.

dynamic sage100 = Activator.CreateInstance(comType);

You will get no IntelliSense for this dynamic object. If you specify non-existing members (method or property names), you can still compile and run your C# code; however, a run-time exception will be thrown when you attempt to access these members.