Skip to content

Commit

Permalink
Use Microsoft.Windows.CsWin32 to generate P/Invoke methods
Browse files Browse the repository at this point in the history
  • Loading branch information
virzak committed Feb 4, 2022
1 parent edf9e7a commit 886846b
Show file tree
Hide file tree
Showing 23 changed files with 760 additions and 1,888 deletions.
4 changes: 2 additions & 2 deletions src/Ookii.Dialogs.Wpf/AnimationResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public static AnimationResource GetShellAnimation(ShellAnimation animation)
return new AnimationResource("shell32.dll", (int)animation);
}

internal SafeModuleHandle LoadLibrary()
internal FreeLibrarySafeHandle LoadLibrary()
{
SafeModuleHandle handle = NativeMethods.LoadLibraryEx(ResourceFile, IntPtr.Zero, NativeMethods.LoadLibraryExFlags.LoadLibraryAsDatafile);
var handle = NativeMethods.LoadLibraryEx(ResourceFile, default, Windows.Win32.System.LibraryLoader.LOAD_LIBRARY_FLAGS.LOAD_LIBRARY_AS_DATAFILE);
if( handle.IsInvalid )
{
int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
Expand Down
30 changes: 18 additions & 12 deletions src/Ookii.Dialogs.Wpf/ComCtlv6ActivationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ namespace Ookii.Dialogs.Wpf
sealed class ComCtlv6ActivationContext : IDisposable
{
// Private data
private IntPtr _cookie;
private static NativeMethods.ACTCTX _enableThemingActivationContext;
private static ActivationContextSafeHandle _activationContext;
private nuint _cookie;
private static ACTCTXW _enableThemingActivationContext;
private static SafeFileHandle _activationContext;
private static bool _contextCreationSucceeded;
private static readonly object _contextCreationLock = new object();

Expand All @@ -35,10 +35,10 @@ public ComCtlv6ActivationContext(bool enable)
{
if( EnsureActivateContextCreated() )
{
if( !NativeMethods.ActivateActCtx(_activationContext, out _cookie) )
if( !NativeMethods.ActivateActCtx((HANDLE)_activationContext.DangerousGetHandle(), out _cookie) )
{
// Be sure cookie always zero if activation failed
_cookie = IntPtr.Zero;
_cookie = 0;
}
}
}
Expand All @@ -57,12 +57,12 @@ public void Dispose()

private void Dispose(bool disposing)
{
if( _cookie != IntPtr.Zero )
if( _cookie != 0 )
{
if( NativeMethods.DeactivateActCtx(0, _cookie) )
{
// deactivation succeeded...
_cookie = IntPtr.Zero;
_cookie = 0;
}
}
}
Expand Down Expand Up @@ -95,15 +95,21 @@ private static bool EnsureActivateContextCreated()
}
}

_enableThemingActivationContext = new NativeMethods.ACTCTX
unsafe
{
cbSize = Marshal.SizeOf(typeof(NativeMethods.ACTCTX)),
lpSource = manifestTempFilePath,
};
fixed (char* szManifestTempFilePath = manifestTempFilePath)
{
_enableThemingActivationContext = new ACTCTXW
{
cbSize = (uint)Marshal.SizeOf(typeof(ACTCTXW)),
lpSource = szManifestTempFilePath,
};
}
}

// Note this will fail gracefully if file specified
// by manifestFilePath doesn't exist.
_activationContext = NativeMethods.CreateActCtx(ref _enableThemingActivationContext);
_activationContext = NativeMethods.CreateActCtx(_enableThemingActivationContext);
_contextCreationSucceeded = !_activationContext.IsInvalid;

try
Expand Down
332 changes: 189 additions & 143 deletions src/Ookii.Dialogs.Wpf/CredentialDialog.cs

Large diffs are not rendered by default.

