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

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
0
3

Nevermind, .

API, .

.

, , : (

+1

 Credentials.Type_ := CRED_TYPE_GENERIC;

?

 Credentials.Type := CRED_TYPE_GENERIC;
0

I'm curious this is for a smart card, right? and you keep the pin not on the smart card, maybe the attachment / driver is not loaded for the lender to work? It is possible that the CredWrite function will be activated with the standard specific smart card software / software used by windows, I believe that CredWrite is trying to contact something (perhaps a general API call for a standard function in smart card hardware?). .. just my thoughts ...

Hope this helps, Regards, Tom.

0
source

Source: https://habr.com/ru/post/1735852/


All Articles