2013-04-15 29 views
3

我正在開發一個現代WPF應用程序。我想用TaskDialog其實,但我總是得到普遍的錯誤:TaskDialog觸發異常:需要版本6中的comctl32.dll

TaskDialog feature needs to load version 6 of comctl32.dll but a different version is current loaded in memory.

我嘗試添加一個清單(其中已經包含了正確的COMCTL32.DLL所需要的依賴),並設置爲默認的清單項目屬性。

它仍然拋出這個異常: -/

我的應用程序是這樣的: It's啓動應用程序(正常的Windows應用程序,非WPF)。它只有「Program.cs」這是入口點。它會動態加載真正的應用程序(這是一個庫,而不是WPF應用程序項目)。它調用它啓動應用程序的啓動方法。

工程很好,但我總是得到這個例外。我想這是因爲這個啓動系統......但是,解決這個問題的方法是什麼?

感謝很多:)

[R

+0

你爲什麼這樣做?爲什麼不只有一個以'System.Windows.Application'開頭的常規WPF應用程序呢?我猜想winforms(或者之前加載的任何東西)正在加載一堆古老的廢話,WPF並不在意。 –

+0

好吧,這個系統是我之前有一個bug的一個片段。我只是改變了系統。它現在通過一個真正的WPF項目開始。 它仍然沒有工作。如果我激活「系統自己的調試」,我沒有得到任何執行,並打開對話框。唯一的錯誤是:圖標丟失: -/ 這可能是MS Api Codepack中的一個問題? – SharpShade

回答

3

也許我的解決方案將幫助你。

我的C#「應用程序」是用作WIX的CustomAction的類庫/ dll。我想要一個TaskDialog而不是MessageBox,但是我遇到了和你一樣的異常,而據我所知,清單文件對於C#類庫不起作用。我必須結合使用我的代碼才能加載正確版本的comctl32.dll。

我剛剛得到它的工作,所以我的代碼有點凌亂和脂肪。

來源:

  1. http://truecheaters.com/f51/%5Bc-%5D-taskdialog-9368.html
  2. http://support.microsoft.com/kb/830033

1)附上EnableThemingInScope類原樣從上面的第二連桿。

2)包括此改性TaskDialog其實枚舉/類:

[Flags] 
public enum TaskDialogButtons { 
    OK = 0x0001, 
    Cancel = 0x0008, 
    Yes = 0x0002, 
    No = 0x0004, 
    Retry = 0x0010, 
    Close = 0x0020 
} 

public enum TaskDialogIcon { 
    Information = UInt16.MaxValue - 2, 
    Warning = UInt16.MaxValue, 
    Stop = UInt16.MaxValue - 1, 
    Question = 0, 
    SecurityWarning = UInt16.MaxValue - 5, 
    SecurityError = UInt16.MaxValue - 6, 
    SecuritySuccess = UInt16.MaxValue - 7, 
    SecurityShield = UInt16.MaxValue - 3, 
    SecurityShieldBlue = UInt16.MaxValue - 4, 
    SecurityShieldGray = UInt16.MaxValue - 8 
} 

public enum TaskDialogResult { 
    None, 
    OK, 
    Cancel, 
    Yes, 
    No, 
    Retry, 
    Close 
} 

public class StatusDialog { 
    #region API 
    [DllImport("comctl32.dll", CharSet = CharSet.Unicode)] 
    public static extern int TaskDialog(IntPtr hWndParent, IntPtr hInstance, string pszWindowTitle, string pszMainInstruction, string pszContent, int dwCommonButtons, IntPtr pszIzon, out int pnButton); 
    #endregion 

    #region Modal 
    public static TaskDialogResult Show(IWin32Window owner, string text) { 
     return Show(owner, text, null, null, TaskDialogButtons.OK); 
    } 

    public static TaskDialogResult Show(IWin32Window owner, string text, string instruction) { 
     return Show(owner, text, instruction, null, TaskDialogButtons.OK, 0); 
    } 

    public static TaskDialogResult Show(IWin32Window owner, string text, string instruction, string caption) { 
     return Show(owner, text, instruction, caption, TaskDialogButtons.OK, 0); 
    } 

    public static TaskDialogResult Show(IWin32Window owner, string text, string instruction, string caption, TaskDialogButtons buttons) { 
     return Show(owner, text, instruction, caption, buttons, 0); 
    } 

