Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write to Registry HKEY_LOCAL_MACHINE

Status
Not open for further replies.

webowner47

Programmer
Nov 26, 2018
16
DZ
hello,
i'm trying to store application Code in the Registry HKLM, but i cannot write, the weird thing is that i can read anything in this Registry Root which is HKLM, but problem is in writing o it:

this is my code:

Code:
var KISReg : TRegistry;
    KISKey : string;
    RandNum1,RandNum2,RandNum3 : integer;
begin
     KISReg := TRegistry.Create(KEY_ALL_ACCESS or KEY_WOW64_64KEY);
     KISReg.RootKey := HKEY_LOCAL_MACHINE;
     if KISReg.OpenKeyReadOnly('\SOFTWARE\WOW6432Node\Examer\ExamManager\PathFile') then
       KISKey := KISReg.ReadString('EMPD');
     KISReg.OpenKey('\SOFTWARE\WOW6432Node\Examer\ExamManager\PathFile', true);
     RandNum1 := RandomRange(48, 57);
     RandNum2 := RandomRange(48, 57);
     RandNum3 := RandomRange(48, 57);
     KISKey[length(KISKey)-1] := Chr(RandNum1);
     KISKey[length(KISKey)-2] := Chr(RandNum2);
     KISKey[length(KISKey)-3] := Chr(RandNum3);
     KISReg.WriteString('EMPD', KISKey);
end;
 
What error are you getting? Where does the error occur? Does the key already exist? Is it possible that it won't exist? Does the string KISKey have a value if the OpenKeyReadOnly fails?
Your code also fails to close the key if the OpenKeyReadOnly succeeds which leaves you in a ReadOnly mode.
Mirtheil
 
I forgot to post the Error sorry :),

I'm getting this message "Failed to set data for 'EMPD'", Also the key already Exists.
By the way, the "KISKey" reads the Registry key value, the problem is when i write to 'EMPD' not when reading, the error comes from this line:

Code:
KISReg.WriteString('EMPD', KISKey);

any other suggestion please.

 
There are two issues I see. First, your code is opening the Registry key as ReadOnly. I changed it to:
Code:
KISReg := TRegistry.Create(KEY_ALL_ACCESS or KEY_WOW64_64KEY);
KISReg.RootKey := HKEY_LOCAL_MACHINE;
KISReg.OpenKey('\SOFTWARE\WOW6432Node\Examer\ExamManager\PathFile', true);
KISKey := KISReg.ReadString('EMPD');
RandNum1 := RandomRange(48, 57);
RandNum2 := RandomRange(48, 57);
RandNum3 := RandomRange(48, 57);
KISKey[length(KISKey)-1] := Chr(RandNum1);
KISKey[length(KISKey)-2] := Chr(RandNum2);
KISKey[length(KISKey)-3] := Chr(RandNum3);
KISReg.WriteString('EMPD', KISKey);

Once I did that, I no longer got the "Failed to set data" error but the registry was not updated. Second, writing to HKLM requires Administrative rights.
I had to "Run as Administrator" on the program for it to update the registry.



Mirtheil
 
thank you so much, now i no longer see that error, also the Registry update values
thanks for your help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top