Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/com-interop-by-displaying-a-windows-form-shadow.md

3.4 KiB

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
How to: Support COM Interop by Displaying a Windows Form with the ShowDialog Method 03/30/2017
COM [Windows Forms]
Windows Forms, unmanaged
COM interop [Windows Forms], calling methods
ActiveX controls [Windows Forms], COM interop
Windows Forms, interop
87aac8ad-3c04-43b3-9b0c-d0b00df9ee74

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 xref:System.Windows.Forms.Application.Run%2A?displayProperty=nameWithType 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 xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType 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 xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType 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 xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType method creates a new .NET Framework message loop to process the form's messages.

The disadvantage of using the xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType 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 xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType method to display the form.

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

See also