2012-02-24 48 views
0

我正在創建一個通過調用CredUIPromptForCredentials API來詢問管理憑據的對話框。下面是代碼片段:標題和消息只顯示調用CredUIPromptForCredentials API時的第一個字母API

 int maxUserID = 100; 
    int maxPassword = 100; 
    int maxDomain = 100; 
    StringBuilder userID = new StringBuilder(maxUserID); 
    StringBuilder userPassword = new StringBuilder(maxPassword); 
    StringBuilder userDomain = new StringBuilder(maxDomain); 

    bool getCredential = false; 

    // Setup the flags and variables   
    CREDUI_INFO credUI = new CREDUI_INFO(); 
    credUI.cbSize = Marshal.SizeOf(credUI); 
    credUI.pszCaptionText = "Title"; 
    credUI.pszMessageText = "Please login as an administrator."; 
    credUI.hwndParent = hwndParent; 
    bool save = false; 

    // for Windows XP 
    if (IsWindowsXP) 
    {    
     CREDUI_FLAGS flags = CREDUI_FLAGS.DO_NOT_PERSIST | CREDUI_FLAGS.REQUEST_ADMINISTRATOR; 
     CredUIReturnCodes returnCode1; 

     returnCode1 = PInvoke.CredUIPromptForCredentials(ref credUI, serverName, IntPtr.Zero, 0, userID, maxUserID, userPassword, maxPassword, ref save, flags); 
     if (returnCode1 == CredUIReturnCodes.NO_ERROR) 
     { 
      getCredential = true; 
     } 
    } 

然而,在Windows XP下只有標題和消息的第一個字母出現,在我的情況下,「T」和「P」。我無法弄清楚爲什麼?任何提示將不勝感激!

回答

1

發佈您的CREDUI_INFO聲明。它應該是這個樣子:

struct CREDUI_INFO 
    { 
     public int cbSize; 
     public IntPtr hwndParent; 
     public string pszMessageText; 
     public string pszCaptionText; 
     public IntPtr hbmBanner; 
    } 
+0

是的,這是正確的,但我有一個裝飾'[StructLayout(LayoutKind.Sequential,字符集= CharSet.Unicode)]'。 – 2012-02-27 09:13:06

+1

@Kevin「CharSet.Unicode」聽起來很可疑。如果你正在調用一個ANSI版本的函數並傳遞一個Unicode字符串,我期待你看到的行爲。試試'CharSet.Auto'或'CharSet.ANSI';或者確保你明確地調用了API的UNICODE版本(W後綴)。 – Joe 2012-02-27 09:57:50

+0

謝謝,你是絕對正確的! – 2012-02-27 16:56:14