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
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

internal partial class Interop
{
internal static class CLSID
{
// C0B4E2F3-BA21-4773-8DBA-335EC946EB8B
internal static Guid FileSaveDialog = new Guid(0xC0B4E2F3, 0xBA21, 0x4773, 0x8D, 0xBA, 0x33, 0x5E, 0xC9, 0x46, 0xEB, 0x8B);

// DC1C5A9C-E88A-4DDE-A5A1-60F82A20AEF7
internal static Guid FileOpenDialog = new Guid(0xDC1C5A9C, 0xE88A, 0x4DDE, 0xA5, 0xA1, 0x60, 0xF8, 0x2A, 0x20, 0xAE, 0xF7);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,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 NativeMethods.ActiveX.IID_IUnknown,
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 Down Expand Up @@ -139,7 +140,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 NativeMethods.ActiveX.IID_IUnknown,
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 Down Expand Up @@ -151,6 +152,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.FileSaveDialog,
IntPtr.Zero,
Ole32.CLSCTX.INPROC_SERVER | Ole32.CLSCTX.LOCAL_SERVER | Ole32.CLSCTX.REMOTE_SERVER,
ref NativeMethods.ActiveX.IID_IUnknown,
out object obj);
if (!hr.Succeeded())
{
Marshal.ThrowExceptionForHR((int)hr);
}

return (IFileSaveDialog)obj;
}
}
}