An error occurred while trying to save the value in the registry

Using the code below, I try to set the value in the registry key HKEY_LOCAL_MACHINE, but I get the error message "Could not set data for ....." If I use HKEY_CURRENT_USER, there is no problem.

What I miss here.

(The code is not complete, but I think these are important parts)

type TTypWinBits = (Bit32, Bit64); function WinBits: TTypWinBits; type TIsWow64Process = function(Handle:THandle; var IsWow64 : BOOL) : BOOL; stdcall; var hKernel32 : Integer; IsWow64Process : TIsWow64Process; IsWow64 : BOOL; begin Result := Bit32; hKernel32 := LoadLibrary('kernel32.dll'); if (hKernel32 = 0) then RaiseLastOSError; @IsWow64Process := GetProcAddress(hkernel32, 'IsWow64Process'); if Assigned(IsWow64Process) then begin IsWow64 := False; if (IsWow64Process(GetCurrentProcess, IsWow64)) then Result := Bit64 else RaiseLastOSError; end; FreeLibrary(hKernel32); end; function TFastRegistry.CreateConnection: TRegistry; begin Result := TRegistry.Create; try case WinBits of Bit32: Result := TRegistry.Create; Bit64: Result := TRegistry.Create(KEY_WRITE OR KEY_WOW64_64KEY); end; except on E: exception do Result := nil; end; end; procedure TFastRegistry.RunAdd(aDesc, aName: string); var Reg: TRegistry; sRegKey: String; begin sRegKey := 'Software\Microsoft\Windows\CurrentVersion\Run'; Reg := CreateConnection; with Reg do begin try RootKey := HKEY_LOCAL_MACHINE; if not KeyExists(sRegKey) then OpenKey(sRegKey, True) else OpenKey(sRegKey, False); WriteString(aDesc, aName); finally CloseKey; Free; end; end; end; 
+4
source share
2 answers

Writing to the local machine key requires elevated privileges. Without this, functions will fail, as you have noticed. If your program should be an administrative tool, use the manifest file so that the OS asks for permission. If you do not need this, then write to the current user key instead so that it does not affect all the accounts in the system.

+6
source

You just need to free the "Free" descriptor for the next entry in the register in order to recreate it, rather than constantly installing and opening and closing them through OpenKey and CloseKey! This seems like an error :-)

0
source

All Articles