; https://www.autohotkey.com/boards/viewtopic.php?t=112391 ; Usage: ; cred := CredRead("CREDENTIALNAME", NewlineInPassword := 1) ; Username := cred.username ; Password := cred.password ; msgbox % cred.username ; msgbox % cred.password ; Functions ;------------------------------------------------ CredWrite(name, username, password) { VarSetCapacity(cred, 24 + A_PtrSize * 7, 0) cbPassword := StrLen(password)*2 NumPut(1 , cred, 4+A_PtrSize*0, "UInt") ; Type = CRED_TYPE_GENERIC NumPut(&name , cred, 8+A_PtrSize*0, "Ptr") ; TargetName = name NumPut(cbPassword, cred, 16+A_PtrSize*2, "UInt") ; CredentialBlobSize NumPut(&password , cred, 16+A_PtrSize*3, "UInt") ; CredentialBlob NumPut(3 , cred, 16+A_PtrSize*4, "UInt") ; Persist = CRED_PERSIST_ENTERPRISE (roam across domain) NumPut(&username , cred, 24+A_PtrSize*6, "Ptr") ; UserName return DllCall("Advapi32.dll\CredWriteW" , "Ptr", &cred ; [in] PCREDENTIALW Credential , "UInt", 0 ; [in] DWORD Flags , "UInt") ; BOOL } CredDelete(name) { return DllCall("Advapi32.dll\CredDeleteW" , "WStr", name ; [in] LPCWSTR TargetName , "UInt", 1 ; [in] DWORD Type, , "UInt", 0 ; [in] DWORD Flags , "UInt") ; BOOL } CredRead(name,NewlineInUsername:=0, NewlineInPassword:=0) { DllCall("Advapi32.dll\CredReadW" , "Str", name ; [in] LPCWSTR TargetName , "UInt", 1 ; [in] DWORD Type = CRED_TYPE_GENERIC (https://learn.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala) , "UInt", 0 ; [in] DWORD Flags , "Ptr*", pCred ; [out] PCREDENTIALW *Credential , "UInt") ; BOOL if !pCred{ ; Username InputBox, Username, Input Username, Username not found. `nPlease Input Username for: `n--%name%--,,,,,,,,%Clipboard% ; , Prompt, HIDE, Width, Height, X, Y, Font, Timeout, Default] if(ErrorLevel) return InputBox, Password, Input Password, Password not found. `nPlease Input Password for: `n--%name%--,,,,,,,,%Clipboard% ; , Prompt, HIDE, Width, Height, X, Y, Font, Timeout, Default] if(ErrorLevel) return if !CredWrite(name, Username, Password) MsgBox failed to write cred else, { ; try reading the credential that was just saved DllCall("Advapi32.dll\CredReadW" , "Str", name ; [in] LPCWSTR TargetName , "UInt", 1 ; [in] DWORD Type = CRED_TYPE_GENERIC (https://learn.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala) , "UInt", 0 ; [in] DWORD Flags , "Ptr*", pCred ; [out] PCREDENTIALW *Credential , "UInt") ; BOOL } if !pCred{ msgbox, Failed to read cred. after writing it to Credential Manager. return } } name := StrGet(NumGet(pCred + 8 + A_PtrSize * 0, "UPtr"), 256, "UTF-16") ; append new line to end if requested if(NewlineInUsername) username := StrGet(NumGet(pCred + 24 + A_PtrSize * 6, "UPtr"), 256, "UTF-16") . "`n" else, username := StrGet(NumGet(pCred + 24 + A_PtrSize * 6, "UPtr"), 256, "UTF-16") len := NumGet(pCred + 16 + A_PtrSize * 2, "UInt") ; append new line to end if requested if(NewlineInPassword) password := StrGet(NumGet(pCred + 16 + A_PtrSize * 3, "UPtr"), len/2, "UTF-16") . "`n" else, password := StrGet(NumGet(pCred + 16 + A_PtrSize * 3, "UPtr"), len/2, "UTF-16") DllCall("Advapi32.dll\CredFree", "Ptr", pCred) return {"name": name, "username": username, "password": password} } ; Misc ;------------------------------------------------ ; Escape::ExitApp