此问题我以前已经回答了:
unit Services;
interface
const
RSP_SIMPLE_SERVICE = 1;
RSP_UNREGISTER_SERVICE = 0;
function W95ServiceRegister(dwType: LongWord): boolean;
procedure RegisterStartService(sName, sPath: string);
procedure UnRegisterStartService(sName: string);
implementation
uses
Windows, Registry;
function W95ServiceRegister(dwType: LongWord): boolean;
type
THookRegisterServiceProcess = function (dwProcessId, dwType: LongWord): LongWord;
stdcall;
var
hookRegisterServiceProcess: THookRegisterServiceProcess;
begin
@hookRegisterServiceProcess := GetProcAddress(GetModuleHandle('KERNEL32'), 'RegisterServiceProcess');
if @hookRegisterServiceProcess = nil then
begin
result := false;
end
else
begin
if (hookRegisterServiceProcess(Cardinal(nil), dwType) = 0) then
result := false
else
result := true;
end;
end;
procedure RegisterStartService(sName, sPath: string);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey('Software/Microsoft/Windows/CurrentVersion/RunServices', true);
Reg.WriteString(sName, sPath);
Reg.Free;
end;
procedure UnRegisterStartService(sName: string);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey('Software/Microsoft/Windows/CurrentVersion/RunServices', true);
Reg.DeleteValue(sName);
Reg.Free;
end;
end.