2009-09-12 10 views
6

J'utilise le WindowsAPICodePack pour TaskDialog. Lorsque j'essaie d'afficher la boîte de dialogue, elle indique qu'elle doit charger la version 6 de comctl32.dll. J'ai donc ajouté la version 6 à app.manifest et j'ai essayé de l'exécuter. Toujours pas de chance. Je suis allé au dossier Déboguer et ai couru le programme sans Visual Studio et cela fonctionne très bien. Je devine que Visual Studio n'utilise pas le fichier manifeste ... Je me demandais s'il y avait un moyen de le faire faire.C#: comctl32.dll version 6 dans le débogueur

Répondre

9

Rob pol86, votre code lance SEHExceptions, car les signatures pour ActivateActCtx et DeactivateActCtx ne sont pas correctes. Vous devez utiliser UIntPtr au lieu de uint pour le lpCookie.

Par conséquent, le bon code pour EnableThemingInScope.cs serait:

using System; 
using System.IO; 
using System.Runtime.InteropServices; 
using System.Security; 
using System.Security.Permissions; 
using System.Windows.Forms; 

namespace Microsoft.WindowsAPICodePack.Dialogs 
{ 
    /// http://support.microsoft.com/kb/830033 
    /// <devdoc> 
    ///  This class is intended to use with the C# 'using' statement in 
    ///  to activate an activation context for turning on visual theming at 
    ///  the beginning of a scope, and have it automatically deactivated 
    ///  when the scope is exited. 
    /// </devdoc> 

    [SuppressUnmanagedCodeSecurity] 
    internal class EnableThemingInScope : IDisposable 
    { 
     // Private data 
     private UIntPtr cookie; 
     private static ACTCTX enableThemingActivationContext; 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")] 
     private static IntPtr hActCtx; 
     private static bool contextCreationSucceeded = false; 

     public EnableThemingInScope(bool enable) 
     { 
      cookie = UIntPtr.Zero; 
      if (enable && OSFeature.Feature.IsPresent(OSFeature.Themes)) 
      { 
       if (EnsureActivateContextCreated()) 
       { 
        if (!ActivateActCtx(hActCtx, out cookie)) 
        { 
         // Be sure cookie always zero if activation failed 
         cookie = UIntPtr.Zero; 
        } 
       } 
      } 
     } 

     ~EnableThemingInScope() 
     { 
      Dispose(); 
     } 

     void IDisposable.Dispose() 
     { 
      Dispose(); 
      GC.SuppressFinalize(this); 
     } 

     private void Dispose() 
     { 
      if (cookie != UIntPtr.Zero) 
      { 
       try 
       { 
        if (DeactivateActCtx(0, cookie)) 
        { 
         // deactivation succeeded... 
         cookie = UIntPtr.Zero; 
        } 
       } 
       catch (SEHException) 
       { 
        //Hopefully solved this exception 
       } 
      } 
     } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2002:DoNotLockOnObjectsWithWeakIdentity")] 
     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; 
      } 
     } 

     // All the pinvoke goo... 
     [DllImport("Kernel32.dll")] 
     private extern static IntPtr CreateActCtx(ref ACTCTX actctx); 
     [DllImport("Kernel32.dll")] 
     private extern static bool ActivateActCtx(IntPtr hActCtx, out UIntPtr lpCookie); 
     [DllImport("Kernel32.dll")] 
     private extern static bool DeactivateActCtx(uint dwFlags, UIntPtr lpCookie); 

     private const int ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID = 0x004; 

     private struct ACTCTX 
     { 
      public int cbSize; 
      public uint dwFlags; 
      public string lpSource; 
      public ushort wProcessorArchitecture; 
      public ushort wLangId; 
      public string lpAssemblyDirectory; 
      public string lpResourceName; 
      public string lpApplicationName; 
     } 
    } 
} 
+0

Cheers, c'est la bonne réponse. Pas besoin de changer de manifeste avec ça. –

+0

+1 Pour une réponse correcte. Pour référence future, j'ai eu une implémentation de cookie uint cassée similaire à partir de cet article de base de connaissances msdn: https://support.microsoft.com/en-us/kb/830033 juste pour plus de clarté: je pouvais créer la portée, mais j'ai eu un SEH Exception sur DeactivateActCtx. un débogage supplémentaire a révélé que c'était le code d'erreur 6, qui est ERROR_INVALID_HANDLE car le cookie n'a pas pu être utilisé pour désactiver correctement le contexte en raison de son mauvais type. – Samuel

