Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CoClass usages from System.Windows.Forms #4971

Merged
merged 10 commits into from
Jun 4, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ namespace System.Windows.Forms
[SRDescription(nameof(SR.DescriptionFolderBrowserDialog))]
public sealed class FolderBrowserDialog : CommonDialog
{
private static readonly Guid IID_IFileOpenDialog = new Guid("d57c7288-d4ad-4768-be02-9d969532d960");
private static readonly Guid CLSID_FileOpenDialog = new Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7");

// Root node of the tree view.
private Environment.SpecialFolder _rootFolder;

Expand Down Expand Up @@ -205,12 +208,23 @@ protected override bool RunDialog(IntPtr hWndOwner)

private bool TryRunDialogVista(IntPtr owner, out bool returnValue)
{
OpenFileDialog.NativeFileOpenDialog dialog;
IFileOpenDialog dialog;
try
{
// Creating the Vista dialog can fail on Windows Server Core, even if the
// Server Core App Compatibility FOD is installed.
dialog = new OpenFileDialog.NativeFileOpenDialog();
HRESULT hr = Ole32.CoCreateInstance(
ref CLSID_FileOpenDialog,
IntPtr.Zero,
Ole32.CLSCTX.INPROC_SERVER | Ole32.CLSCTX.LOCAL_SERVER | Ole32.CLSCTX.REMOTE_SERVER,
ref IID_IFileOpenDialog,
out object obj);
if (!hr.Succeeded())
{
Marshal.ThrowExceptionForHR((int)hr);
}

dialog = (IFileOpenDialog)obj;
}
catch (COMException)
{
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using static Interop;
using static Interop.Shell32;

Expand All @@ -19,6 +20,9 @@ namespace System.Windows.Forms
[SRDescription(nameof(SR.DescriptionOpenFileDialog))]
public sealed partial class OpenFileDialog : FileDialog
{
private static readonly Guid IID_IFileOpenDialog = new Guid("d57c7288-d4ad-4768-be02-9d969532d960");
private static readonly Guid CLSID_FileOpenDialog = new Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7");

/// <summary>
/// Gets or sets a value indicating whether the dialog box displays a
/// warning if the user specifies a file name that does not exist.
Expand Down Expand Up @@ -139,7 +143,21 @@ private protected override string[] ProcessVistaFiles(IFileDialog dialog)
}
}

private protected override IFileDialog CreateVistaDialog() => new NativeFileOpenDialog();
private protected override IFileDialog CreateVistaDialog()
{
HRESULT hr = Ole32.CoCreateInstance(
ref CLSID_FileOpenDialog,
IntPtr.Zero,
Ole32.CLSCTX.INPROC_SERVER | Ole32.CLSCTX.LOCAL_SERVER | Ole32.CLSCTX.REMOTE_SERVER,
ref IID_IFileOpenDialog,
out object obj);
if (!hr.Succeeded())
{
Marshal.ThrowExceptionForHR((int)hr);
}

return (IFileOpenDialog)obj;
}

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using static Interop;
using static Interop.Shell32;

Expand All @@ -22,6 +23,9 @@ namespace System.Windows.Forms
]
public sealed partial class SaveFileDialog : FileDialog
{
private static readonly Guid IID_IFileSaveDialog = new Guid("84bccd23-5fde-4cdb-aea4-af64b83d78ab");
private static readonly Guid CLSID_FileSaveDialogCoClass = new Guid("C0B4E2F3-BA21-4773-8DBA-335EC946EB8B");

/// <summary>
/// Gets or sets a value indicating whether the dialog box prompts the user for
/// permission to create a file if the user specifies a file that does not exist.
Expand Down Expand Up @@ -151,6 +155,20 @@ private protected override string[] ProcessVistaFiles(IFileDialog dialog)
return new string[] { GetFilePathFromShellItem(item) };
}

private protected override IFileDialog CreateVistaDialog() => new NativeFileSaveDialog();
private protected override IFileDialog CreateVistaDialog()
{
HRESULT hr = Ole32.CoCreateInstance(
ref CLSID_FileSaveDialogCoClass,
IntPtr.Zero,
Ole32.CLSCTX.INPROC_SERVER | Ole32.CLSCTX.LOCAL_SERVER | Ole32.CLSCTX.REMOTE_SERVER,
ref IID_IFileSaveDialog,
out object obj);
if (!hr.Succeeded())
{
Marshal.ThrowExceptionForHR((int)hr);
}

return (IFileSaveDialog)obj;
}
}
}