1,593
社区成员
发帖
与我相关
我的任务
分享
procedure TForm1.Button1Click(Sender: TObject);
begin
CreateServices('stest',Application.exename);
end;
function CreateServices(Const SvrName,FilePath:String):Boolean;
var
Svc,Service:SC_HANDLE;
begin
Result:=False;
if FilePath ='' then Exit;
Svc := OpenSCManager(nil,nil,SC_MANAGER_CREATE_SERVICE); {连接服务数据库,参数:1,机器名,打开本机SCM用nil;2,
打开SCM数据库;3,访问权限}
if Svc<= 0 then
begin
showmessage(syserrormessage(getlasterror));
CloseServiceHandle(Svc);
Exit;
end;
try
Service := CreateService(Svc,PChar(SvrName), //服务控制句柄,指向服务器内部名称
PChar(SvrName), //显示服务名称
SERVICE_ALL_ACCESS, // 服务的访问类型
SERVICE_INTERACTIVE_PROCESS or SERVICE_WIN32_OWN_PROCESS, // 服务类型
SERVICE_AUTO_START,SERVICE_ERROR_NORMAL, //启动类型 错误控制类型
PChar(FilePath),nil,nil,nil,nil,nil); // 服务程序路径,组服务名称,组标识,依赖的服务,启动服务帐号,启动服务口令
if Service <= 0 then
begin
ShowMessage(SysErrorMessage(GetlastError));
Exit;
end;
CloseServiceHandle(Service);
CloseServiceHandle(Svc);
Result:=True;
except
CloseServiceHandle(Service);
CloseServiceHandle(Svc);
Exit;
end;
end;