Пример #1
0
        /// <summary>
        /// Explicitly load a manifest and create the process-default activation 
        /// context. It takes effect immediately and stays there until the process exits. 
        /// </summary>
        static public void CreateActivationContext()
        {
            string rootFolder = AppDomain.CurrentDomain.BaseDirectory;
            string manifestPath = Path.Combine(rootFolder, "webapp.manifest");
            UInt32 dwError = 0;

            // Build the activation context information structure 
            ACTCTX info = new ACTCTX();
            info.cbSize = Marshal.SizeOf(typeof(ACTCTX));
            info.dwFlags = ACTCTX_FLAG_SET_PROCESS_DEFAULT;
            info.lpSource = manifestPath;
            if (null != rootFolder && "" != rootFolder)
            {
                info.lpAssemblyDirectory = rootFolder;
                info.dwFlags |= ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;
            }

            dwError = 0;

            // Create the activation context 
            IntPtr result = CreateActCtx(ref info);
            if (-1 == result.ToInt32())
            {
                dwError = (UInt32)Marshal.GetLastWin32Error();
            }

            if (-1 == result.ToInt32() && ActivationContext.ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET != dwError)
            {
                string err = string.Format("Cannot create process-default win32 sxs context, error={0} manifest={1}", dwError, manifestPath);
                ApplicationException ex = new ApplicationException(err);
                throw ex;
            }
        }
Пример #2
0
        /// <summary>
        /// Explicitly load a manifest and create the process-default activation
        /// context. It takes effect immediately and stays there until the process exits.
        /// </summary>
        static public void CreateActivationContext()
        {
            string rootFolder   = AppDomain.CurrentDomain.BaseDirectory;
            string manifestPath = Path.Combine(rootFolder, "webapp.manifest");
            UInt32 dwError      = 0;

            // Build the activation context information structure
            ACTCTX info = new ACTCTX();

            info.cbSize   = Marshal.SizeOf(typeof(ACTCTX));
            info.dwFlags  = ACTCTX_FLAG_SET_PROCESS_DEFAULT;
            info.lpSource = manifestPath;
            if (null != rootFolder && "" != rootFolder)
            {
                info.lpAssemblyDirectory = rootFolder;
                info.dwFlags            |= ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;
            }

            dwError = 0;

            // Create the activation context
            IntPtr result = CreateActCtx(ref info);

            if (-1 == result.ToInt32())
            {
                dwError = (UInt32)Marshal.GetLastWin32Error();
            }

            if (-1 == result.ToInt32() && ActivationContext.ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET != dwError)
            {
                string err = string.Format("Cannot create process-default win32 sxs context, error={0} manifest={1}", dwError, manifestPath);
                ApplicationException ex = new ApplicationException(err);
                throw ex;
            }
        }
Пример #3
0
        private static bool EnsureActivateContextCreated()
        {
            lock (typeof(EnableThemingInScope))
            {
                if (!contextCreationSucceeded)
                {
                    // Pull manifest from the .NET Framework install directory.
                    string           assemblyLoc = null;
                    FileIOPermission fiop        = new FileIOPermission(PermissionState.None)
                    {
                        AllFiles = FileIOPermissionAccess.PathDiscovery
                    };
                    fiop.Assert();

                    try
                    {
                        assemblyLoc = typeof(Object).Assembly.Location;
                    }
                    finally
                    {
                        CodeAccessPermission.RevertAssert();
                    }

                    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 ACTCTX();
                        enableThemingActivationContext.cbSize   = Marshal.SizeOf(typeof(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             = ACTCTX_FLAGS.ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;

                        // Note this will fail gracefully if file specified
                        // by manifestLoc doesn't exist.
                        hActCtx = CreateActCtx(ref enableThemingActivationContext);
                        contextCreationSucceeded = (hActCtx != new IntPtr(-1));
                    }
                }

                // If we return false, we'll try again on the next call into
                // EnsureActivateContextCreated(), which is fine.
                return(contextCreationSucceeded);
            }
        }
            // Static constructor, make sure common controls activation context is created
            static VisualStyles()
            {
                var actctx = new ACTCTX();

                actctx.cbSize         = System.Runtime.InteropServices.Marshal.SizeOf(typeof(ACTCTX));
                actctx.lpSource       = Assembly.GetExecutingAssembly().Location;
                actctx.dwFlags        = ACTCTX_RESOURCE_NAME_VALID;
                actctx.lpResourceName = 2;
                hActCtxCommonControls = CreateActCtx(ref actctx);
            }
Пример #5
0
		static Context()
		{
			ACTCTX actctx = new ACTCTX();
			actctx.cbSize = Marshal.SizeOf(typeof(ACTCTX));
			actctx.lpSource = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "XPThemes.manifest");
			IntPtr handle = CreateActCtx(ref actctx);
			if (handle != new IntPtr(-1))
			{
				hActCtx = handle;
			}
		}