23 changes: 0 additions & 23 deletions src/Ookii.Dialogs.Wpf/Interop/COMGuids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,13 @@
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Ookii.Dialogs.Wpf.Interop
{
internal static class IIDGuid
{
internal const string IModalWindow = "b4db1657-70d7-485e-8e3e-6fcb5a5c1802";
internal const string IFileDialog = "42f85136-db7e-439c-85f1-e4075d135fc8";
internal const string IFileOpenDialog = "d57c7288-d4ad-4768-be02-9d969532d960";
internal const string IFileSaveDialog = "84bccd23-5fde-4cdb-aea4-af64b83d78ab";
internal const string IFileDialogEvents = "973510DB-7D7F-452B-8975-74A85828D354";
internal const string IFileDialogControlEvents = "36116642-D713-4b97-9B83-7484A9D00433";
internal const string IFileDialogCustomize = "e6fdd21a-163f-4975-9c8c-a69f1ba37034";
internal const string IShellItem = "43826D1E-E718-42EE-BC55-A1E261C37BFE";
internal const string IShellItemArray = "B63EA76D-1F85-456F-A19C-48159EFA858B";
internal const string IKnownFolder = "38521333-6A87-46A7-AE10-0F16706816C3";
internal const string IKnownFolderManager = "44BEAAEC-24F4-4E90-B3F0-23D258FBB146";
internal const string IPropertyStore = "886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99";
internal const string IProgressDialog = "EBBC7C04-315E-11d2-B62F-006097DF5BD4";
}

Expand All @@ -46,12 +31,4 @@ internal static class CLSIDGuid
internal const string KnownFolderManager = "4df0c730-df9d-4ae3-9153-aa6b82e9795a";
internal const string ProgressDialog = "F8383852-FCD3-11d1-A6B9-006097DF5BD4";
}

internal static class KFIDGuid
{
internal const string ComputerFolder = "0AC0837C-BBF8-452A-850D-79D08E667CA7";
internal const string Favorites = "1777F761-68AD-4D8A-87BD-30B759FA33DD";
internal const string Documents = "FDD39AD0-238F-46AF-ADB4-6C85480369C7";
internal const string Profile = "5E6C858F-0E22-4760-9AFE-EA3317B67173";
}
}
4 changes: 0 additions & 4 deletions src/Ookii.Dialogs.Wpf/Interop/ComDlgResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
//
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Ookii.Dialogs.Wpf.Interop
{
static class ComDlgResources
Expand Down
31 changes: 0 additions & 31 deletions src/Ookii.Dialogs.Wpf/Interop/ErrorHelper.cs

This file was deleted.

90 changes: 0 additions & 90 deletions src/Ookii.Dialogs.Wpf/Interop/IProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,94 +31,4 @@ internal class ProgressDialogRCW
internal interface ProgressDialog : IProgressDialog
{
}

[Flags]
internal enum ProgressDialogFlags : uint
{
Normal = 0x00000000,
Modal = 0x00000001,
AutoTime = 0x00000002,
NoTime = 0x00000004,
NoMinimize = 0x00000008,
NoProgressBar = 0x00000010,
MarqueeProgress = 0x00000020,
NoCancel = 0x00000040,
}

[Flags]
internal enum ProgressDialogTimerAction : uint
{
Reset = 0x00000001,
Pause = 0x00000002,
Resume = 0x00000003,
}

[ComImport]
[Guid(IIDGuid.IProgressDialog)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IProgressDialog
{

[PreserveSig]
void StartProgressDialog(
IntPtr hwndParent,
[MarshalAs(UnmanagedType.IUnknown)]
object punkEnableModless,
ProgressDialogFlags dwFlags,
IntPtr pvResevered
);

[PreserveSig]
void StopProgressDialog();

[PreserveSig]
void SetTitle(
[MarshalAs(UnmanagedType.LPWStr)]
string pwzTitle
);

[PreserveSig]
void SetAnimation(
SafeModuleHandle hInstAnimation,
ushort idAnimation
);

[PreserveSig]
[return: MarshalAs(UnmanagedType.Bool)]
bool HasUserCancelled();

[PreserveSig]
void SetProgress(
uint dwCompleted,
uint dwTotal
);
[PreserveSig]
void SetProgress64(
ulong ullCompleted,
ulong ullTotal
);

[PreserveSig]
void SetLine(
uint dwLineNum,
[MarshalAs(UnmanagedType.LPWStr)]
string pwzString,
[MarshalAs(UnmanagedType.VariantBool)]
bool fCompactPath,
IntPtr pvResevered
);

[PreserveSig]
void SetCancelMsg(
[MarshalAs(UnmanagedType.LPWStr)]
string pwzCancelMsg,
object pvResevered
);

[PreserveSig]
void Timer(
ProgressDialogTimerAction dwTimerAction,
object pvReserved
);
}
}
Loading

0 comments on commit 886846b

Please sign in to comment.