Note: I am using Delphi XE2
I am trying to add a registry key. Here is my current code:
function AddRegKey(Key:HKEY;Keyname,Value:String):Boolean;
var
phkResult:HKEY;
begin
if RegOpenKeyEx(Key, PWideChar('SOFTWARE\Microsoft\Windows\CurrentVersion\Run'), 0, KEY_SET_VALUE, phkResult) = ERROR_SUCCESS then
begin
Result := (RegSetValueEx(phkResult, PWideChar(KeyName), 0, REG_SZ, PWideChar(Value), Length(Value)) = ERROR_SUCCESS);
RegCloseKey(phkResult);
end
else Result:=False;
end;
begin
If AddRegKey(HKEY_CURRENT_USER,'TestTest','123456789') Then
MessageBox(0,'Success','Test',0);
end.
Which is resulting in the following:

It is only adding the first four characters
But when I change the “Value” parameter to:
123456789123456789
It then adds:
123456789
So it seems as if it is only adding half of the given value for some reason. How do I go about solving this?
Am I passing the wrong data type for the *lpData parameter in RegSetValueEx?
Read the documentation. The last parameter of
RegSetValueEx()is a byte count, but you are passing it a character count instead (and not even the right count, either – you have to include the null terminator).SizeOf(WideChar)is 2, so you are tellingRegSetValueEx()to write only half of the String data. You need to fix that, eg:With that said, You might consider using the
TRegistryclass, which handles these details for you: