Search code examples
delphiwinapicredui

CredWrite returns Win32 error code 2 (ERROR_INVALID_FUNCTION) "Incorrect function."


i'm trying to call CredWrite, but it is returning ERROR_INVALID_FUNCTION. i can call CredRead to fetch credentials out of the password store, and i can store new credentials by using CredUIPromptForCredentials.

But i cannot figure out how to get CredWrite to work.

The code i'm using is:

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 is returning 1 (ERROR_INVALID_FUNCTION)

Is this function incorrect? It's not even returning ERROR_INVALID_PARAMETER, it's returning "Incorrect function". What is incorrect?

Is there any sample code out there that calls CredWrite?

Notes

  • i've tried calling the Ansi version (CredWriteA), same result
  • i've tried using CRED_PERSIST_SESSION and CRED_PERSIST_LOCAL_MACHINE, in addition to CRED_PERSIST_ENTERPRISE

Solution

  • Nevermind, i figured it out.

    And it's not the fault of the API call, or my parameters.

    i'm just stupid.

    And i want to sulk off, without having to say what i did :(