CredWrite returns Win32 error code 2 (ERROR_INVALID_FUNCTION) “Incorrect function.”
Question
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
andCRED_PERSIST_LOCAL_MACHINE
, in addition toCRED_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 :(
OTHER TIPS
Shouldn't this
Credentials.Type_ := CRED_TYPE_GENERIC;
Be this instead?
Credentials.Type := CRED_TYPE_GENERIC;
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.