Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than advapi32, prefix the name with the module name and a period.
CredRead (advapi32)
.
C# Signature:
[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Unicode, EntryPoint="CredReadW")]
static extern boolCredRead(string target, int type,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(CredentialReadMarshaler)] out CREDENTIAL cred);
VB Signature:
Declare Function CredRead Lib "advapi32.dll" (TODO) As TODO
User-Defined Types:
[StructLayout(LayoutKind.Sequential)]
private struct CREDENTIAL
{
public UInt32 flags;
public UInt32 type;
public string targetName;
public string comment;
public COMTypes.FILETIME lastWritten;
public UInt32 credentialBlobSize;
public IntPtr credentialBlob;
public UInt32 persist;
public UInt32 attributeCount;
public IntPtr credAttribute;
public string targetAlias;
public string userName;
}
Alternative Managed API:
Do you know one? Please contribute it!
Notes:
None.
Tips & Tricks:
To safely use CredRead(), you need to provide a custom marshaler. This is because, the structure pointer returned need to be freed using CredFree(). Marshal the credential blob based on the type of credential you are expecting. For instance you could use Marshal.PtrToStringUni() if you expect the password to be formatted in unicode. Otherwise, you need to extract it as a byte array. This needs to be done in the custom marshaling code below.
Same applies for credential attributes.
Sample Code:
public class CredentialMarshaler : ICustomMarshaler
{
public void CleanUpManagedData(object ManagedObj)
{
// Nothing to do since all data can be garbage collected.
}
public void CleanUpNativeData(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
{
return;
}
NativeMethods.CredFree(pNativeData);
}
public int GetNativeDataSize()
{
throw new Exception("The method or operation is not implemented.");
}
public IntPtr MarshalManagedToNative(object obj)
{
throw new Exception("Not implemented yet");
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
{
return null;
}
NativeMethods.CREDENTIAL cred = (NativeMethods.CREDENTIAL) Marshal.PtrToStructure(pNativeData,typeof(NativeMethods.CREDENTIAL));
return new Credential(cred);
}
#endregion
public static ICustomMarshaler GetInstance(string cookie)
{
return new CredentialMarshaler(false);
}
}
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).