Win64中的注册表节点描述 [英] Registry Node Decription in Win64

查看:216
本文介绍了Win64中的注册表节点描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Delphi XE2项目在Windows注册表中创建一些节点和子节点,如下所述:



我已经定义了以下代码:

  function GetWinDir:串; 
var
WindowsDirectory:数组[0..MAX_PATH]的Char;
begin
GetWindowsDirectory(WindowsDirectory,MAX_PATH - 1);
SetLength(Result,StrLen(WindowsDirectory));
结果:= IncludeTrailingPathDelimiter(WindowsDirectory);
结束

函数GetSysDir:string;
var
SystemDirectory:数组[0..MAX_PATH]的Char;
begin
GetSystemDirectory(SystemDirectory,MAX_PATH - 1);
SetLength(Result,StrLen(SystemDirectory));
结果:= IncludeTrailingPathDelimiter(SystemDirectory);
结束

函数GetSysNativeDir:string;
var
WindowsDirectory:数组[0..MAX_PATH]的Char;
begin
GetWindowsDirectory(WindowsDirectory,MAX_PATH - 1);
SetLength(Result,StrLen(WindowsDirectory));
结果:= IncludeTrailingPathDelimiter(WindowsDirectory)+'Sysnative\';
结束

程序TMainForm.BitBtn01Click(发件人:TObject);
var
RegistryEntry:TRegistry;
RegistryEntryValue:string;
begin
RegistryEntry:= TRegistry.Create(KEY_READ或KEY_WOW64_64KEY);
RegistryEntry.RootKey:= HKEY_CLASSES_ROOT;
if(not RegistryEntry.KeyExists('CLSID\ {BE800AEB-A440-4B63-94CD-AA6B43647DF9} \'))然后
begin
RegistryEntry.Access:= KEY_WRITE或KEY_WOW64_64KEY;
如果RegistryEntry.OpenKey('CLSID\ {00000000-0000-0000-0000-000000000001} \',true)然后
begin
Memo01.Font.Color:= 3992580;
Memo01.Lines.Add('Windows注册表项已在您的系统中找到)
RegistryEntry.WriteString('','Delphi应用程序向导');
RegistryEntry.OpenKey('Subnode 01\',true);
RegistryEntry.WriteExpandString('','%SystemRoot%\System32\Application Wizard 01.dll');
RegistryEntry.WriteString('Subnode String 01','00001');
RegistryEntry.CloseKey();
RegistryEntry.OpenKey('CLSID\ {00000000-0000-0000-0000-000000000001} \Subnode 02\',true);
RegistryEntry.WriteExpandString('','%SystemRoot%\System32\Application Wizard 02.dll');
RegistryEntry.WriteString('Subnode String 02','00002');
RegistryEntry.CloseKey();
RegistryEntry.OpenKey('CLSID\ {00000000-0000-0000-0000-000000000001} \Subnode 03\',true);
RegistryEntry.WriteExpandString('','%SystemRoot%\System32\Application Wizard 03.dll');
RegistryEntry.WriteString('Subnode String 03','00003');
RegistryEntry.CloseKey();
RegistryEntry.OpenKey('CLSID\ {00000000-0000-0000-0000-000000000001} \Subnode 04\',true);
RegistryEntry.WriteExpandString('','%SystemRoot%\System32\Application Wizard 04.dll');
RegistryEntry.WriteString('Subnode String 04','00004');
RegistryEntry.CloseKey();
RegistryEntry.OpenKey('CLSID\ {00000000-0000-0000-0000-000000000001} \Subnode 05\',true);
RegistryEntry.WriteExpandString('','%SystemRoot%\System32\Application Wizard 05.dll');
RegistryEntry.WriteString('Subnode String 05','00005');
Memo01.Font.Color:= 3992580;
Memo01.Lines.Add(Windows注册表项已成功创建)
end
如果注册表编号为(CLSID\ {00000000-0000-0000-0000-000000000001} \',false)然后
begin
Memo01.Font.Color:= 7864575;
Memo01.Lines.Add('Windows注册表项未成功创建)
end
end
else
begin
if(RegistryEntry.KeyExists ('CLSID\ {00000000-0000-0000-0000-000000000001} \'))然后
begin
Memo01.Font.Color:= 7864575;
Memo01.Lines.Add('Windows注册表项在系统中找到)
end;
结束
RegistryEntry.CloseKey();
RegistryEntry.Free;
结束

..
..
..
..
..
程序TMainForm.BitBtn02Click(发件人:TObject);
var
RegistryEntry:TRegistry;
begin
RegistryEntry:= TRegistry.Create(KEY_READ或KEY_WOW64_64KEY);
RegistryEntry.RootKey:= HKEY_CLASSES_ROOT;
if(RegistryEntry.KeyExists('CLSID\ {00000000-0000-0000-0000-000000000001} \'))然后
begin
Memo01.Font.Color:= 3992580;
Memo01.Lines.Add('Windows注册表项已在您的系统中找到)
RegistryEntry.Access:= KEY_WRITE或KEY_WOW64_64KEY;
RegistryEntry.DeleteKey('CLSID\ {00000000-0000-0000-0000-000000000001} \Subnode 01\');
RegistryEntry.DeleteKey('CLSID\ {00000000-0000-0000-0000-000000000001} \Subnode 02\');
RegistryEntry.DeleteKey('CLSID\ {00000000-0000-0000-0000-000000000001} \Subnode 03\');
RegistryEntry.DeleteKey('CLSID\ {00000000-0000-0000-0000-000000000001} \Subnode 04\');
RegistryEntry.DeleteKey('CLSID\ {00000000-0000-0000-0000-000000000001} \Subnode 05\');
RegistryEntry.DeleteKey('CLSID\ {00000000-0000-0000-0000-000000000001} \');
RegistryEntry.CloseKey();
RegistryEntry.Free;
Memo01.Font.Color:= 16756480;
Memo01.Lines.Add(Windows注册表项已成功删除);
end
else
begin
Memo01.Font.Color:= 7864575;
Memo01.Lines.Add('Windows注册表项未在您的系统中找到)
结束
结束

我的问题是:


  1. 尽管我试图将每个子节点的默认字符串写为%SystemRoot%\System32\Application Wizard 01.dll %SystemRoot%\SysWow64\Application Wizard 01.dll 已写入。如何避免?


  2. 在Subnode 01中写入一些字符串之后,当我在Subnode 02等中写入一些字符串时,无论是每次都要调用 CloseKey(); OpenKey 主节点,就像我在代码中所做的那样。还是有其他解决方案?


  3. 如果我想使用一些代码删除主节点,我必须删除第一个Subnode 05,然后删除Subnode 04 ...等等。只有在删除所有子节点后,我才能删除主节点。如果任何子节点包含另一个子节点,我将检查这些内容。有没有解决方案,而不是删除这些子节点,直接主节点将被删除?



解决方案


为什么system32映射到SysWOW64?


这是一个相当模糊注册表的角落。因为您的进程是一个32位进程,注册表重定向器插入。 文档指出:


此外,包含system32的REG_SZ或REG_EXPAND_SZ键替换为syswow64。字符串必须以指向%windir%\system32下的路径开始。字符串比较不区分大小写。在匹配路径之前,会展开环境变量,因此将替换以下所有路径:%windir%\system32,%SystemRoot%\system32和C:\windows\system32。


解决这个问题的最简单的方法是(而且可能是唯一的)是从64位进程执行写入,从而逃避注册表重定向器的离散。 p>





我必须调用 OpenKey()对于我写的每个不同的键?


是。






我可以删除所有的子项吗?


<$ c $的文档c> TRegistry.DeleteKey


调用DeleteKey删除指定的键及其关联数据,如果有的话,从注册表。在Windows 95下,如果密钥具有子密钥,则还将删除子密钥和任何关联的数据。在Windows NT下,子键必须通过单独调用DeleteKey显式删除。


原因是 DeleteKey 调用 RegDeleteKeyEx 文档说明:


要删除的子项不能有子项。要删除一个键及其所有子项,您需要枚举子项并单独删除它们。要递归删除密钥,请使用RegDeleteTree或SHDeleteKey功能。


所以,如果你准备直接调用Windows API函数,你可以使用上述任一功能删除一个密钥及其子密钥。



如果您需要支持XP,则使用 SHDeleteKey 你叫这样:

  SHDeleteKey(HKEY_CLASSES_ROOT,PChar(SubKeyName)); 


I am having one Delphi XE2 Project to create some node and subnodes in Windows Registry as described below :

I have defined the following codes :

function GetWinDir: string;
var
  WindowsDirectory: array[0..MAX_PATH] of Char;
begin
   GetWindowsDirectory(WindowsDirectory, MAX_PATH - 1);
   SetLength(Result, StrLen(WindowsDirectory));
   Result := IncludeTrailingPathDelimiter(WindowsDirectory);
end;

function GetSysDir: string;
var
  SystemDirectory: array[0..MAX_PATH] of Char;
begin
  GetSystemDirectory(SystemDirectory, MAX_PATH - 1);
  SetLength(Result, StrLen(SystemDirectory));
  Result := IncludeTrailingPathDelimiter(SystemDirectory);
end;

function GetSysNativeDir: string;
var
  WindowsDirectory: array[0..MAX_PATH] of Char;
begin
   GetWindowsDirectory(WindowsDirectory, MAX_PATH - 1);
   SetLength(Result, StrLen(WindowsDirectory));
   Result := IncludeTrailingPathDelimiter(WindowsDirectory)  + 'Sysnative\';
end;

procedure TMainForm.BitBtn01Click(Sender: TObject);
var
  RegistryEntry : TRegistry;
  RegistryEntryValue : string;   
begin
  RegistryEntry := TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);
  RegistryEntry.RootKey := HKEY_CLASSES_ROOT;
  if (not RegistryEntry.KeyExists('CLSID\{BE800AEB-A440-4B63-94CD-AA6B43647DF9}\')) then
    begin
      RegistryEntry.Access:= KEY_WRITE or KEY_WOW64_64KEY;
      if RegistryEntry.OpenKey('CLSID\{00000000-0000-0000-0000-000000000001}\',true) then
        begin
          Memo01.Font.Color := 3992580;
          Memo01.Lines.Add('Windows Registry Entry Has Been Found In Your System');
          RegistryEntry.WriteString('', 'Delphi Application Wizard');
          RegistryEntry.OpenKey('Subnode 01\',true);
          RegistryEntry.WriteExpandString('', '%SystemRoot%\System32\Application Wizard 01.dll');
          RegistryEntry.WriteString('Subnode String 01', '00001');
          RegistryEntry.CloseKey();
          RegistryEntry.OpenKey('CLSID\{00000000-0000-0000-0000-000000000001}\Subnode 02\',true);
          RegistryEntry.WriteExpandString('', '%SystemRoot%\System32\Application Wizard 02.dll');
          RegistryEntry.WriteString('Subnode String 02', '00002');
          RegistryEntry.CloseKey();
          RegistryEntry.OpenKey('CLSID\{00000000-0000-0000-0000-000000000001}\Subnode 03\',true);
          RegistryEntry.WriteExpandString('', '%SystemRoot%\System32\Application Wizard 03.dll');
          RegistryEntry.WriteString('Subnode String 03', '00003');
          RegistryEntry.CloseKey();
          RegistryEntry.OpenKey('CLSID\{00000000-0000-0000-0000-000000000001}\Subnode 04\',true);
          RegistryEntry.WriteExpandString('', '%SystemRoot%\System32\Application Wizard 04.dll');
          RegistryEntry.WriteString('Subnode String 04', '00004');
          RegistryEntry.CloseKey();
          RegistryEntry.OpenKey('CLSID\{00000000-0000-0000-0000-000000000001}\Subnode 05\',true);
          RegistryEntry.WriteExpandString('', '%SystemRoot%\System32\Application Wizard 05.dll');
          RegistryEntry.WriteString('Subnode String 05', '00005');
          Memo01.Font.Color := 3992580;
          Memo01.Lines.Add('Windows Registry Entry Has Been Created Successfully')
        end
      else if RegistryEntry.OpenKey('CLSID\{00000000-0000-0000-0000-000000000001}\',false) then
        begin
          Memo01.Font.Color := 7864575;
          Memo01.Lines.Add('Windows Registry Entry Has Not Been Created Successfully')
        end
    end
  else
    begin
      if (RegistryEntry.KeyExists('CLSID\{00000000-0000-0000-0000-000000000001}\')) then
        begin
          Memo01.Font.Color := 7864575;
          Memo01.Lines.Add('Windows Registry Entry Has Been Found In Your System')
        end;
    end;
  RegistryEntry.CloseKey();
  RegistryEntry.Free;
end;

..
..
..
..
..
procedure TMainForm.BitBtn02Click(Sender: TObject);
var
  RegistryEntry : TRegistry;
begin
  RegistryEntry := TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);
  RegistryEntry.RootKey := HKEY_CLASSES_ROOT;
  if (RegistryEntry.KeyExists('CLSID\{00000000-0000-0000-0000-000000000001}\')) then
    begin
      Memo01.Font.Color := 3992580;
      Memo01.Lines.Add('Windows Registry Entry Has Been Found In Your System');
      RegistryEntry.Access:= KEY_WRITE or KEY_WOW64_64KEY;
      RegistryEntry.DeleteKey('CLSID\{00000000-0000-0000-0000-000000000001}\Subnode 01\');
      RegistryEntry.DeleteKey('CLSID\{00000000-0000-0000-0000-000000000001}\Subnode 02\');
      RegistryEntry.DeleteKey('CLSID\{00000000-0000-0000-0000-000000000001}\Subnode 03\');
      RegistryEntry.DeleteKey('CLSID\{00000000-0000-0000-0000-000000000001}\Subnode 04\');
      RegistryEntry.DeleteKey('CLSID\{00000000-0000-0000-0000-000000000001}\Subnode 05\');
      RegistryEntry.DeleteKey('CLSID\{00000000-0000-0000-0000-000000000001}\');
      RegistryEntry.CloseKey();
      RegistryEntry.Free;
      Memo01.Font.Color := 16756480;
      Memo01.Lines.Add('Windows Registry Entry Has Been Deleted Successfully');
    end
  else
    begin
      Memo01.Font.Color := 7864575;
      Memo01.Lines.Add('Windows Registry Entry Has Not Been Found In Your System');
    end;
end;  

My questions is that :

  1. Though I am trying to write the the default string for every Subnode as %SystemRoot%\System32\Application Wizard 01.dll yet %SystemRoot%\SysWow64\Application Wizard 01.dll is written. How to avoid that?

  2. After writing some string in Subnode 01, when I am tring to write some string in Subnode 02 and so on, whether, every time, I have to call CloseKey(); and OpenKey the Main Node or not, as I have done in my codes. Or is there any other solution?

  3. If I wish to delete the Main node using some codes, I have to delete first Subnode 05, then Subnode 04 ... and so on. Only after deleting all the subnodes I will be able to delete the main node. If any Subnodes contain another Subnodes, I will have check those things. Is there any solution so that instead of deleting those Subnodes, directly the Main node will be deleted?

解决方案

Why does system32 get mapped to SysWOW64?

This is a rather obscure corner of the registry. Because your process is a 32 bit process the registry redirector interjects. The documentation states:

In addition, REG_SZ or REG_EXPAND_SZ keys containing system32 are replaced with syswow64. The string must begin with the path pointing to or under %windir%\system32. The string comparison is not case-sensitive. Environment variables are expanded before matching the path, so all of the following paths are replaced: %windir%\system32, %SystemRoot%\system32, and C:\windows\system32.

The simplest (and possibly the only) way to get around this is to perform the writing from a 64 bit process and thus escape the clutches of the registry redirector.


Do I have to call OpenKey() for each different key that I write to?

Yes.


Do I have to empty all the subkeys before I can delete a key?

From the documentation of TRegistry.DeleteKey:

Call DeleteKey to remove a specified key and its associated data, if any, from the registry. Under Windows 95, if the key has subkeys, the subkeys and any associated data are also removed. Under Windows NT, subkeys must be explicitly deleted by separate calls to DeleteKey.

The reason for this is that DeleteKey calls RegDeleteKeyEx whose documentation states:

The subkey to be deleted must not have subkeys. To delete a key and all its subkeys, you need to enumerate the subkeys and delete them individually. To delete keys recursively, use the RegDeleteTree or SHDeleteKey function.

So, if you are prepared to call Windows API functions directly, you can delete a key and its subkeys in one API call, using either of those aforementioned functions.

If you need to support XP then use SHDeleteKey which you call like this:

SHDeleteKey(HKEY_CLASSES_ROOT, PChar(SubKeyName));

这篇关于Win64中的注册表节点描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