0

I am creating a dialog asking for administrative credential by calling CredUIPromptForCredentials API. Here is a snippet of the code:

     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;
        }
     } 

However, under Windows XP only the first letters of the Caption and Message appear, in my case, "T" and "P". And I cannot figure it out why? Any hints would be greatly appreciated!

1 Answers1

1

Post your CREDUI_INFO declaration. It should look something like:

    struct CREDUI_INFO
    {
        public int cbSize;
        public IntPtr hwndParent;
        public string pszMessageText;
        public string pszCaptionText;
        public IntPtr hbmBanner;
    }
Joe
  • 122,218
  • 32
  • 205
  • 338
  • yes, that is correct, except I have a decoration of `[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]`. – Kevin Wang Feb 27 '12 at 09:13
  • 1
    @Kevin "CharSet.Unicode" sounds suspicious. If you're calling an ANSI version of the function and passing a Unicode string, I'd expect exactly the behavior you're seeing. Try `CharSet.Auto` or `CharSet.ANSI`; or make sure you're explicitly calling the UNICODE version of the API (W suffix). – Joe Feb 27 '12 at 09:57