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.
<Runtime.InteropServices.DllImport("advapi32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Unicode)> _
Function CredReadW( _
ByVal strTargetName$, _
ByVal intType As UInteger, _
ByVal intFlags As UInteger, _
ByRef intCredential As IntPtr) As Boolean
User-Defined Types:
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential, CharSet:=Runtime.InteropServices.CharSet.Unicode)> _
Structure Credential
Dim Flags As UInteger
Dim Type As UInteger
Dim TargetName$
Dim Comment$
Dim LastWritten As System.Runtime.InteropServices.ComTypes.FILETIME
Dim CredentialBlobSize As UInteger
Dim CredentialBlob As IntPtr
Dim Persist As UInteger
Dim AttributeCount As UInteger
Dim Attributes As IntPtr
Dim TargetAlias$
Dim UserName$
End Structure
public enum CRED_TYPE : uint
{
GENERIC = 1,
DOMAIN_PASSWORD = 2,
DOMAIN_CERTIFICATE = 3,
DOMAIN_VISIBLE_PASSWORD = 4,
GENERIC_CERTIFICATE = 5,
DOMAIN_EXTENDED = 6,
MAXIMUM = 7, // Maximum supported cred type
MAXIMUM_EX = (MAXIMUM + 1000), // Allow new applications to run on old OSes
}
//This type is deliberately not designed to be marshalled.
public class Credential
{
public UInt32 Flags;
public CRED_TYPE Type;
public string TargetName;
public string Comment;
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
public byte[] CredentialBlob;
public CRED_PERSIST Persist;
public CREDENTIAL_ATTRIBUTE[] Attributes;
public string TargetAlias;
public string UserName;
}
/// <summary>
///
/// </summary>
public class CredentialInMarshaler : ICustomMarshaler
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private class NATIVECREDENTIAL
{
public UInt32 Flags;
public CRED_TYPE Type;
public string TargetName;
public string Comment;
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
public UInt32 CredentialBlobSize;
public IntPtr CredentialBlob;
public CRED_PERSIST Persist;
public UInt32 AttributeCount;
public IntPtr Attributes;
public string TargetAlias;
public string UserName;
}
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;
}
CredFree(pNativeData);
}
public int GetNativeDataSize()
{
throw new NotImplementedException();
}
public IntPtr MarshalManagedToNative(object obj)
{
throw new NotImplementedException();
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
{
return null;
}
public static ICustomMarshaler GetInstance(string cookie)
{
return new CredentialInMarshaler();
}
}
Alternative Managed API:
Do you know one? Please contribute it!
Notes:
Attributes are not properly supported at this time.
An alternate approach to this can be found at http://blogs.msdn.com/peerchan/pages/487834.aspx. However as presented that technique requires more manual marshalling and more work for the calling code.
Custom Marshalling provide a self contained and caller transparent solution to this scenario, the returned Credentials object is fully managed without the need to worry about object lifetimes.
Tips & Tricks:
Use System.Text.Encoding.Unicode.GetString to convert the CredentialBlob into a password string.
Sample Code:
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).