Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

com

winapi

I am developing an extension for existing application via COM.

Current interface of the application to extend allows to create custom property windows and use them inside that application.


Now, I am using .NET for that purpose and have strange 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 get extended, but after that something strange starts to happen.

enter image description here

Basically, if I switch to Ololo tab, then back to any of other 3 tabs (Attributes, Drawing or Services), the application freezes. I also know that the freeze happens inside of some unmanaged code block.


Another interesting fact here is that if I don't write the extensionForm.Controls.Add(new Button()) (with or without the Suspend / Resume Layout calls), everything works fine. So, if the recently constructed form has no controls (buttons or any other) on it, it doesn't freeze.

Here is a Spy++ log on the Ololo window right before the freeze (last message is the WM_CTLCOLORBTN, right after that the application became frozen):

enter image description here


Combining everything together:

  • Freezing happens only if I switch from Ololo to some other tab and then switch to the Ololo tab again.
  • Freezing only happens if the integrated form has at least one control on it, forms without controls don't freeze.
  • Application is not running any managed code at the moment and is not spending any CPU time.

So - any ideas / similiar problems solved / etc to help me in this case?

like image 682
Yippie-Ki-Yay Avatar asked Jan 05 '12 05:01

Yippie-Ki-Yay


People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

C dalam Latin berapa?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).

Bahasa C dibuat pertama kali oleh siapa dan tahun berapa?

Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.


1 Answers

The Win32 HWND handles for the Forms in .NET are lazy initialized. And I think this may be a problem here.

You may argue that the handle is created in your line ExApplAPI.AddCustomPropertyWindow(extensionForm.Handle.ToInt32(), "Ololo"); due to accessing Handle property. It is true and what documentation acknowledges.

However, it creates the handle for the Form itself, but handles for child controls (Button in this case) are not created. This can be forced by calling CreateControl method. See more documentation.

I don't know if not having a handle for button may be a cause of your problem, but this is definitely something I would 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");
like image 121
Krizz Avatar answered Oct 25 '22 08:10

Krizz