//终于找到了一个能用的
//原文地址:http://bbs.znpc.net/viewthread.php?tid=3507
//关于 Vista 下启动、关闭服务的代码
//注意下面 OpenService 中的 access 参数,要 STOP, START, 而不能是 All,否则,系统不让访问,而报错:
//Access is Denied. (0x00000005)
// SSServices.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
#include <stdio.h>
int StopServiceT( char *szSerName )
{
char szDrvPath[MAX_PATH]={0};
char * _t=0;
SC_HANDLE hScHandle=0;
SC_HANDLE hSerHandle=0 ;
SERVICE_STATUS ssStatus;
int err =0;
if( strlen(szSerName)<=0 )
{printf("service name error"); return 0; }
hScHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hScHandle == NULL)
{ printf("open service manager failed: win error code: 0x%8x",GetLastError()); return 0 ; }
hSerHandle = OpenService(hScHandle, szSerName , SERVICE_STOP); // SERVICE_ALL_ACCESS);
if (hSerHandle != NULL)
{
printf(" 1 ");
if(ControlService(hSerHandle, SERVICE_CONTROL_STOP, &ssStatus)==0)
{
err=GetLastError();
printf("stop service failed: win error code: 0x%8x",err);
printf(" 2 ");
return 0;
}
//DeleteService(hSerHandle);
// Print the service status.
printf(" 3 ");
printf("/nStatus of Sample_Srv: /n");
printf(" Service Type: 0x%x/n", ssStatus.dwServiceType);
printf(" Current State: 0x%x/n", ssStatus.dwCurrentState);
printf(" Controls Accepted: 0x%x/n",
ssStatus.dwControlsAccepted);
printf(" Exit Code: %d/n", ssStatus.dwWin32ExitCode);
printf(" Service Specific Exit Code: %d/n",
ssStatus.dwServiceSpecificExitCode);
printf(" Check Point: %d/n", ssStatus.dwCheckPoint);
printf(" Wait Hint: %d/n", ssStatus.dwWaitHint);
}
printf(" 4 ");
CloseServiceHandle(hSerHandle);
CloseServiceHandle(hScHandle);
hScHandle =0 ;
hSerHandle = 0;
printf(" here? ");
return 1;
}
int StartServiceT(IN char *szServiceName )
{
char szDrvPath[MAX_PATH]={0};
SC_HANDLE hScm;
SC_HANDLE hServiceHandle ;
int err =0;
if( strlen(szServiceName)<=0 )
{printf("service name error"); return 0; }
hScm = OpenSCManager (NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
hServiceHandle = OpenService(hScm, // SCManager database
szServiceName, // name of service
SERVICE_START); //_ALL_ACCESS); // desired access
if (hServiceHandle == NULL )
{
err = GetLastError();
CloseServiceHandle(hScm); hScm=0;
printf("cannot start service '%s':win error code:0x%8x",szServiceName,err);
goto FAILED;
}
if ( StartService (hServiceHandle, // service identifier
0, // number of arguments
NULL // pointer to arguments
) == false )
{
err = GetLastError();
CloseServiceHandle(hScm); hScm =0 ;
printf("start service '%s' failed:win error code:0x%8x.",szServiceName,err);
//return 1;
}
CloseServiceHandle(hServiceHandle);
CloseServiceHandle(hScm);
hScm=0 ; hServiceHandle=0;
return 1;
FAILED:
return 0;
}
VOID ControlSampleService(DWORD fdwControl)
{
SERVICE_STATUS ssStatus;
SC_HANDLE schService;
DWORD fdwAccess;
DWORD dwStartTickCount, dwWaitTime;
// The required service object access depends on the control.
switch (fdwControl)
{
case SERVICE_CONTROL_STOP:
fdwAccess = SERVICE_STOP;
break;
case SERVICE_CONTROL_PAUSE:
case SERVICE_CONTROL_CONTINUE:
fdwAccess = SERVICE_PAUSE_CONTINUE;
break;
case SERVICE_CONTROL_INTERROGATE:
fdwAccess = SERVICE_INTERROGATE;
break;
default:
fdwAccess = SERVICE_INTERROGATE;
}
// Open a handle to the service.
schService = OpenService(
NULL, // SCManager database
"ScardSvr", // name of service
fdwAccess); // specify access
if (schService == NULL)
{
printf("OpenService");
return;
}
// Send a control value to the service.
if (! ControlService(
schService, // handle to service
fdwControl, // control value to send
&ssStatus) ) // address of status info
{
printf("ControlService");
return;
}
// Print the service status.
printf("/nStatus of Sample_Srv: /n");
printf(" Service Type: 0x%x/n", ssStatus.dwServiceType);
printf(" Current State: 0x%x/n", ssStatus.dwCurrentState);
printf(" Controls Accepted: 0x%x/n",
ssStatus.dwControlsAccepted);
printf(" Exit Code: %d/n", ssStatus.dwWin32ExitCode);
printf(" Service Specific Exit Code: %d/n",
ssStatus.dwServiceSpecificExitCode);
printf(" Check Point: %d/n", ssStatus.dwCheckPoint);
printf(" Wait Hint: %d/n", ssStatus.dwWaitHint);
return;
}
int main(int argc, char* argv[])
{
if(StopServiceT("SCardSvr"))
printf("/n Stop OK");
else
printf("/n Stop Failed.");
MessageBox( 0 , "1" , "ss" , MB_OK );
if(StartServiceT("SCardSvr"))
printf("/n Start OK");
else
printf("/n Start Failed.");
return 0;
}