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=3D"CredReadW", CharSet =3D
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 =3D 1,
DOMAIN_PASSWORD =3D 2,
DOMAIN_CERTIFICATE =3D 3,
DOMAIN_VISIBLE_PASSWORD =3D 4,
MAXIMUM =3D 5



}


[StructLayout(LayoutKind.Seque=ADntial, CharSet=3DCharSet.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 =3D new CREDENTIAL();
bool bSuccess =3D Security.Advapi32Wrapper.CredR=ADead(target,
CRED_TYPE.GENERIC, 0, ref credential));

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


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

Re: PInvoke CredRead in Advapi32.dll by Willy

Willy
Thu May 12 03:56:43 CDT 2005


"hsd31" <hdhanjal@gmail.com> wrote in message
news:1115845553.578077.290420@g43g2000cwa.googlegroups.com...
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.




Re: PInvoke CredRead in Advapi32.dll by hsd31