uses lsaapi;
TLSAObjectAttributes =
packed record
Length : ULONG;
RootDirectory : THandle;
ObjectName : PLSAUnicodeString;
Attributes : ULONG;
SecurityDescriptor : pointer;
// Points to type SECURITY_DESCRIPTOR
SecurityQualityOfService : pointer;
// Points to type SECURITY_QUALITY_OF_SERVICE
end;
LSA_HANDLE = pointer;
PLSA_HANDLE = ^LSA_HANDLE;
TLSAUnicodeString =
packed record
Length : WORD;
MaximumLength : WORD;
Buffer : PWideChar
end;
PLSAUnicodeString = ^TLSAUnicodeString;
NTStatus = Integer;
//DWORD;
// ^-- Auszüge aus der lsaapi
// THX to: http://msdn.microsoft.com/en-us/library/Aa378826
function StoreAutoLogonPassword(_Password:
string = '
'): DWORD;
var
ObjectAttributes: TLSAObjectAttributes;
LsaPolicyHandle: LSA_HANDLE;
lusSecretName: TLSAUnicodeString;
lusSecretData: TLSAUnicodeString;
SecretNameLength: Word;
SecretDataLength: Word;
ntsResult: NTStatus;
dwRetCode: DWORD;
t: TLSAUnicodeString;
begin
LsaPolicyHandle :=
NIL;
SecretNameLength := 0;
SecretDataLength := 0;
ntsResult := STATUS_SUCCESS;
dwRetCode := ERROR_SUCCESS;
// Object attributes are reserved, so initialize to zeros.
ZeroMemory(@ObjectAttributes, SizeOf(ObjectAttributes));
// Get a handle to the Policy object.
ntsResult := LsaOpenPolicy(
t,
// local Machine
ObjectAttributes,
POLICY_CREATE_SECRET,
LsaPolicyHandle);
if (STATUS_SUCCESS <> ntsResult)
then begin
// An error occurred. Display it as a win32 error code.
dwRetCode := LsaNtStatusToWinError(ntsResult);
ShowMessage('
Failed call to LsaOpenPolicy: ' + IntToStr(dwRetCode));
Result := dwRetCode;
Exit;
end;
// Initialize an LSA_UNICODE_STRING for the name of the
// private data ("DefaultPassword").
SecretNameLength := word(length('
DefaultPassword'));
lusSecretName.Buffer := '
DefaultPassword';
lusSecretName.Length := SecretNameLength * sizeof(WCHAR);
lusSecretName.MaximumLength := (SecretNameLength+1) * sizeof(WCHAR);
// If the pwszSecret parameter is NULL, then clear the secret.
if(_Password = '
')
then begin
ShowMessage('
Clearing the secret...');
ntsResult := LsaStorePrivateData(
LsaPolicyHandle,
lusSecretName,
t);
dwRetCode := LsaNtStatusToWinError(ntsResult);
end else begin
ShowMessage('
Setting the secret...');
// Initialize an LSA_UNICODE_STRING for the value
// of the private data.
SecretDataLength := word(length(_Password));
lusSecretData.Buffer := pwidechar(_Password);
lusSecretData.Length := SecretDataLength * sizeof(WCHAR);
lusSecretData.MaximumLength :=
(SecretDataLength+1) * sizeof(WCHAR);
ntsResult := LsaStorePrivateData(
LsaPolicyHandle,
lusSecretName,
lusSecretData);
dwRetCode := LsaNtStatusToWinError(ntsResult);
end;
LsaClose(LsaPolicyHandle);
if (dwRetCode <> ERROR_SUCCESS)
then begin
writeln('
Failed call to LsaStorePrivateData: ' + IntToStr(dwRetCode));
end;
Result := dwRetCode;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
StoreAutoLogonPassword('
1234567890');
end;