1

I love using dynamic variables for accessing COM objects. However I have a problem with one object. See the following code working in VBS:

WScript.Echo "Nazwa firmy: " & itg.FirmaInfo.Nazwa

itg is a specific object that works basically equally well in vbscript and in c# using dynamic variables. Until I try to use the member FirmaInfo. Seems like it is a very special member which requires QueryInterface call. When I was accessing it through Jacob it was in this way:

static final String sIFirmaInfo = "{3F707848-DC7D-4B37-A4C8-7270644020F7}";
ActiveXComponent fi0 = itg.getPropertyAsComponent("firmainfo");
fi = new ActiveXComponent(fi0.QueryInterface(sIFirmaInfo));
fi0.safeRelease();
// now I am able access Nazwa member of fi

I can't find a way to do this in c#. When I do a simple approach:

Console.WriteLine(itg.FirmaInfo.Nazwa)

I get an error:

Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.__ComObject' does not contain a definition for 'Nazwa'
   at CallSite.Target(Closure , CallSite , ComObject )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at Itg.open(String sKatFirmy, String sUser, String sPass) in w:\introl\prozapbi\Itg.cs:line 100

I know I could try a static client to COM object, but I am not familiar with this technique. Maybe I can stay with my dynamic approach, but need just a 3 suitable lines of code? Something that would turn my FirmaInfo object to one that exposes the IFirmaInfo interface.

Jarekczek
  • 7,456
  • 3
  • 46
  • 66

1 Answers1

0

I wasn't able to accomplish the task using dynamic. I present a workaround.

Switching myself to a static way of accessing a COM object turned out to be very easy and fast. It took a couple of minutes. Here's what I did:

set tlbimp="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\TlbImp.exe"
%tlbimp% "C:\path_to_type_library\Mxfk2015_c.dll"

These commands produce a DLL. I analyzed the DLL using ilspy, but ildasm would do too. I needed exact names to use in code. Finally after adding a reference to the dll created by tlbimp, I could change the only invocation that was failing to a static cast.

 dynamic itg = ...
 var fi = (MXDokFK.FirmaInfo)itg.FirmaInfo;

So the thing started working and I could move on. I don't use GUI. All from command line.

Jarekczek
  • 7,456
  • 3
  • 46
  • 66