Marshaling byte or char array in managed code as unsigned short * in unmanaged code RRS feed

  • Question

  • Hi

    I am trying to use AcquireCredentialHandle in a C# project using interop. One of the parameter in this function is " __in   PVOID pAuthData", which takes user credentials. Now, to pass this argument, we have defined SEC_WINNT_AUTH_IDENTITY structure, which seems to take password in the string format.

    In our application, we are always keeping the password in SecureString, and just convert it to byte for a short duration (if required) and then clean up that memory (by over-writing 0 over it). However, this structure doesn't seems to accept the byte array for the password and the return response from POP3 server is "-ERR Username or passowrd is incorrect". However, everything works fine if I pass the password as string.

    Is there a way to pass the password as byte without converting to string. (I don't want to use string, because it is immutable and you can not over-write on it)


    The define structure is:

        internal struct SEC_WINNT_AUTH_IDENTITY

            public string User;
            public int UserLength;
            public string Domain;
            public int DomainLength;
            public byte[] Password; //Works fine if I change the datatype to string
            public int PasswordLength;
            public int Flags;


    The basic question is that, we need to make a call to unmanaged code which accept a strcuture containing the following member.

    "unsigned short __RPC_FAR *Password"

    Is there a way to call the unmanaged code from the managed code and the corresponding structure in managed code is char[] or byte[] and it correspondingly marshal as the above member.

    Tuesday, October 4, 2011 4:53 AM


  • I think I would declare Password as IntPtr and just pin the byte array.
    Jose R. MCP
    • Marked as answer by Kunal Garg Tuesday, October 4, 2011 1:23 PM
    Tuesday, October 4, 2011 6:52 AM

All replies