Udostępnij za pośrednictwem


How to: Support COM Interop by Displaying a Windows Form with the ShowDialog Method

You can resolve Component Object Model (COM) interoperability problems by displaying your Windows Form on a .NET Framework message loop, which is created by using the Application.Run method.

To make a form work correctly from a COM client application, you must run it on a Windows Forms message loop. To do this, use one of the following approaches:

Procedure

Using the Form.ShowDialog method can be the easiest way to display a form on a .NET Framework message loop because, of all the approaches, it requires the least code to implement.

The Form.ShowDialog method suspends the unmanaged application's message loop and displays the form as a dialog box. Because the host application's message loop has been suspended, the Form.ShowDialog method creates a new .NET Framework message loop to process the form's messages.

The disadvantage of using the Form.ShowDialog method is that the form will be opened as a modal dialog box. This behavior blocks any user interface (UI) in the calling application while the Windows Form is open. When the user exits the form, the .NET Framework message loop closes and the earlier application's message loop starts running again.

You can create a class library in Windows Forms which has a method to show the form, and then build the class library for COM interop. You can use this DLL file from Visual Basic 6.0 or Microsoft Foundation Classes (MFC), and from either of these environments you can call the Form.ShowDialog method to display the form.

To support COM interop by displaying a windows form with the ShowDialog method

See Also

Tasks

How to: Support COM Interop by Displaying Each Windows Form on Its Own Thread

Concepts

Exposing .NET Framework Components to COM

Other Resources

Windows Forms and Unmanaged Applications