I am trying to call CredWrite , but it returns ERROR_INVALID_FUNCTION . I can call CredRead to retrieve the credentials from the password store, and I can store the new credentials using CredUIPromptForCredentials .
But I can't figure out how to get CredWrite to work.
Used code:
var
Target, Username, Password: WideString;
begin
Target := 'StackOverflowSomething';
Username := 'IanBoyd';
Password := 'password69';
ZeroMemory(@Credentials, SizeOf(Credentials));
Credentials.TargetName := PWideChar(Target);
Credentials.Type_ := CRED_TYPE_GENERIC;
Credentials.UserName := PWideChar(Username);
Credentials.Persist := CRED_PERSIST_ENTERPRISE;
Credentials.CredentialBlob := PByte(Password);
Credentials.CredentialBlobSize := 2*(Length(Password));
Credentials.UserName := PWideChar(Username);
if not CredWriteW(@Credentials, 0) then
RaiseLastWin32Error;
And GetLastError returns 1 ( ERROR_INVALID_FUNCTION)
Is this function incorrect? It does not even return ERROR_INVALID_PARAMETER, it returns "Invalid function". What is wrong?
Is there any example code that calls CredWrite?
Notes
- I tried calling the Ansi version (CredWriteA), same result
CRED_PERSIST_SESSION CRED_PERSIST_LOCAL_MACHINE, CRED_PERSIST_ENTERPRISE