找到了如下代码,可以控制
procedure SwitchService(ASwitch: Integer;
const AServiceName: string; AMessage: string);
var
lpServiceArgVectors: Pchar;
hscmanager,hService: SC_HANDLE;
ReturnStatus: TServiceStatus;
vServiceName: string;
begin
AMessage := 'Occur unknown error!';
vServiceName := AServiceName;
lpServiceArgVectors := nil;
hscmanager := OpenSCManager(nil,nil,SC_MANAGER_ENUMERATE_SERVICE);// 打开service control manager database
hService := OpenService(hscmanager,PChar(vServiceName),SERVICE_ALL_ACCESS);
//打开该Service
try
if(hService = 0) then
begin
AMessage := 'Unknown service!';
Exit;
end;
QueryServiceStatus(hService, ReturnStatus); //查看该Service的状态
if (ASwitch = 1) and (ReturnStatus.dwCurrentState = SERVICE_STOPPED) then //如果该Service已停止则启动它
begin
AMessage := 'Starting...';
try
StartService(hService,0,lpServiceArgVectors);
AMessage := 'Start success!';
except
AMessage := 'Start fail!';
end;
end
else if (ASwitch = 0) and (ReturnStatus.dwCurrentState <> SERVICE_STOPPED) then
begin
AMessage := 'Stopping...';
try
ControlService(hService, SERVICE_CONTROL_STOP, ReturnStatus);
AMessage := 'Stop success!';
except
AMessage := 'Stop fail!';
end;
end
else AMessage := 'The Service be ready!';
finally
CloseServiceHandle(hService);//关闭该Service
CloseServiceHandle(hscmanager);
end;
end;
但如何把Amessage在StatusBar1中显示出来?
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
begin
SwitchService(0,'pservice',StatusBar1.SimpleText);
// StatusBar1.SimpleText:=s;
end;
我这样写是错的,不知道如何写?谢谢