注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

珠海渔郎之电子网档

项目管理 + 程序开发 + Delphi + 电脑应用 + 数码 + 进化感悟

 
 
 

日志

 
 

Delphi中纯API操作注册表的函数  

2009-08-22 18:17:39|  分类: Delphi |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Delphi中纯API操作注册表的函数,2004-3-22

//////////////////////////////////////////////////////////////////////////////
//       Registry/INI file function & procedure
//////////////////////////////////////////////////////////////////////////////

function RegExportToFile(const RootKey: HKEY; const SubKey, FileName: string): Boolean;
var
  Key: HKEY;
begin
  Result := False;
  EnablePrivilege('SeBackupPrivilege', True);
  if ERROR_SUCCESS = RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_ALL_ACCESS, Key) then
  begin
    Result := RegSaveKey(Key, PChar(FileName), nil) = ERROR_SUCCESS;
    RegCloseKey(Key);
  end;
end;

function RegImportFromFile(const RootKey: HKEY; const SubKey, FileName: string): Boolean;
begin
  EnablePrivilege('SeBackupPrivilege', True);
  Result := RegLoadKey(RootKey, PChar(SubKey), PChar(FileName)) = ERROR_SUCCESS;
end;

function RegReadString(const RootKey: HKEY; const SubKey, ValueName: string): string;
var
  Key: HKEY;
  T: DWORD;
  L: DWORD;
begin
  if ERROR_SUCCESS = RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_ALL_ACCESS, Key) then
  begin
    if ERROR_SUCCESS = RegQueryValueEx(Key, PChar(ValueName), nil, @T, nil, @L) then
    begin
      if T <> REG_SZ then raise Exception.Create('Data type mismatch');
      SetLength(Result, L);
      RegQueryValueEx(Key, PChar(ValueName), nil, @T, PByte(PChar(Result)), @L);
    end;
    RegCloseKey(Key);
  end;
end;

function RegReadInteger(const RootKey: HKEY; const SubKey, ValueName: string): integer;
var
  Key: HKEY;
  T: DWORD;
  L: DWORD;
begin
  if ERROR_SUCCESS = RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_ALL_ACCESS, Key) then
  begin
    if ERROR_SUCCESS = RegQueryValueEx(Key, PChar(ValueName), nil, @T, nil, @L) then
    begin
      if T <> REG_DWORD then raise Exception.Create('Data type mismatch');
      RegQueryValueEx(Key, PChar(ValueName), nil, @T, @Result, @L);
    end;
    RegCloseKey(Key);
  end;
end;

function RegReadBinary(const RootKey: HKEY; const SubKey, ValueName: string; Data: PChar; out Len: integer): Boolean;
{
  从注册表中读取二进制数据
  RootKey:指定主分支
  SubKey:子键的名字
  ValueName:键名,可以为空,为空即表示默认值
  Data:保存读取到的数据
  Len:读取到的数据的长度
}
var
  Key: HKEY;
  T: DWORD;
begin
  Result := False;
  if ERROR_SUCCESS = RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_ALL_ACCESS, Key) then
  begin
    if ERROR_SUCCESS = RegQueryValueEx(Key, PChar(ValueName), nil, @T, nil, @Len) then
    begin
      ReallocMem(Data, Len);
      Result := ERROR_SUCCESS = RegQueryValueEx(Key, PChar(ValueName), nil, @T, PByte(PChar(Result)), @Len);
    end;
    RegCloseKey(Key);
  end;
end;

function RegWriteString(const RootKey: HKEY; const SubKey, ValueName, Value: string): Boolean;
{
  写入一个字符串到注册表中
  RootKey:指定主分支
  SubKey:子键的名字
  ValueName:键名,可以为空,为空即表示写入默认值
  Value:数据
}
var
  Key: HKEY;
  R: DWORD;
begin
  Result := (ERROR_SUCCESS = RegCreateKeyEx(RootKey, PChar(SubKey), 0, 'Data',
    REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, Key, @R)) and
    (ERROR_SUCCESS = RegSetValueEx(Key, PChar(ValueName), 0, REG_SZ, PChar(Value), Length(Value)));
  RegCloseKey(Key);
end;

function RegWriteInteger(const RootKey: HKEY; const SubKey, ValueName: string; const Value: integer): Boolean;
{
  写入一个整数到注册表中
  RootKey:指定主分支
  SubKey:子键的名字
  ValueName:键名,可以为空,为空即表示写入默认值
  Value:数据
}
var
  Key: HKEY;
  R: DWORD;
begin
  Result := (ERROR_SUCCESS = RegCreateKeyEx(RootKey, PChar(SubKey), 0, 'Data',
    REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, Key, @R)) and
    (ERROR_SUCCESS = RegSetValueEx(Key, PChar(ValueName), 0, REG_DWORD, @Value, SizeOf(Value)));
  RegCloseKey(Key);
