C # add-in for application (via COM) freezes when control is added to form?

I am developing an extension for an existing application through COM.

The current application extension interface allows you to create custom property windows and use them inside this application.


Now I use .NET for this purpose and have weird problems:

  extensionForm = new Form(); extensionForm.SetBounds(0, 0, 100, 100); extensionForm.Controls.Add(new Button()); ExApplAPI.AddCustomPropertyWindow(extensionForm.Handle.ToInt32(), "Ololo"); 

As you can see below, the property sheets actually expand, but after that something strange happens.

enter image description here

Basically, if I go to the Ololo tab and then return to any of the three tabs ( Attributes , Drawing or Services ), the application freezes. I also know that freezing occurs inside some unmanaged block of code.


Another interesting fact is that if I do not write extensionForm.Controls.Add(new Button()) (with or without Suspend / Resume Layout calls), everything works fine. So, if a newly built form has no controls (buttons or any other) on it, it does not freeze.

Here is the Spy++ log in the Ololo window right before the freeze (the last message is WM_CTLCOLORBTN , immediately after that the application freezes):

enter image description here


Putting it all together:

  • Freezing occurs only if I switch from Ololo to another tab and then switch back to the Ololo tab.
  • Freezing occurs only if the integrated form has at least one control; forms without controls do not freeze.
  • Currently, the application does not manage any managed code and does not waste CPU time.

So - any ideas / similar problems that / etc have been resolved to help me in this case?

+8
c # winapi com
source share
4 answers

Win32 HWND processing for forms in .NET is initialized lazily. And I think this may be a problem here.

You can claim that the handle is created in your line ExApplAPI.AddCustomPropertyWindow(extensionForm.Handle.ToInt32(), "Ololo"); due to access to the Handle property. This is true and that the documentation confirms.

However, it creates a handle to the Form itself, but no handles for child controls ( Button in this case) are created. This may be caused by calling the CreateControl method. See more documentation .

I don’t know if there might be a reason for your problem with the knob for the button, but this is definitely what I will investigate.

To summarize, I suggest changing your code to:

 extensionForm = new Form(); extensionForm.SetBounds(0, 0, 100, 100); extensionForm.Controls.Add(new Button()); extensionForm.CreateControl(); ExApplAPI.AddCustomPropertyWindow(extensionForm.Handle.ToInt32(), "Ololo"); 
+4
source share

Is there any exception? We had a similar behavior when using WPF and COM, it was solved by double-calculating reset using

[DllImport ("msvcr70.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int _fpreset ();

+1
source share

It is possible that the resource descriptor is invalid. As you mentioned, this only happens when the integrated form has at least one control in it, the Ololo tab cannot find resources when working actively. Try to save the resource descriptor first, and then restore it every time the tab is active.

0
source share

To understand why the application freezes, there are 2 things that can help:

  • Can you post a trace of the user interface thread stack while the application hangs?
  • Which thread calls your code and actually creates windows?
0
source share

All Articles