Пример #6
0
 public ActivationContext(string source = null)
 {
     if (source == null)
     {
         GetCurrentActCtx(out hActCtx);
     }
     else
     {
         var actctx = new ACTCTX(source);
         Create(ref actctx);
     }
     Activate();
 }
Пример #7
0
        static Context()
        {
            ACTCTX actctx = new ACTCTX();

            actctx.cbSize   = Marshal.SizeOf(typeof(ACTCTX));
            actctx.lpSource = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "XPThemes.manifest");
            IntPtr handle = CreateActCtx(ref actctx);

            if (handle != new IntPtr(-1))
            {
                hActCtx = handle;
            }
        }
Пример #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ComCtl32v6Context"/> class.
        /// </summary>
        public ComCtl32v6Context()
        {
            if (Environment.OSVersion.Version.Major < 6)
            {
                return;
            }
            var actctx = new ACTCTX(GetType().Assembly.Location)
            {
                dwFlags        = ActCtxFlags.ACTCTX_FLAG_RESOURCE_NAME_VALID,
                lpResourceName = "#2"
            };

            Create(ref actctx);
            Activate();
        }
Пример #9
0
        bool EnsureActivateContextCreated()
        {
            lock (typeof(EnableThemingInScope))
            {
                if (contextCreationSucceeded != null)
                {
                    return(contextCreationSucceeded.Value);
                }

                // Use a custom manifest from resources and write it to a temp file
                var manifestLoc = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                try
                {
                    var stream = typeof(EnableThemingInScope).Assembly.GetManifestResourceStream("Eto.Wpf.XPThemes.manifest");
                    if (stream == null)
                    {
                        return(false);
                    }

                    using (var fs = File.Create(manifestLoc))
                    {
                        stream.CopyTo(fs);
                    }

                    if (manifestLoc != null)
                    {
                        enableThemingActivationContext          = new ACTCTX();
                        enableThemingActivationContext.cbSize   = Marshal.SizeOf(typeof(ACTCTX));
                        enableThemingActivationContext.lpSource = manifestLoc;

                        // Note this will fail gracefully if file specified
                        // by manifestLoc doesn't exist.
                        hActCtx = CreateActCtx(ref enableThemingActivationContext);
                        contextCreationSucceeded = (hActCtx != new IntPtr(-1));
                    }
                }
                finally
                {
                    if (File.Exists(manifestLoc))
                    {
                        File.Delete(manifestLoc);
                    }
                }

                return(contextCreationSucceeded.Value);
            }
        }
Пример #10
0
        public static bool EnableThemingInWindows()
        {
            ACTCTX actctx = new ACTCTX();

            actctx.cbSize   = Marshal.SizeOf(actctx);
            actctx.dwFlags  = 28;
            actctx.lpSource = "shell32.dll";
            actctx.wProcessorArchitecture = 0;
            actctx.wLangId             = 0;
            actctx.lpAssemblyDirectory = Environment.SystemDirectory;
            actctx.lpResourceName      = (IntPtr)124;

            IntPtr c   = CreateActCtxA(ref actctx);
            bool   suc = ActivateActCtx(c, IntPtr.Zero);

            return(suc);
        }
