eth0
eth0

Reputation: 5137

Loading ActiveX controls dynamically from CLSID

I'm parsing some third-party's software "script" which generates a GUI form based on it's contents and I want to read the script within a C# program and produce a similar output, here's an example script:

  BEGIN SECTION Intro
    BACKPICTURE=xxxx.gif
    PICTUREPOSN=Center
    BEGIN CONTROL CLI
    IS RADIO=NO
      CLSID={49EBC3A3-727D-11CF-9BB9-080000001311}
      POSITION=(24,16,250,45)
      QUESTION=@0:232
      BEGIN PROPERTY Title
        DISPID=2
        SETTING=CLI :
        TYPE=BSTR
      END PROPERTY
      BEGIN PROPERTY Arrangement
        DISPID=3
        SETTING=1
        TYPE=I4
      END PROPERTY
      BEGIN PROPERTY EditBoxLength
        DISPID=4
        SETTING=3
        TYPE=I4
      END PROPERTY

The properties I'm interested in is BEGIN CONTROL and BEGIN PROPERTY as these indicate the start of an ActiveX control and it's properties.

My question: how would I load this ActiveX control by it's CLSID and set its properties? Type.GetTypeFromCLSID seems to be what I want and it doesn't throw any exceptions when I use Activator.CreateInstance(Type) so it must be creating a valid instance but how would one set is properties and then "draw" this control to a Windows form?

Thanks.

Upvotes: 2

Views: 2231

Answers (1)

Hans Passant
Hans Passant

Reputation: 941495

That's difficult in .NET. An ActiveX control requires a wrapper to give it a hospitable home. That wrapper is implemented by the AxHost class. Unfortunately you cannot use this class directly in code, its constructors are protected. It was designed to be used by the AxImp.exe tool. That tool auto-generates a .NET class that derives from AxHost. The resulting class is then readily usable as a control. Problem is, that tool needs to be run up front, while you design your form. That's never a real problem, except here.

The best you could do is create wrappers with AxImp for any of the ActiveX controls you might ever find in that script up front. It is likely to be a limited subset. Then have the script interpreter select the correct wrapper, based on the clsid. Doing it dynamically like you intended requires you to create your own wrapper. AxHost is however not a small class and ActiveX hosting is quite unpleasant with many details to take care of.

Upvotes: 4

Related Questions