Error creating window handle.

D

David Battams

Hi there,

Does anyone have any idea why a .Net form cached in a managed C++ static
variable would cause this error when trying to show the form?

System.ComponentModel.Win32Exception: Error creating window handle.
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.OnParentVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()

The story isn't quite so simple, so let me draw a picture...

I have a managed C++ class with two methods. InitializeApplication() and
StartApplication().
InitializeApplication() creates the form I wish to display, and
ShowApplication() shows it.
These two methods are called from unmanaged C++ by invoking the methods as
follows:

CComPtr<IDispatch> pDisp;
pDisp = GetDispatchInterface(spDefAppDomain);

OLECHAR FAR* szMember = _T("StartApplication");
DISPID dispid;
DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
pDisp->GetIDsOfNames (IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT,
&dispid);

pDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
&dispparamsNoArgs, NULL, NULL, NULL);

hence the need to cache the constructed form in a static variable before
showing it.

One last compilcation: InitializeApplication is called on a worker thread,
and StartApplication is called by the main application thread.

If I do this, I get the above error. If, however, I don't cache the form,
and just construct it in ShowApplication() it works fine, but that defeats
the purpose of saving time by using the background thread.

Any thoughts?

Wayne.
 
M

Max

This is most likely because of the two different threads.
There are a lot of Win32 API window manipulations that would fail if called
from different thread, not the on the window belongs to.
That is why, you should decide in what thread you want to have the window,
and when you are trying to manipulate it from other threads - use invoke
functions.
 

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