end;

function RegWriteBinary(const RootKey: HKEY; const SubKey, ValueName: string; Data: PChar; Len: integer): Boolean;
{
  从注册表中读取二进制数据
}
var
  Key: HKEY;
  R: DWORD;
begin
  Result := (ERROR_SUCCESS = RegCreateKeyEx(RootKey, PChar(SubKey), 0, 'Data',
    REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, Key, @R)) and
    (ERROR_SUCCESS = RegSetValueEx(Key, PChar(ValueName), 0, REG_BINARY, Data, Len));
  RegCloseKey(Key);
end;

function RegValueExists(const RootKey: HKEY; const SubKey, ValueName: string): Boolean; stdcall;
{
  判断注册表中是否存在指定的键名
}
var
  Key: HKEY;
  Dummy: DWORD;
begin
  Result := False;
  if ERROR_SUCCESS = RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_READ, Key) then
  begin
    Result := ERROR_SUCCESS = RegQueryValueEx(Key, PChar(ValueName), nil, @Dummy, nil, @Dummy);
    RegCloseKey(Key);
  end;
end;

function RegKeyExists(const RootKey: HKEY; const SubKey: string): Boolean; stdcall;
{
  判断注册表中是否存在指定的键
}
var
  Key: HKEY;
begin
  Result := RegOpenKey(RootKey, PChar(SubKey), Key) = ERROR_SUCCESS;
  if Result then RegCloseKey(Key);
end;

function RegKeyDelete(const RootKey: HKEY; const SubKey: string): Boolean; stdcall;
{
  删除注册表中指定的主键
}
begin
  Result := RegDeleteKey(RootKey, PChar(SubKey)) = ERROR_SUCCESS;
end;

function RegValueDelete(const RootKey: HKEY; const SubKey, ValueName: string): Boolean; stdcall;
{
  删除注册表中指定的键值
}
var
  RegKey: HKEY;
begin
  Result := False;
  if RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_SET_VALUE, RegKey) = ERROR_SUCCESS then
  begin
    Result := RegDeleteValue(RegKey, PChar(ValueName)) = ERROR_SUCCESS;
    RegCloseKey(RegKey);
  end
end;

function RegGetValueNames(const RootKey: HKEY; const SubKey: string; Names: TStrings): Boolean; stdcall;
{
  返回注册表中指定主键下的所有的键名列表
}
var
  RegKey: HKEY;
  I: DWORD;
  Size: DWORD;
  NumSubKeys: DWORD;
  NumSubValues: DWORD;
  MaxSubValueLen: DWORD;
  ValueName: string;
begin
  Result := False;
  if RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_READ, RegKey) = ERROR_SUCCESS then
  begin
    if RegQueryInfoKey(RegKey, nil, nil, nil, @NumSubKeys, nil, nil, @NumSubValues, @MaxSubValueLen, nil, nil, nil) = ERROR_SUCCESS then
    begin
      SetLength(ValueName, MaxSubValueLen + 1);
      if NumSubValues <> 0 then
        for I := 0 to NumSubValues - 1 do
        begin
          Size := MaxSubValueLen + 1;
          RegEnumValue(RegKey, I, PChar(ValueName), Size, nil, nil, nil, nil);
          Names.Add(PChar(ValueName));
        end;
      Result := True;
    end;
    RegCloseKey(RegKey);
  end;
end;

function RegGetKeyNames(const RootKey: HKEY; const SubKey: string; Names: TStrings): Boolean; stdcall;
{
  返回注册表中指定主键下的所有子键的名称列表
}
var
  RegKey: HKEY;
  I: DWORD;
  Size: DWORD;
  NumSubKeys: DWORD;
  MaxSubKeyLen: DWORD;
  KeyName: string;
begin
  Result := False;
  if RegOpenKeyEx(RootKey, PChar(SubKey), 0, KEY_READ, RegKey) = ERROR_SUCCESS then
  begin
    if RegQueryInfoKey(RegKey, nil, nil, nil, @NumSubKeys, @MaxSubKeyLen, nil, nil, nil, nil, nil, nil) = ERROR_SUCCESS then
    begin
      SetLength(KeyName, MaxSubKeyLen + 1);
      if NumSubKeys <> 0 then
        for I := 0 to NumSubKeys - 1 do
        begin
          Size := MaxSubKeyLen + 1;
          RegEnumKeyEx(RegKey, I, PChar(KeyName), Size, nil, nil, nil, nil);
          Names.Add(PChar(KeyName));
        end;
      Result := True;
    end;
    RegCloseKey(RegKey);
  end
end;

  评论这张
 
阅读(1487)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017