Prev: Assembly Name
Next: MemberInfo derived classes
From: Steven Blair on 14 May 2010 05:26 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 *** Sent via Developersdex http://www.developersdex.com ***
From: Peter Duniho on 14 May 2010 11:20 Steven Blair wrote: > 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
|
Pages: 1 Prev: Assembly Name Next: MemberInfo derived classes |