ActiveX control without a form

20

We are required to use a 3rd party ActiveX control.

The only issue is, the layer in our software is a business layer and has no access to a window or form. It also runs on separate threads (and should work from any thread) that are not STA.

Rather than breaking our separation of UI from business logic, we used this workaround to make it work:

Thread thread = new Thread((ThreadStart)
delegate
{
_myActiveX = new MyActiveXType();
_myActiveX.CreateControl();

//more initialize work

Application.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();

Then anytime we need to reference the control, we call _myActiveX.BeginInvoke() or Invoke().

On disposing of this class (exiting our app), we dispose the control and abort the thread.

My question is, are there any problems with this? Is there a better way to handle this?

Is there a better built in way to work with an ActiveX control from within an unknown multi-threaded environment? We are trying to write our class in a way that wraps the control but will work from any thread.

UPDATE: As an answer suggested, we really would rather use the standard COM object and not use a control at all. Our issue with that was we would get the error "(Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)" upon the first method or property we call on the COM object. This is a pretty generic error that we don't get when using the ActiveX, any ideas?

UPDATE: Our ocx is "CX25.ocx", using tlbimp.exe we get CX25Lib.dll. Using aximp.exe, we get AxCX25Lib.dll and CX25Lib.dll. CX25Lib.dll does not work in either case. AxCX25Lib.dll works.

c#
.net
winforms
activex
asked on Stack Overflow Oct 7, 2009 by jonathanpeppers • edited Dec 15, 2015 by Irshad

5 Answers

18

I'm assuming this is the proper way to go about this.

We've been using my code above in test environments for the past few weeks with no issues.

If anyone has to use an ActiveX without a form, I assume this is one way to do it.

Just make sure to call _yourActiveXControl.CreateControl() directly after your ActiveX object's constructor. This simplified many issues we had originally.

answered on Stack Overflow Oct 13, 2009 by jonathanpeppers
3

If you are calling the ActiveX control from a business layer, that means that it must be able to be used without a UI, e.g. just by calling its public methods. Why not just create an interop RCW for the ActiveX control class and call its methods directly?

answered on Stack Overflow Oct 7, 2009 by Christian Hayter
2

My solution is to create a hidden winform that host the activex control

answered on Stack Overflow Dec 1, 2009 by Benny
2

I know this is an old post, but I would recommend using the TPL in our modern era.

It's better to use the task parallel library instead of the old threading API because of the features around exception handling, cancellation, continuation, and returning results.

Here's an example:

using (var sta = new StaTaskScheduler(1))
{    
    var taskResult = await Task.Factory.StartNew(() =>
    {
        var results = new List<ResultType>();

        using (var ax = new MyActiveXType())
        {
            // important to call this just after constructing ActiveX type
            ax.CreateControl();

            ax.SomeIterativeEvent += (s, e) => results.Add(e.SomeThing);

            // if applicable, you can tear down the message pump
            ax.SomeFinalEvent += (s, e) => Application.ExitThread();

            //more initialize work

            // start message pump
            Application.Run();

            return results;
        }
    }, CancellationToken.None, TaskCreationOptions.None, sta);

    return taskResult;
}

Some points:

  1. StaTaskScheduler is a type found in the ParallelExtensionsExtras nuget package. You'll need this to schedule tasks to execute in a Single Threaded Apartment.

  2. I'm passing 1 to the constructor of StaTaskScheduler so that it only ever creates a single thread for me.

  3. Application.ExitThread() is called to stop the message pump, which in turn allows execution to pass by Application.Run() so that some result can be returned to the caller.

answered on Stack Overflow Jul 8, 2014 by Ronnie Overby
1

The CreateControl() method is from AxHost and requires System.Windows.Forms as a dependency. If you want to use ActiveX without UI you can directly create COM object of ocx using native call.

   [DllImport("ole32.dll", PreserveSig = false)]
    [return: MarshalAs(UnmanagedType.Interface)]
    public static extern object CoCreateInstance([In] ref Guid clsid,[MarshalAs(UnmanagedType.Interface)] object punkOuter,int context, [In] ref Guid iid);


public object createComObject(){ 
    Guid IID_IUnknown = new Guid("{00000000-0000-0000-C000-000000000046}");

    var gid = "{6bf52a52-394a-11d3-b153-00c04f79faa6}"; //your ocx guid
    var clsid = new Guid(gid);

    object yourOCX = CoCreateInstance(ref clsid, (object)null, 1, ref IID_IUnknown);
  return yourOCX ;
}

You can later cast the COM object to required interfaces

IOleObject iole = yourOCX as IOleObject;

IWMPCore iwmp =  yourOCX as IWMPCore;

I have created Windows Media Player ActiveX without UI or AxHost in C# over this link. It might help someone trying to run ActiveX without UI.

answered on Stack Overflow Jun 20, 2019 by Vinit Siriah

User contributions licensed under CC BY-SA 3.0