0

I am trying to get the Value of an entry in the Registry in Delphi. typing in reg query HKLM\SOFTWARE\Microsoft\Cryptography /v MachineGuid in the command line (regardless of running it as admin or not) works and outputs me the value I am looking for. I would like Delphi to lookup that value too, but I can't get it to work. I tried GetDosOutput (like this one) and I tried the Component DOSCommand (this one). GetDosOutput and the Component DOSCommand are working very well or running other stuff (for example systeminfo or getmac or ipconfig or even running some batch files) but while trying the command reg query HKLM\SOFTWARE\Microsoft\Cryptography /v MachineGuid using one of those methods to output the cmd content I get something like this: ERROR: The specified registry key or value was not found. What works so far is this one: reg delete HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v MyApplication /f and this one: REG ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /f /v BackgroundVideoUploader /t REG_SZ /d C:\temp\BackgroundVideoUploader.exe I assume that I am not allowed to lookup HKLM but HKCU works. But even running my Delphi App as Administrator won't enable me to prform this command getting the desired output.

Even the TRegistry Component didnt output anything (empty string):

procedure TForm1.Button2Click(Sender: TObject);
var
  Registry: TRegistry;
begin
  Registry := TRegistry.Create(KEY_READ);
  try
    Registry.RootKey := HKEY_LOCAL_MACHINE;
    Registry.OpenKey('SOFTWARE\Microsoft\Cryptography', False);
    showmessage(Registry.ReadString('MachineGuid'));
  finally
    Registry.Free;
  end;
end;

Can someone tell me why its not working and how I can get it running?

qGold
  • 341
  • 2
  • 14
  • This is the wrong approach. The right approach is to use the [`TRegistry`](https://docwiki.embarcadero.com/RADStudio/Sydney/en/Using_TRegistry) class. It's really simple. – Andreas Rejbrand Oct 26 '21 at 19:48
  • I tried `TRegistry` with the same result: The Value couldn't be read.. – qGold Oct 26 '21 at 19:50
  • https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-virtualization ? – Andreas Rejbrand Oct 26 '21 at 19:53
  • @AndreasRejbrand I use 32 Bit VCL App in HKLM/Software with Admin rights, so due to the content of the given link I assume Registry Virtualization should work. I just dont know how to use the flags for the `TRegistry` Component – qGold Oct 26 '21 at 20:00
  • I think you will see the expected output if you compile your app as 64-bit instead. This should answer your question: https://stackoverflow.com/a/869863/282848 – Andreas Rejbrand Oct 26 '21 at 20:01
  • Thanks for the Link. I tried `SOFTWARE\Wow6432Node\Microsoft\Cryptography` now, but it still doesnt output anything using the `TRegistry` Component – qGold Oct 26 '21 at 20:03
  • Ah, [here's the solution](https://stackoverflow.com/a/869863/282848): `Registry := TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);`. This will let your 32-bit app access the 64-bit registry. – Andreas Rejbrand Oct 26 '21 at 20:10
  • Great, then I'll close this Q as a duplicate of the other one! – Andreas Rejbrand Oct 26 '21 at 20:12

0 Answers0