+0

Merci! J'avais des problèmes avec comctl32.dll lors de la publication ClickOnce, et cela a résolu – dariusc

0

Cette page décrit comment ajouter un manifeste personnalisé à votre projet afin de dire Windows pour charger la nouvelle comctl32.dll (version 6.0):

Votre manifeste ont la bonne dépendance sur comctl32.dll? Avez-vous intégré le manifeste créé?

1

J'ai le même problème avec Visual Studio en mode débogage. Jusqu'à présent, je n'ai pas trouvé de solution de contournement, cela fonctionne très bien en mode Release.

4

J'ai récemment rencontré ce problème lors du débogage du code avec TaskDialogDemo dans le CodePack. C'est comme ça que je l'ai réparé. Le problème avec l'utilisation de ceci est que si j'ouvre deux ou trois boîtes de dialogue il lance une SEHException, que je n'ai pas trouvée comment réparer. Alors acheteur méfiez-vous.

Ajouter core \ Interop \ TaskDialogs \ EnableThemingInScope.cs:

using System; 
using System.IO; 
using System.Runtime.InteropServices; 
using System.Security; 
using System.Security.Permissions; 
using System.Windows.Forms; 

namespace Microsoft.WindowsAPICodePack.Dialogs { 
    /// http://support.microsoft.com/kb/830033 
    /// <devdoc> 
    ///  This class is intended to use with the C# 'using' statement in 
    ///  to activate an activation context for turning on visual theming at 
    ///  the beginning of a scope, and have it automatically deactivated 
    ///  when the scope is exited. 
    /// </devdoc> 

    [SuppressUnmanagedCodeSecurity] 
    internal class EnableThemingInScope : IDisposable { 
     // Private data 
     private uint cookie; 
     private static ACTCTX enableThemingActivationContext; 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")] 
     private static IntPtr hActCtx; 
     private static bool contextCreationSucceeded = false; 

     public EnableThemingInScope(bool enable) { 
      cookie = 0; 
      if (enable && OSFeature.Feature.IsPresent(OSFeature.Themes)) { 
       if (EnsureActivateContextCreated()) { 
        if (!ActivateActCtx(hActCtx, out cookie)) { 
         // Be sure cookie always zero if activation failed 
         cookie = 0; 
        } 
       } 
      } 
     } 

     ~EnableThemingInScope() { 
      Dispose(); 
     } 

     void IDisposable.Dispose() { 
      Dispose(); 
      GC.SuppressFinalize(this); 
     } 

     private void Dispose() { 
      if (cookie != 0) { 
       try { 
        if (DeactivateActCtx(0, cookie)) { 
         // deactivation succeeded... 
         cookie = 0; 
        } 
       } catch (SEHException) { 
        // Robpol86: I don't know how to fix this! 
       } 
      } 
     } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2002:DoNotLockOnObjectsWithWeakIdentity")] 
     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; 
      } 
     } 

     // All the pinvoke goo... 
     [DllImport("Kernel32.dll")] 
     private extern static IntPtr CreateActCtx(ref ACTCTX actctx); 
     [DllImport("Kernel32.dll")] 
     private extern static bool ActivateActCtx(IntPtr hActCtx, out uint lpCookie); 
     [DllImport("Kernel32.dll")] 
     private extern static bool DeactivateActCtx(uint dwFlags, uint lpCookie); 

     private const int ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID = 0x004; 

     private struct ACTCTX { 
      public int cbSize; 
      public uint dwFlags; 
      public string lpSource; 
      public ushort wProcessorArchitecture; 
      public ushort wLangId; 
      public string lpAssemblyDirectory; 
      public string lpResourceName; 
      public string lpApplicationName; 
     } 
    } 
} 

Puis, dans de base \ Interop \ TaskDialogs \ NativeTaskDialog.cs en ligne 93 (ci-dessus HResult hresult = TaskDialogNativeMethods.TaskDialogIndirect) faire ressembler la section comme ceci (à la fin vous aurez trois nouvelles lignes):

Questions connexes