How can I read 64-bit registry key from a 32-bit process?

后端 未结 4 2019
轻奢々
轻奢々 2020-12-16 03:01

I\'ve been using the value of key MachineGuid from HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Cryptography to uniquely identify hosts, but from 32-b

4条回答
  •  误落风尘
    2020-12-16 03:23

    I would suggest you use the IsWow64Process() function to know when you are a 32-process running on a 64-bit OS, and then only apply the KEY_WOW64_64KEY flags in that specific condition. If the app is a 32-bit process on a 32-bit OS, or a 64-bit process on a 64-bit OS, the flags is not needed.

    For example:

    const 
      KEY_WOW64_64KEY = $0100; 
    var 
      key: HKEY; 
      str: string; 
      len: DWORD; 
      flag: REGSAM;
      wow64: BOOL;
    begin 
      flag := 0;
      wow64 := 0;
      IsWow64Process(GetCurrentProcess(), @wow64);
      if wow64 <> 0 then flag := KEY_WOW64_64KEY;
    
      if RegOpenKeyEx(HKEY_LOCAL_MACHINE, 'Software\Microsoft\Cryptography', 0, KEY_QUERY_VALUE or flag, key) = ERROR_SUCCESS then 
      try
        SetLength(str, 40); 
        len := Length(str) * SizeOf(Char); 
        if RegQueryValueEx(key, 'MachineGuid', nil, nil, PByte(Pointer(s)), @len) <> ERROR_SUCCESS then len := 0;
        SetLength(str, len div SizeOf(Char)); 
      finally
        RegCloseKey(key); 
      end; 
    end;
    

提交回复
热议问题