我正在为TaskDialog使用WindowsAPICodePack。当我尝试显示对话框时,它说它需要加载comctl32.dll的版本6。所以我在app.manifest中添加了第6版,并尝试运行它。仍然没有运气。我去了调试文件夹,并运行没有Visual Studio的程序,它工作正常。我猜Visual Studio没有使用清单文件...我想知道是否有办法让它做到这一点。调试器中的C#:comctl32.dll版本6
6
A
回答
9
Rob pol86,您的代码正在抛出SEHExceptions,因为ActivateActCtx和DeactivateActCtx的签名不正确。您必须使用UIntPtr
而不是uint
作为lpCookie。
因此,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 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
本页面介绍如何添加自定义清单到您的项目,以告诉Windows加载新的comctl32.dll(版本6.0):
您是否有表现comctl32.dll的正确依赖关系?你嵌入了创建的清单吗?
1
我在调试模式下遇到与Visual Studio相同的问题。到目前为止,我还没有找到解决方法,它在发布模式下工作正常。
4
我最近遇到这个问题时CodePack中的TaskDialogDemo调试代码。这是我如何修复它。使用这个的问题是,如果我打开两个或三个对话框,它会抛出一个SEHException,我还没有想出如何解决。所以买家要小心。
添加核心\互操作\ 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;
}
}
}
然后,在上线93 核心\互操作\ TaskDialogs \ NativeTaskDialog.cs(以上的HResult HRESULT = TaskDialogNativeMethods.TaskDialogIndirect )使部分看起来像这样(最终你将有三条新线):
// Here is the way we use "vanilla" P/Invoke to call TaskDialogIndirect().
HResult hresult;
using (new EnableThemingInScope(true)) {
hresult = TaskDialogNativeMethods.TaskDialogIndirect(
nativeDialogConfig,
out selectedButtonId,
out selectedRadioButtonId,
out checkBoxChecked);
}
相关问题
- 1. TaskDialog触发异常:需要版本6中的comctl32.dll
- 2. 调试从C#调用VC++ 6 DLL#
- 3. C++调试版本
- 4. 在.NET中检测COMCTL32版本
- 5. 在C++中使用调试/发行版本DLL
- 6. C++ DLL调试
- 7. Azure dll版本调试难度
- 8. 在VS2005中调试DLL(C++)
- 9. 在C中调试DLL#
- 10. 调试版本找不到调试运行时DLL
- 11. 如何继承win32控件并保持与旧版本comctl32.dll的兼容性?
- 12. C#调试器拦截来自本地DLL的WriteConsole事件
- 13. 调试版运行,但版本提供0xc000007b - 缺少dll?
- 14. C#DLL版本冲突
- 15. C#版本控制dll/api
- 16. 调试安装的版本 - C#
- 17. 如何调试C++ dll调用C++ DllImport
- 18. 调试底层C++ DLL中的问题
- 19. Ordinal未找到在XP上的ComCtl32.dll C++
- 20. 试用版6
- 21. 无法将“comctl32.dll”DLL加载到内存中,同时冒充
- 22. 从C#调试CUDA Dll#
- 23. 用C语言调试dll
- 24. JNA C DLL调试指南?
- 25. 的Qt发布版本需要调试的DLL
- 26. 如何调试C++ dll的ctypes调用?
- 27. 输入HTML版本的Facebook调试器
- 28. C#项目使用的C++ DLL调试
- 29. 使用不同的DLL进行调试和发布版本
- 30. 发布版本依赖于来自MFC的调试DLL
欢呼声,这是正确的答案。没有必要用这个来改变清单。 – 2014-05-20 15:16:48
+1正确答案。为了将来的参考,我从这篇msdn知识库文章中得到了一个类似的破解uint cookie实现:https://support.microsoft.com/en-us/kb/830033只是为了清楚:我可以创建范围,但后来我得到了一个SEH DeactivateActCtx异常。进一步的调试发现它是错误代码6,这是ERROR_INVALID_HANDLE,因为由于错误的类型,cookie不能用于正确地停用上下文。 – Samuel 2015-11-25 07:46:53
谢谢!我在发布ClickOnce时遇到了comctl32.dll问题,并解决了这个问题 – dariusc 2016-04-25 17:20:56