    public static TaskDialogResult Show(IWin32Window owner, string text, string instruction, string caption, TaskDialogButtons buttons, TaskDialogIcon icon) { 
     return ShowInternal(owner.Handle, text, instruction, caption, buttons, icon); 
    } 
    #endregion 

    #region Non-Modal 
    public static TaskDialogResult Show(string text) { 
     return Show(text, null, null, TaskDialogButtons.OK); 
    } 

    public static TaskDialogResult Show(string text, string instruction) { 
     return Show(text, instruction, null, TaskDialogButtons.OK, 0); 
    } 

    public static TaskDialogResult Show(string text, string instruction, string caption) { 
     return Show(text, instruction, caption, TaskDialogButtons.OK, 0); 
    } 

    public static TaskDialogResult Show(string text, string instruction, string caption, TaskDialogButtons buttons) { 
     return Show(text, instruction, caption, buttons, 0); 
    } 

    public static TaskDialogResult Show(string text, string instruction, string caption, TaskDialogButtons buttons, TaskDialogIcon icon) { 
     return ShowInternal(IntPtr.Zero, text, instruction, caption, buttons, icon); 
    } 
    #endregion 

    #region Core Implementation 
    private static TaskDialogResult ShowInternal(IntPtr owner, string text, string instruction, string caption, TaskDialogButtons buttons, TaskDialogIcon icon) { 
     int p; 
     using (new EnableThemingInScope(true)) { 
      int resss = TaskDialog(owner, IntPtr.Zero, caption, instruction, text, (int) buttons, new IntPtr((int) icon), out p); 
      if (resss != 0) 
       throw new InvalidOperationException("Something weird has happened." + resss.ToString()); 
     } 

     switch (p) { 
      case 1: 
       return TaskDialogResult.OK; 
      case 2: 
       return TaskDialogResult.Cancel; 
      case 4: 
       return TaskDialogResult.Retry; 
      case 6: 
       return TaskDialogResult.Yes; 
      case 7: 
       return TaskDialogResult.No; 
      case 8: 
       return TaskDialogResult.Close; 
      default: 
       return TaskDialogResult.None; 
     } 
    } 
    #endregion 
} 

3.調用它,只需:

try { 
    StatusDialog.Show("About to test this...", "Heading I won't use.", "Dialog Title", TaskDialogButtons.OK); 
} catch (Exception e) { 
    MessageBox.Show(e.ToString(), "Error Found", MessageBoxButtons.OK); 
} 

4.結果:

enter image description here

+0

我得到一個異常,在comctl.dll中找不到入口點「TaskDialog」: - /另外我不喜歡這種方式鎖定設置控制進度條或其他東西的可能性的事實: -/ 但謝謝你的答案:) – SharpShade

+0

另外我發現這個:http://stackoverflow.com/questions/1415270/c-comctl32-dl​​l-version-6-in-debugger 所以也許這是一個錯誤的Visual Studio本身。正如我所說,在釋放模式下它工作正常(唯一的缺失的圖標)... – SharpShade

+0

@Razer像進度條的東西,你將不得不使用TaskDialogIndirect來代替。查看\ source \ WindowsAPICodePack \ Core \ Interop \ TaskDialogs \ TaskDialogNativeMethods.cs中該Windows®API代碼包的源代碼。雖然我沒有一個簡單的解決方案,但它要複雜得多。至於例外,不知道發生了什麼事。確保您的DllImport行與我在第2步中完全相同。如果它已經是這樣......那麼我不確定:(你很歡迎! – Robpol86

3

運行程序的* .exe版本,而不是Visual Studio * .vshost的版本。EXE

在Visual Studio這樣做,禁用標誌「調試/啓用調試器/啓用在Visual Studio宿主進程」

+0

另請參見:http://stackoverflow.com/問題/ 2069940/what-is-the-visual-studio-hosting-process- – Guildenstern70

+0

似乎是對問題的合理回答......只要我仍然能夠調試應用程序,這個應該是一個可能的解決方案,我會很快測試它 – SharpShade

+0

好吧,這很奇怪......現在它工作的很好,我沒有觸摸該項目大約半年,現在它運行良好,沒有禁用主機進程,也許這個問題已經被許多Windows/Visual Studio更新中的一個修復了。 – SharpShade

0

我發現,如果你有權限運行「RequireAdministrator」它拋出異常,但如果權限爲「AsInvoker」則不會拋出異常。只是我的觀察。如果你的應用程序需要管理員權限,那麼我很難受

相關問題