现在的位置: 首页 > 综合 > 正文

如何获得修改注册表的权限

2013年10月07日 ⁄ 综合 ⁄ 共 861字 ⁄ 字号 评论关闭

在编写查找计算机上曾经接入USB设备的软件的时候发现我的程序无法修改某些注册表中的项,有些项只有使用只读模式才能读取数据,如果使用读写权限将会被拒绝。经过一翻查找,找到了办法:

 

uses

  jclwin32,
   AccCtrl;
function   AllowRegKeyForEveryone(Key:   HKEY;   Path:   string):   Boolean;
var  
      WidePath:   PWideChar;  
      Len:   Integer;
begin  
      case   Key   of  
          HKEY_LOCAL_MACHINE:  
              Path   :=   'MACHINE\'   +   Path;  
          HKEY_CURRENT_USER:  
              Path   :=   'CURRENT_USER\'   +   Path;  
          HKEY_CLASSES_ROOT:  
              Path   :=   'CLASSES_ROOT\'   +   Path;  
          HKEY_USERS:  
              Path   :=   'USERS\'   +   Path;  
      end;  
      Len   :=   (Length(Path)+1)*SizeOf(WideChar);  
      GetMem(WidePath,Len);  
      MultiByteToWideChar(CP_ACP,   MB_PRECOMPOSED,   PChar(Path),   -1,   WidePath,   Len);  
      Result   :=   SetNamedSecurityInfoW(WidePath,   SE_REGISTRY_KEY,
          DACL_SECURITY_INFORMATION,   nil,   nil,   nil,   nil)   =   ERROR_SUCCESS;  
      FreeMem(WidePath);  
end;

这里引用的两个unit来自于JCL1.94。以后有空再将里面没用的部分剔除掉,然后单独做一个单元。


2007年2月写。

直到现在也没单独做:)

抱歉!评论已关闭.