PInvoke CredRead in Advapi32.dll

  • Thread starter Thread starter hsd31
  • Start date Start date
H

hsd31

I'm trying to PInvoke the 'CredRead' function in Advapi32.dll. It
doesn't seem to work. CredRead returns true, but the CREDENTIALS struct

has flag set to 1538928. Anyone know what it means ? Is my CREDENTIALS
struct incorrect?


PInvoke Signature used:
-----------------------
[DllImport("advapi32.dll", EntryPoint="CredReadW", CharSet =
CharSet.Unicode)]
public static extern bool CredRead(string target,
CRED_TYPE type, int
reservedFlag, ref CREDENTIAL credential);


User Defined Structures:
------------------------
public enum CRED_TYPE : int
{
GENERIC = 1,
DOMAIN_PASSWORD = 2,
DOMAIN_CERTIFICATE = 3,
DOMAIN_VISIBLE_PASSWORD = 4,
MAXIMUM = 5



}


[StructLayout(LayoutKind.Seque­ntial, CharSet=CharSet.Unicode)]
public struct CREDENTIAL
{
public UInt32 flags;
public UInt32 type;
public string targetName;
public string comment;
public FILETIME lastWritten;
public UInt32 credentialBlobSize;
public IntPtr credentialBlob;
public UInt32 persist;
public UInt32 attributeCount;
public IntPtr credAttribute;
public string targetAlias;
public string userName;


}


Method Call:
------------
Security.CREDENTIAL credential = new CREDENTIAL();
bool bSuccess = Security.Advapi32Wrapper.CredR­ead(target,
CRED_TYPE.GENERIC, 0, ref credential));

(NOTE: bSuccess returns TRUE, credentials is not null)


Result:
-------
flags = 1538928
attributeCount = 0
comment = null
credAttribute = 0
credentialBlob = 0
credentialBlobSize = 0
lastWritten = {System.Runtime.InteropService­s.FILETIME}
persist = 0
targetAlias = null
targetName = null
type = 0
userName = null
 
I'm trying to PInvoke the 'CredRead' function in Advapi32.dll. It
doesn't seem to work. CredRead returns true, but the CREDENTIALS struct

has flag set to 1538928. Anyone know what it means ? Is my CREDENTIALS
struct incorrect?


PInvoke Signature used:
-----------------------
[DllImport("advapi32.dll", EntryPoint="CredReadW", CharSet =
CharSet.Unicode)]
public static extern bool CredRead(string target,
CRED_TYPE type, int
reservedFlag, ref CREDENTIAL credential);


User Defined Structures:
------------------------
public enum CRED_TYPE : int
{
GENERIC = 1,
DOMAIN_PASSWORD = 2,
DOMAIN_CERTIFICATE = 3,
DOMAIN_VISIBLE_PASSWORD = 4,
MAXIMUM = 5



}


[StructLayout(LayoutKind.Seque­ntial, CharSet=CharSet.Unicode)]
public struct CREDENTIAL
{
public UInt32 flags;
public UInt32 type;
public string targetName;
public string comment;
public FILETIME lastWritten;
public UInt32 credentialBlobSize;
public IntPtr credentialBlob;
public UInt32 persist;
public UInt32 attributeCount;
public IntPtr credAttribute;
public string targetAlias;
public string userName;


}


Method Call:
------------
Security.CREDENTIAL credential = new CREDENTIAL();
bool bSuccess = Security.Advapi32Wrapper.CredR­ead(target,
CRED_TYPE.GENERIC, 0, ref credential));

(NOTE: bSuccess returns TRUE, credentials is not null)


Result:
-------
flags = 1538928
attributeCount = 0
comment = null
credAttribute = 0
credentialBlob = 0
credentialBlobSize = 0
lastWritten = {System.Runtime.InteropService­s.FILETIME}
persist = 0
targetAlias = null
targetName = null
type = 0
userName = null


Seems correct, the flags field must be masked with 0x0000000F, only the
least significant 4 bits count, in your case flags = 8 (check the header for
the meaning of Credential flags field).

Willy.
 
Back
Top