调用CredUIPromptForCredentials API时,标题和消息仅显示第一个字母

本文关键字:显示 第一个 消息 API CredUIPromptForCredentials 标题 调用 | 更新日期: 2023-09-27 18:25:38

我正在创建一个对话框,通过调用CredUIPromptForCredentials 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;
        }
     } 

然而,在WindowsXP下,只有标题和消息的第一个字母出现,在我的例子中是"T"answers"p"。我不明白为什么?如有任何提示,我们将不胜感激!

调用CredUIPromptForCredentials API时,标题和消息仅显示第一个字母

发布CREDUI_INFO声明。它应该看起来像:

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