CredPackAuthenticationBuffer (credui)
Last changed: -208.107.125.70

.
Summary
The CredPackAuthenticationBuffer function converts a string user name and password into an authentication buffer.

C# Signature:

    [DllImport("credui.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern Boolean CredPackAuthenticationBuffer(
      int dwFlags,
      string pszUserName,
      string pszPassword,
      IntPtr pPackedCredentials,
      ref int pcbPackedCredentials);

VB Signature:

    ''' <summary>
    ''' Windows Platform function (called via pInvoke) which populates an authentication buffer with authentication details.
    ''' See http://msdn.microsoft.com/en-us/library/aa374802(v=vs.85).aspx
    ''' </summary>
    ''' <param name="dwFlags">RESERVED - must be zero.</param>
    ''' <param name="pszUserName">User Name.</param>
    ''' <param name="pszPassword">Password.</param>
    ''' <param name="pPackedCredentials">The buffer to be packed with credentials.</param>
    ''' <param name="pcbPackedCredentials">The the size of the buffer.</param>
    ''' <returns><c>True</c> on success; otherwise <c>False</c>. For extended error information, call the GetLastError() function.</returns>
    <DllImport("credui.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> <CLSCompliant(False)> _
    Public Shared Function CredPackAuthenticationBuffer(ByVal dwFlags As UInt32, ByVal pszUserName As String, ByVal pszPassword As String, ByVal pPackedCredentials As IntPtr, ByRef pcbPackedCredentials As UInt32) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

To determine required buffer size, call first with pPackedCredentials = IntPtr.Zero. Should return error code 160, but pcbPackedCredentials will be populated with the expected buffer size. Use Marshal.AllocCoTaskMem() to allocate the appropriate buffer, then call again, this time passing the allocated buffer.

Sample Code:

C# Sample:

    string username = "User";
    string password = "";
    int inCredSize = 1024;
    IntPtr inCredBuffer = Marshal.AllocCoTaskMem(inCredSize);

    CredPackAuthenticationBuffer(0, username, password, inCredBuffer, ref inCredSize);

Documentation