本文介绍了使用Delphi读取和写入类型REG_MULTI_SZ的注册表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi提供了库System.Win.Registry来操纵Windows注册表。
不幸的是,它不包含注册表数据类型 REG_MULTI_SZ (=字符串列表)的读/写过程。

Delphi offers the library System.Win.Registry to manipulate the windows registry.Unfortunately it doesn't contain read/write procedures for the registry datatype REG_MULTI_SZ (=list of strings).

以下代码返回带有无效数据类型的 ERegistryException -似乎仅适用于数据类型REG_SZ:

The following code returns an ERegistryException with "invalid datatype" - it seems only to work with datatype REG_SZ:

  Registry := TRegistry.Create;
  Registry.RootKey := HKEY_LOCAL_MACHINE;
  Registry.OpenKey(cKey, false);
  sValue := Registry.ReadString('MyRegEntry');

同时我可以使用

  Registry.ReadBinaryData('MyRegEntry', pBuf, sizeof(pBuf));

但是如果我使用 WriteBinaryData()它将以数据类型 REG_BINARY 而不是 REG_MULTI_SZ 写入注册表。这样就不能正常工作。

but if I write it back using WriteBinaryData() it will be written to the registry as datatype REG_BINARY instead of REG_MULTI_SZ. So that's not working properly.

如何使用Delphi处理数据类型为 REG_MULTI_SZ 的注册表数据?

How can I manipulate registry data of datatype REG_MULTI_SZ using Delphi?

推荐答案

我编写了两个函数(类帮助器)以扩展TRegistry的功能:

I have written two functions (a class helper) to extend the functionality of TRegistry:

unit Common.RegistryHelper;

interface

uses
  System.Classes, System.Win.Registry, Winapi.Windows, System.Math;

type
  TRegistryHelper = class helper for TRegistry
  public
    function ReadMultiSz(const name: string; var Strings: TStrings): boolean;
    function WriteMultiSz(const name: string; const value: TStrings): boolean;
  end;

implementation

function TRegistryHelper.ReadMultiSz(const name: string; var Strings: TStrings): boolean;
var
  iSizeInByte: integer;
  Buffer: array of WChar;
  iWCharsInBuffer: integer;
  z: integer;
  sString: string;
begin
  iSizeInByte := GetDataSize(name);
  if iSizeInByte > 0 then begin
    SetLength(Buffer, Floor(iSizeInByte / sizeof(WChar)));
    iWCharsInBuffer := Floor(ReadBinaryData(name, Buffer[0],
      iSizeInByte) / sizeof(WChar));
    sString := '';
    for z := 0 to iWCharsInBuffer do begin
      if Buffer[z] <> #0 then begin
        sString := sString + Buffer[z];
      end else begin
        if sString <> '' then begin
          Strings.Append(sString);
          sString := '';
        end;
      end;
    end;
    result := true;
  end else begin
    result := false;
  end;
end;

function TRegistryHelper.WriteMultiSz(const name: string; const value: TStrings): boolean;
var
  sContent: string;
  x: integer;
begin
  sContent := '';
  for x := 0 to pred(value.Count) do begin
    sContent := sContent + value.Strings[x] + #0;
  end;
  sContent := sContent + #0;
  result := RegSetValueEx(CurrentKey, pchar(name), 0, REG_MULTI_SZ,
    pointer(sContent), Length(sContent)*sizeof(Char)) = 0;
end;

end.

使用以上功能,您可以在程序中简单地编写以下代码,即可为REG_MULTI_SZ添加值条目:

Using the functions above you can simply write in your program the following code to add a value to a REG_MULTI_SZ entry:

procedure AddValueToRegistry();
const
  cKey = '\SYSTEM\ControlSet001\services\TcSysSrv';
var
  Registry: TRegistry;
  MyList: TStrings;
begin
  Registry := TRegistry.Create;
  Registry.RootKey := HKEY_LOCAL_MACHINE;
  Registry.OpenKey(cKey, false);
  try
    MyList := TStringList.Create();
    Registry.ReadMultiSz('MyRegEntry', MyList);
    MyList.Add('NewEntry');
    Registry.WriteMultiSz('MyRegEntry', MyList);
  finally
    MyList.Free;
  end;
  Registry.Free;
end;

这篇关于使用Delphi读取和写入类型REG_MULTI_SZ的注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 11:49