Пример #11
0
        /// <summary>
        /// Applies content of <paramref name="pathToManifest"/> file to current context, invokes <paramref name="thingToDo"/> delegate, deactivates applied context.
        /// </summary>
        /// <param name="pathToManifest"></param>
        /// <param name="thingToDo"></param>
        /// <exception cref="FileNotFoundException"></exception>
        public static void UsingManifestDo(string pathToManifest, doSomething thingToDo)
        {
            var context = new ACTCTX();

            context.cbSize = Marshal.SizeOf(typeof(ACTCTX));
            bool wrongContextStructure = (context.cbSize != 0x20 && IntPtr.Size == 4) || // ensure stucture is right on 32 bits
                                         (context.cbSize != 52 && IntPtr.Size == 8); // the same for 64 bits

            if (wrongContextStructure)
            {
                throw new Exception("ACTCTX.cbSize is wrong");
            }
            context.lpSource = pathToManifest;

            IntPtr hActCtx = NativeMethods.CreateActCtx(ref context);

            if (hActCtx == Constants.INVALID_HANDLE_VALUE)
            {
                var error = Marshal.GetLastWin32Error();
                if (error == SYSTEM_ERROR_CODES.ERROR_FILE_NOT_FOUND)
                {
                    throw new System.IO.FileNotFoundException("Failed to find manifest", pathToManifest);
                }
                throw new Win32Exception(error);
            }
            try // with valid hActCtx
            {
                IntPtr cookie = IntPtr.Zero;
                if (!NativeMethods.ActivateActCtx(hActCtx, out cookie))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
                try // with activated context
                {
                    thingToDo();
                }
                finally
                {
                    NativeMethods.DeactivateActCtx(0, cookie);
                }
            }
            finally
            {
                NativeMethods.ReleaseActCtx(hActCtx);
            }
        }
