views:

254

answers:

3

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
A: 

Shouldn't this

 Credentials.Type_ := CRED_TYPE_GENERIC;

Be this instead?

 Credentials.Type := CRED_TYPE_GENERIC;
John Knoeller
No. :-) Type is a reserved word in Delphi, and so importing a type library converts Type to Type_ instead, and things are remapped internally. Good eye, though. ;-)
Ken White
@Ken White: OK, that makes sense. Thanks for the explanation.
John Knoeller
^What he said. :P
Ian Boyd
A: 

I am curious, this is for the smart card, right? and you're storing the pin no to the smart card, maybe the attachment/driver is not loaded for the credwrite to work? Perhaps, with a standard defined smartcard hardware/software used by windows will activate the CredWrite function, my guess is that CredWrite is attempting to communicate with something (perhaps a generic API call to a standard function within the smartcard hardware?)...just my thoughts...

Hope this helps, Best regards, Tom.

tommieb75
It's not for a smart card. i'm storing generic credentials.
Ian Boyd
A: 

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 :(

Ian Boyd