[Interest] The calling thread must be STA, because many UI components require this.

Constantin Makshin cmakshin at gmail.com
Fri Apr 3 08:14:07 CEST 2015


To set the thread as STA, initialize COM by calling either
CoInitialize(nullptr)
or
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)

But remember at least two things:
1) only the first call to CoInitialize[Ex]() matters — all subsequent
ones will fail unless they're told to set the same apartment type as the
one currently used by the calling thread;
2) every call to CoInitialize[Ex]() that returns S_OK or S_FALSE must be
paired with a call to CoUninitialize().

https://msdn.microsoft.com/en-us/library/windows/desktop/ms680112(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms678543(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms695279(v=vs.85).aspx

On 04/01/2015 02:11 PM, prasad v n wrote:
> hi,
> I wanted to host a WPF user control in my QT application.
> So I created a CLI dll that returns the HWND of the user control. It
> creates an HwndSource for the WPF control and returns the handle of that
> HWND source.
> 
> I added reference to this CLI dll from my QT application and tried to
> get the HWND.
> 
> But I am getting an error as given below.
> 
> 
> Unhandled Exception: System.InvalidOperationException: The calling
> thread must be STA, because many UI components require this.
> 
> at System.Windows.Input.InputManager..ctor()
> 
> at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
> 
> at System.Windows.Interop.HwndMouseInputProvider..ctor(HwndSource source)
> 
> at System.Windows.Interop.HwndSource.Initialize(HwndSourceParameters
> parameters)
> 
> at System.Windows.Interop.HwndSource..ctor(Int32 classStyle, Int32
> style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height,
> String name, IntPtr parent)
> 
> at GetWPFHwnd(HWND__* parent, Int32 x, Int32 y, Int32 width, Int32
> height) in
> c:\users\ing08746\desktop\qt\staticclilib\staticclilib\staticclilib.cpp:line
> 42
> 
> at WinMainCRTStartup()
> 
> 
> I think we need to set the ApartmentState of QT application as STA.
> 
> How can  set the apartment state to STA for QT application?
> 
> 
> with regards,
> 
> prasad

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150403/e1ffe201/attachment.sig>


More information about the Interest mailing list