1,183
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSvc, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function InstallService(SvcName, SvcDispName, SvcFullPath: string): integer;
var
hSCM, hService: SC_HANDLE;
sInfo: string;
begin
// 这里要填注册表信息,要不不会自动启动,因为我那个单元封装了,所以没贴上来
result := 0;
hSCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if hSCM = 0 then
begin
result := GetLastError;
exit;
end;
try
hService := CreateService(hSCM,
PChar(SvcName),
PChar(SvcDispName),
SERVICE_ALL_ACCESS, //SERVICE_START OR SERVICE_QUERY_STATUS OR _DELETE,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START, //SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
PChar(SvcFullPath),
nil, nil, nil, nil, nil);
if hService = 0 then
begin
result := GetLastError;
case result of
ERROR_ACCESS_DENIED: sInfo := 'ERROR_ACCESS_DENIED';
ERROR_CIRCULAR_DEPENDENCY: sInfo := 'ERROR_CIRCULAR_DEPENDENCY';
ERROR_DUPLICATE_SERVICE_NAME: sInfo := 'ERROR_DUPLICATE_SERVICE_NAME';
ERROR_INVALID_HANDLE: sInfo := 'ERROR_INVALID_HANDLE';
ERROR_INVALID_NAME: sInfo := 'ERROR_INVALID_NAME';
ERROR_INVALID_PARAMETER: sInfo := 'ERROR_INVALID_PARAMETER';
ERROR_INVALID_SERVICE_ACCOUNT: sInfo := 'ERROR_INVALID_SERVICE_ACCOUNT';
ERROR_SERVICE_EXISTS: sInfo := 'ERROR_SERVICE_EXISTS';
end;
end
else
CloseServiceHandle(hService);
finally
CloseServiceHandle(hSCM);
end;
// 这里也要填注册表信息
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
InstallService('Test', 'Test1', Application.ExeName);
end;
end.