Kendy--DELPHI加注册表自启动的最简单代码

来源:百度文库 编辑:神马文学网 时间:2024/04/23 16:46:59
.style1 { FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #ff6699}.yjx { BORDER-RIGHT: #f0f0f0 0px solid; BORDER-TOP: #f0f0f0 0px solid; BORDER-LEFT: #f0f0f0 0px solid; BORDER-BOTTOM: #f0f0f0 0px solid}[Delphi]DELPHI加注册表自启动的最简单代码作者:jondy   来源:jondy‘s Blog(http://zhack.blog.163.com/)

[感谢jondy朋友的热心!把他更新后的代码贴上来,这个更适合写小软件]

program  exe;
uses
 windows;
// 注册表新建键值的函数
procedure CreateKey(const RootKey : HKey; Key, ValueName, Value: string);
var
  Handle: HKey;
  Res,
  Disposition: Integer;
begin
  Res := RegCreateKeyEx(RootKey, PChar(Key), 0, ‘‘,
    REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, Handle, @Disposition);
  if Res = 0 then begin
    Res := RegSetValueEx(Handle, PChar(ValueName), 0,
      REG_SZ, PChar(Value), Length(Value) + 1);
    RegCloseKey(Handle)
  end;
end;
begin      //  跟位置名、文件路径
 CreateKey(HKEY_LOCAL_MACHINE,‘SoftWare\Microsoft\Windows\CurrentVersion\Run‘,‘AutoRun‘,‘C:\WINDOWS\regedit.exe‘);
end.
//下面是以前的代码,由于引用了registry单元,程序会增大40K左右
uses
registry;var reg:tregistry; begin reg:=tregistry.create; reg.rootkey:=HKEY_LOCAL_MACHINE; reg.openkey(‘SOFTWARE\Microsoft\Windows\CurrentVersion\Run‘,true); reg.WriteString(‘ScanRegistry‘,‘mir47.EXE‘); reg.closekey; reg.free; end.