很久没来了,DELPHI都快生疏了,请教个问题. ( 积分: 50 )

  • 主题发起人 主题发起人 北狼
  • 开始时间 开始时间

北狼

Unregistered / Unconfirmed
GUEST, unregistred user!
如何检测WINDOWS系统的某个服务的状态,就是管理工具里的那个服务里的项目,比如Windows Audio
要求程序启动的时候检测Windows Audio的状态,如果是 已启动 则...
如果没启动则启动它
请问该怎么处理????
 
参考这篇文章
http://www.spr1t3.com/blogview.asp?logID=121&cateID=2
或者搜索 Service Control Manger
 
晕,VC的,看不懂......
还是对你表示感谢.
 
这是服务程序,Tserver类,试试这个StBoot,就会在操作系统起动时起到服务.
 
找到了如下代码,可以控制
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;
我这样写是错的,不知道如何写?谢谢
 
用WMI?
查查MSDN
 
找到了如下代码,可以控制
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;
我这样写是错的,不知道如何写?谢谢
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部