Пример #12
0
 public ActivationContext(string source = null, string assemblyDirectory = null)
 {
     if (source == null)
     {
         GetCurrentActCtx(out hActCtx);
     }
     else
     {
         var actctx = new ACTCTX(source);
         if (assemblyDirectory != null)
         {
             actctx.lpAssemblyDirectory = assemblyDirectory;
             actctx.dwFlags            |= ActCtxFlags.ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;
         }
         Create(ref actctx);
     }
     Activate();
 }
            public ActivationContext(string assemblyName)
            {
                var requestedActivationContext = new ACTCTX
                {
                    cbSize   = Marshal.SizeOf <ACTCTX>(),
                    lpSource = assemblyName
                };

                _activationContext = CreateActCtxW(ref requestedActivationContext);
                if (_activationContext != INVALID_HANDLE_VALUE)
                {
                    if (!ActivateActCtx(_activationContext, out _activationContextCookie))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
                else
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
Пример #14
0
        public void ActivateActCtxTest()
        {
            var ctx = new ACTCTX(@"C:\Users\dahall\Documents\GitHubRepos\TaskScheduler\TaskSchedulerMockup\app.manifest");

            using (var hctx = CreateActCtx(ctx))
            {
                Assert.That(hctx, ResultIs.ValidHandle);
                Assert.That(ActivateActCtx(hctx, out var cookie), ResultIs.Successful);

                Assert.That(() => AddRefActCtx(hctx), Throws.Nothing);
                Assert.That(() => ReleaseActCtx(hctx), Throws.Nothing);

                Assert.That(GetCurrentActCtx(out var hctx2), ResultIs.Successful);
                Assert.That(hctx2, ResultIs.ValidHandle);

                Assert.That(RegisterApplicationRestart("cmd.exe", ApplicationRestartFlags.RESTART_NO_REBOOT), ResultIs.Successful);
                var sb = new StringBuilder(1024, 1024);
                var sz = (uint)sb.Capacity;
                Assert.That(GetApplicationRestartSettings(GetCurrentProcess(), sb, ref sz, out var rFlags), ResultIs.Successful);
                Assert.That(sb.ToString(), Is.EqualTo("cmd.exe"));
                Assert.That(UnregisterApplicationRestart(), ResultIs.Successful);

                Assert.That(RegisterApplicationRecoveryCallback(callback, default, 0), ResultIs.Successful);
Пример #15
0
		protected bool EnsureActivateContextCreated(string manifestFileFullPath)
		{
			lock (typeof(WsActivationContext))
			{
				if (!ContextCreationSucceeded)
				{
					if (!string.IsNullOrEmpty(manifestFileFullPath))
					{
						m_activationContext = new ACTCTX();
						m_activationContext.cbSize = Marshal.SizeOf(typeof(ACTCTX));
						m_activationContext.lpSource = manifestFileFullPath;
						m_activationContext.lpAssemblyDirectory = Path.GetDirectoryName(manifestFileFullPath);
						m_activationContext.dwFlags = ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;

						// Note this will fail gracefully if file specified doesn't exist.
						m_hActCtx = CreateActCtx(ref m_activationContext);
						ContextCreationSucceeded = (m_hActCtx != new IntPtr(-1));
					}
				}

				// If we return false, we'll try again on the next call into
				// EnsureActivateContextCreated(), which is fine.
				return ContextCreationSucceeded;
			}
		}
Пример #16
0
            public static bool CreateActivationContext(string dllPath, int nativeResourceManifestID)
            {
                lock (typeof(ThemingScope))
                {
                    if (!contextCreationSucceeded && OSFeature.Feature.IsPresent(OSFeature.Themes))
                    {

                        enableThemingActivationContext = new ACTCTX();

                        enableThemingActivationContext.cbSize = Marshal.SizeOf(typeof(ACTCTX));
                        enableThemingActivationContext.lpSource = dllPath;
                        enableThemingActivationContext.lpResourceName = (IntPtr)nativeResourceManifestID;
                        enableThemingActivationContext.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID;

                        hActCtx = CreateActCtx(ref enableThemingActivationContext);
                        contextCreationSucceeded = (hActCtx != new IntPtr(-1));
                    }

                    return contextCreationSucceeded;
                }
            }
Пример #17
0
 public extern static ActivationContextSafeHandle CreateActCtx(ref ACTCTX actctx);
 private static extern IntPtr CreateActCtx(ref ACTCTX actctx);
 public extern static IntPtr CreateActCtx(ref ACTCTX actctx);
 public static extern IntPtr CreateActCtxW(ref ACTCTX pActCtx);
Пример #21
0
 private extern static IntPtr CreateActCtxA(ref ACTCTX actctx);
Пример #22
0
 public static extern bool GetCurrentActCtx(ref ACTCTX pActCtx);
Пример #23
0
		extern static IntPtr CreateActCtx(ref ACTCTX actctx);
Пример #24
0
 public static extern IntPtr CreateActCtxA(ref ACTCTX pActCtx);
Пример #25
0
 public static extern ActCtxSafeHandle CreateActCtx(ref ACTCTX actctx);
Пример #26
0
 internal static extern IntPtr CreateActCtx(ref ACTCTX pActCtx);
 public static bool CreateActivationContext(string dllPath, int nativeResourceManifestID)
 {
     lock (typeof(System.Windows.Forms.UnsafeNativeMethods.ThemingScope))
     {
         if (!contextCreationSucceeded && OSFeature.Feature.IsPresent(OSFeature.Themes))
         {
             enableThemingActivationContext = new ACTCTX();
             enableThemingActivationContext.cbSize = Marshal.SizeOf(typeof(ACTCTX));
             enableThemingActivationContext.lpSource = dllPath;
             enableThemingActivationContext.lpResourceName = (IntPtr) nativeResourceManifestID;
             enableThemingActivationContext.dwFlags = 8;
             hActCtx = CreateActCtx(ref enableThemingActivationContext);
             contextCreationSucceeded = hActCtx != new IntPtr(-1);
         }
         return contextCreationSucceeded;
     }
 }
Пример #28
0
 public static extern IntPtr CreateActCtx(ref ACTCTX actctx);
Пример #29
0
 public ActivationContext(ref ACTCTX context)
 {
     Create(ref context); Activate();
 }
Пример #30
0
 public static extern ActivationContextSafeHandle CreateActCtx(ref ACTCTX actctx);
 private static extern IntPtr CreateActCtx( ref ACTCTX actctx );
        private static bool EnsureActivateContextCreated()
        {
            lock ( typeof( EnableThemingInScope ) ) {
                if ( !contextCreationSucceeded ) {
                    // Pull manifest from the .NET Framework install
                    // directory

                    string assemblyLoc = null;

                    FileIOPermission fiop = new FileIOPermission( PermissionState.None );
                    fiop.AllFiles = FileIOPermissionAccess.PathDiscovery;
                    fiop.Assert();
                    try {
                        assemblyLoc = typeof( Object ).Assembly.Location;
                    } finally {
                        CodeAccessPermission.RevertAssert();
                    }

                    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 ACTCTX();
                        enableThemingActivationContext.cbSize = Marshal.SizeOf( typeof( 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 = ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;

                        // Note this will fail gracefully if file specified
                        // by manifestLoc doesn't exist.
                        hActCtx = CreateActCtx( ref enableThemingActivationContext );
                        contextCreationSucceeded = (hActCtx != new IntPtr( -1 ));
                    }
                }

                // If we return false, we'll try again on the next call into
                // EnsureActivateContextCreated(), which is fine.
                return contextCreationSucceeded;
            }
        }
 internal extern static IntPtr CreateActCtx(ref ACTCTX actctx);
Пример #34
0
 private void Create(ref ACTCTX context)
 {
     hActCtx = CreateActCtx(ref context);
 }
Пример #35
0
 internal static extern IntPtr CreateActCtx(ref ACTCTX pActCtx);