/
ComCtlv6ActivationContext.cs
108 lines (95 loc) · 3.92 KB
/
ComCtlv6ActivationContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Security;
using System.IO;
using System.Runtime.InteropServices;
namespace Ookii.Dialogs.Wpf
{
sealed class ComCtlv6ActivationContext : IDisposable
{
// Private data
private IntPtr _cookie;
private static NativeMethods.ACTCTX _enableThemingActivationContext;
private static ActivationContextSafeHandle _activationContext;
private static bool _contextCreationSucceeded;
private static readonly object _contextCreationLock = new object();
public ComCtlv6ActivationContext(bool enable)
{
if( enable && NativeMethods.IsWindowsXPOrLater )
{
if( EnsureActivateContextCreated() )
{
if( !NativeMethods.ActivateActCtx(_activationContext, out _cookie) )
{
// Be sure cookie always zero if activation failed
_cookie = IntPtr.Zero;
}
}
}
}
~ComCtlv6ActivationContext()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if( _cookie != IntPtr.Zero )
{
if( NativeMethods.DeactivateActCtx(0, _cookie) )
{
// deactivation succeeded...
_cookie = IntPtr.Zero;
}
}
}
private static bool EnsureActivateContextCreated()
{
lock( _contextCreationLock )
{
if( !_contextCreationSucceeded )
{
// Pull manifest from the .NET Framework install
// directory
string assemblyLoc = null;
assemblyLoc = typeof(Object).Assembly.Location;
string manifestLoc = null;
string installDir = null;
if( assemblyLoc != null )
{
installDir = Path.GetDirectoryName(assemblyLoc);
const string manifestName = "XPThemes.manifest";
manifestLoc = Path.Combine(installDir, manifestName);
}
if( manifestLoc != null && installDir != null )
{
_enableThemingActivationContext = new NativeMethods.ACTCTX();
_enableThemingActivationContext.cbSize = Marshal.SizeOf(typeof(NativeMethods.ACTCTX));
_enableThemingActivationContext.lpSource = manifestLoc;
// Set the lpAssemblyDirectory to the install
// directory to prevent Win32 Side by Side from
// looking for comctl32 in the application
// directory, which could cause a bogus dll to be
// placed there and open a security hole.
_enableThemingActivationContext.lpAssemblyDirectory = installDir;
_enableThemingActivationContext.dwFlags = NativeMethods.ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;
// Note this will fail gracefully if file specified
// by manifestLoc doesn't exist.
_activationContext = NativeMethods.CreateActCtx(ref _enableThemingActivationContext);
_contextCreationSucceeded = !_activationContext.IsInvalid;
}
}
// If we return false, we'll try again on the next call into
// EnsureActivateContextCreated(), which is fine.
return _contextCreationSucceeded;
}
}
}
}