启动、关闭、刷新 WINDOWS服务
1 SC_HANDLE a,b;
2 // SERVICE_STATUS ss;
3 try{
4 a=NULL;b=NULL;
5 a=OpenSCManager((edtCsIp->Text).c_str(), SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
6 if (a==NULL) {
7 setStatusBar("服务启动失败1");
8 Application->MessageBox("服务启动失败1。", "提示", MB_OK + MB_ICONSTOP);
9 return;
10 }
11 b=OpenService(a,edtFw1->Text.c_str(),SERVICE_START);
12 if(b==NULL){
13 CloseServiceHandle(a);
14 setStatusBar("服务启动失败2");
15 Application->MessageBox("服务启动失败2。", "提示", MB_OK + MB_ICONSTOP);
16 return;
17 }
18 StartService(b,0,NULL);//开始Service
19 Idglobal::Sleep(5000);
20 CloseServiceHandle(a);
21 CloseServiceHandle(b);
22 setStatusBar("服务启动完成");
23 sbRefreshClick(Sender);
24 Application->MessageBox("服务启动完成。", "提示", MB_OK + MB_ICONINFORMATION);
25
26 }catch(Exception &E){
27 setStatusBar("服务启动失败");
28 Application->MessageBox(("服务启动失败,原因【"+E.Message +"】").c_str(), "提示", MB_OK +
29 MB_ICONSTOP);
30 }
1 SC_HANDLE a,b;
2 SERVICE_STATUS ss;
3 try{
4 a=NULL;b=NULL;
5 a=OpenSCManager((edtCsIp->Text).c_str(), SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
6 if (a==NULL) {
7 setStatusBar("服务获取失败1");
8 Application->MessageBox("服务获取失败1。", "提示", MB_OK + MB_ICONSTOP);
9 return;
10 }
11
12 b=OpenService(a, edtFw1->Text.c_str(), SERVICE_QUERY_STATUS);
13 // Application->ProcessMessages();
14 if (b==NULL) {
15 CloseServiceHandle(a);
16 setStatusBar("服务获取失败2");
17 Application->MessageBox("服务获取失败2。", "提示", MB_OK + MB_ICONSTOP);
18 return;
19 }
20 bool c = QueryServiceStatus(b,&ss);
21
22 // Idglobal::Sleep(5000);
23 // Application->ProcessMessages();
24 // Application->MessageBox("服务获取成功。", "提示", MB_OK + MB_ICONINFORMATION);
25
26 if (c==false) {
27 CloseServiceHandle(a);
28 CloseServiceHandle(b);
29 setStatusBar("服务获取失败2");
30 Application->MessageBox("服务获取失败3。", "提示", MB_OK + MB_ICONSTOP);
31 return;
32 }
33 switch (ss.dwCurrentState ){
34 case SERVICE_RUNNING:{
35 lblZt1->Font->Color = clGreen;
36 lblZt1->Caption = "运行中";
37 setStatusBar("服务"+edtFw1->Text+"运行中");
38 break;
39 }
40 default:{
41 lblZt1->Font->Color = clRed;
42 lblZt1->Caption = "已停止";
43 setStatusBar("服务"+edtFw1->Text+"已停止");
44 }
45 }
46 // Application->MessageBox("服务获取成功。", "提示", MB_OK + MB_ICONINFORMATION);
47 CloseServiceHandle(a);
48 CloseServiceHandle(b);
49 }catch(Exception &E){
50
51 // CloseServiceHandle(a);
52 // CloseServiceHandle(b);
53 setStatusBar("服务状态获取失败");
54 Application->MessageBox(("服务状态获取失败,原因【"+E.Message +"】").c_str(), "提示", MB_OK +
55 MB_ICONSTOP);
56 }
1 // String sql = "NET STOP "+edtFw1->Text;
2 // WinExec(sql.c_str(),SW_SHOWDEFAULT);
3 SC_HANDLE a,b;
4 SERVICE_STATUS ss;
5 try{
6 a=NULL;b=NULL;
7 a=OpenSCManager((edtCsIp->Text).c_str(), SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
8 if (a==NULL) {
9 setStatusBar("服务停止失败1");
10 Application->MessageBox("服务停止失败1。", "提示", MB_OK + MB_ICONSTOP);
11 return;
12 }
13 b=OpenService(a,edtFw1->Text.c_str(),SERVICE_STOP|SERVICE_QUERY_STATUS);
14 if(b==NULL){
15 CloseServiceHandle(a);
16 setStatusBar("服务停止失败2");
17 Application->MessageBox("服务停止失败2。", "提示", MB_OK + MB_ICONSTOP);
18 return;
19 }
20 QueryServiceStatus(b,&ss);
21 if(ss.dwCurrentState == SERVICE_RUNNING || ss.dwCurrentState == SERVICE_PAUSED)
22 {
23 ControlService(b,SERVICE_CONTROL_STOP,&ss);
24 }
25
26 CloseServiceHandle(a);
27 CloseServiceHandle(b);
28 setStatusBar("服务停止成功");
29 Application->MessageBox("服务停止成功。", "提示", MB_OK + MB_ICONSTOP);
30 sbRefreshClick(Sender);
31 }catch(Exception &E){
32
33 setStatusBar("服务停止失败");
34 Application->MessageBox(("服务停止失败,原因【"+E.Message +"】").c_str(), "提示", MB_OK +
35 MB_ICONSTOP);
36 }