Create an Ax file in a dll?


S

Steven Blair

Hi,

I have a requirement to use an ocx file in my c#.
If I add the registered control through the toolbar in a Windows form, i
can easily use the object. Visual Studio creates a File in the
References called AxNameLib and I use this like any other object.

However, I need to be able to do the same thing from a dll.
I added the newly created file into a DLL but when I attempt to run the
library I get the following error:

ActiveX control 'b92e478a-add0-40b5-895e-f0d95c81609c' cannot be
instantiated because the current thread is not in a single-threaded
apartment.

If this is not possible to be run in a dll, do you think it would be
possible for me create an instance of the form from my DLL (not
displaying) and access it this way?

Thanks in advance,

Steven
 
Ad

Advertisements

P

Peter Duniho

Steven said:
Hi,

I have a requirement to use an ocx file in my c#.
If I add the registered control through the toolbar in a Windows form, i
can easily use the object. Visual Studio creates a File in the
References called AxNameLib and I use this like any other object.

However, I need to be able to do the same thing from a dll.
I added the newly created file into a DLL but when I attempt to run the
library I get the following error:

ActiveX control 'b92e478a-add0-40b5-895e-f0d95c81609c' cannot be
instantiated because the current thread is not in a single-threaded
apartment. [...]

It works in the application only because you are doing it in a
particular kind of application and creating the object on a particular
thread in that application (i.e. it's a Forms or WPF application and
you're creating the object in the main thread of the application).

If you're writing a DLL, you can either delegate the STA thread
requirement to the client code — that is, require the client code to
call your DLL code from an STA thread, or at least call your DLL code
that involves the OCX control that way — or you can have your DLL create
a dedicated STA thread used for the purpose of creating and managing the
OCX COM objects that require one.

If taking the latter approach, remember that you can use the
Thread.SetApartmentState() method to set the apartment state for a
thread, but that it needs to be called _before_ you start the thread by
calling the Thread.Start() method. Also, remember that the thread used
to create the OCX object will have to keep running until the OCX object
itself is no longer needed.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top