|
1 | 1 | using System;
|
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Linq; |
4 |
| -using System.Text; |
5 |
| -using System.Security.Permissions; |
6 |
| -using System.Security; |
7 | 2 | using System.IO;
|
8 | 3 | using System.Runtime.InteropServices;
|
9 | 4 |
|
@@ -58,45 +53,50 @@ private void Dispose(bool disposing)
|
58 | 53 |
|
59 | 54 | private static bool EnsureActivateContextCreated()
|
60 | 55 | {
|
61 |
| - lock( _contextCreationLock ) |
| 56 | + lock (_contextCreationLock) |
62 | 57 | {
|
63 |
| - if( !_contextCreationSucceeded ) |
| 58 | + if (_contextCreationSucceeded) |
64 | 59 | {
|
65 |
| - // Pull manifest from the .NET Framework install |
66 |
| - // directory |
67 |
| - |
68 |
| - string assemblyLoc = null; |
| 60 | + return _contextCreationSucceeded; |
| 61 | + } |
69 | 62 |
|
70 |
| - assemblyLoc = typeof(Object).Assembly.Location; |
| 63 | + const string manifestResourceName = "Ookii.Dialogs.XPThemes.manifest"; |
| 64 | + string manifestTempFilePath; |
71 | 65 |
|
72 |
| - string manifestLoc = null; |
73 |
| - string installDir = null; |
74 |
| - if( assemblyLoc != null ) |
| 66 | + using (var manifest = typeof(ComCtlv6ActivationContext).Assembly.GetManifestResourceStream(manifestResourceName)) |
| 67 | + { |
| 68 | + if (manifest is null) |
75 | 69 | {
|
76 |
| - installDir = Path.GetDirectoryName(assemblyLoc); |
77 |
| - const string manifestName = "XPThemes.manifest"; |
78 |
| - manifestLoc = Path.Combine(installDir, manifestName); |
| 70 | + throw new InvalidOperationException($"Unable to retrieve {manifestResourceName} embedded resource"); |
79 | 71 | }
|
80 | 72 |
|
81 |
| - if( manifestLoc != null && installDir != null ) |
| 73 | + manifestTempFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 74 | + |
| 75 | + using (var tempFileStream = new FileStream(manifestTempFilePath, FileMode.CreateNew, FileAccess.ReadWrite, |
| 76 | + FileShare.Delete | FileShare.ReadWrite)) |
82 | 77 | {
|
83 |
| - _enableThemingActivationContext = new NativeMethods.ACTCTX(); |
84 |
| - _enableThemingActivationContext.cbSize = Marshal.SizeOf(typeof(NativeMethods.ACTCTX)); |
85 |
| - _enableThemingActivationContext.lpSource = manifestLoc; |
| 78 | + manifest.CopyTo(tempFileStream); |
| 79 | + } |
| 80 | + } |
86 | 81 |
|
87 |
| - // Set the lpAssemblyDirectory to the install |
88 |
| - // directory to prevent Win32 Side by Side from |
89 |
| - // looking for comctl32 in the application |
90 |
| - // directory, which could cause a bogus dll to be |
91 |
| - // placed there and open a security hole. |
92 |
| - _enableThemingActivationContext.lpAssemblyDirectory = installDir; |
93 |
| - _enableThemingActivationContext.dwFlags = NativeMethods.ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID; |
| 82 | + _enableThemingActivationContext = new NativeMethods.ACTCTX |
| 83 | + { |
| 84 | + cbSize = Marshal.SizeOf(typeof(NativeMethods.ACTCTX)), |
| 85 | + lpSource = manifestTempFilePath, |
| 86 | + }; |
94 | 87 |
|
95 |
| - // Note this will fail gracefully if file specified |
96 |
| - // by manifestLoc doesn't exist. |
97 |
| - _activationContext = NativeMethods.CreateActCtx(ref _enableThemingActivationContext); |
98 |
| - _contextCreationSucceeded = !_activationContext.IsInvalid; |
99 |
| - } |
| 88 | + // Note this will fail gracefully if file specified |
| 89 | + // by manifestFilePath doesn't exist. |
| 90 | + _activationContext = NativeMethods.CreateActCtx(ref _enableThemingActivationContext); |
| 91 | + _contextCreationSucceeded = !_activationContext.IsInvalid; |
| 92 | + |
| 93 | + try |
| 94 | + { |
| 95 | + File.Delete(manifestTempFilePath); |
| 96 | + } |
| 97 | + catch (Exception) |
| 98 | + { |
| 99 | + // We tried to be tidy but something blocked us :( |
100 | 100 | }
|
101 | 101 |
|
102 | 102 | // If we return false, we'll try again on the next call into
|
|
0 commit comments