问题描述
- 服务程序不执行ServiceMain函数
- // 一个简单服务程序的开发.cpp : 定义控制台应用程序的入口点。
//#include ""stdafx.h""
#include
#include
#include
#include""string.h""
#include
/*首先定义宏作为服务名称*/
#define SERVICE_NAME _T(""FirstService"")
SERVICE_STATUS g_status;
SERVICE_STATUS_HANDLE g_hServiceStatus;
HANDLE g_hEvent=NULL;/*封装一个用于初始化全局变量的函数Init*/
void Init()
{
g_status.dwServiceType =SERVICE_WIN32_OWN_PROCESS;
g_status.dwCurrentState =SERVICE_STOPPED;
//设置服务可以使用的控制
//如果希望服务启动后不能停止,去掉SERVICE_ACCEPT_STOP
//SERVICE_ACCEPT_PAUSE_CONTINUE表示服务可以“暂停/继续""
g_status.dwControlsAccepted =SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_SHUTDOWN;
g_status.dwWin32ExitCode =0;
g_status.dwServiceSpecificExitCode =0;
g_status.dwCheckPoint =0;
g_status.dwWaitHint =0;
//创建事件为有信号的手动内核事件
g_hEvent=CreateEvent(NULLTRUETRUE_T(""Pause""));
}
//封装一个设置服务状态的函数SetStaus
void SetStatus(long lCurrentStatus)
{
g_status.dwCurrentState =lCurrentStatus;
SetServiceStatus(g_hServiceStatus&g_status);
}
/*封装服务控制函数Handle*/
void WINAPI Handler(DWORD dwOpcode)
{
switch(dwOpcode)
{
case SERVICE_CONTROL_STOP:
{
SetStatus(SERVICE_STOP_PENDING);
SetStatus(SERVICE_STOPPED);
}
break;
case SERVICE_CONTROL_PAUSE:
{
SetStatus(SERVICE_PAUSE_PENDING);
ResetEvent(g_hEvent);
SetStatus(SERVICE_PAUSED);
}
break;
case SERVICE_CONTROL_CONTINUE:
{
SetStatus(SERVICE_CONTINUE_PENDING);
SetEvent(g_hEvent);
SetStatus(SERVICE_RUNNING);
}
break;
case SERVICE_CONTROL_INTERROGATE:
break;
case SERVICE_CONTROL_SHUTDOWN:
{
SetStatus(SERVICE_STOP_PENDING);
SetStatus(SERVICE_STOPPED);
}
break;
default:
break;
}
}
//封装服务功能函数
void Run()
{
while(1)
{
TCHAR tcBuffer[4]={0};//准备较小内存,先试探实际需要内存大小
DWORD dwNeededSize=GetLogicalDriveStrings(4tcBuffer);//第一次执行,获得实际内存。
if(dwNeededSize>4)
{
TCHAR pBuffer=new TCHAR[dwNeededSize];
DWORD dwNeededSize=GetLogicalDriveStrings(dwNeededSizepBuffer);//获得路基驱动字符串
TCHAR *pDrive=pBuffer;
int ipreLength=0;
while(1)
{
pDrive=pBuffer+ipreLength;//获得下一个驱动器
if(pDrive[0]==0)
{
break;//退出循环
}
DWORD dwDriveType=GetDriveType(pDrive);//获取驱动器类型
if(dwDriveType==DRIVE_REMOVABLE)
{//如果是移动驱动器
TCHAR tcFile[MAX_PATH]={0};
_stprintf(tcFile_T(""%sa.txt"")pDrive);
//打开已存在的文件
HANDLE hFile=CreateFile(tcFileGENERIC_WRITE|GENERIC_READFILE_SHARE_READ|FILE_SHARE_WRITENULLOPEN_EXISTINGFILE_ATTRIBUTE_NORMALNULL);
if(hFile==INVALID_HANDLE_VALUE)
{
hFile=CreateFile(tcFileGENERIC_WRITE|GENERIC_READFILE_SHARE_READ|FILE_SHARE_WRITENULLCREATE_ALWAYSFILE_ATTRIBUTE_NORMALNULL);
DWORD dwWrite=0;
WriteFile(hFile_T(""hello"")5&dwWriteNULL);
}
CloseHandle(hFile);
}
ipreLength+=_tcslen(pDrive);//跳过当前驱动器
ipreLength+=sizeof(TCHAR);//跳过“”
}
Sleep(500);//暂停500ms继续扫描
//如果g_hEvent无信号则暂停
WaitForSingleObject(g_hEventINFINITE);
}
}
}
void WINAPI ServiceMain(DWORD dwArgcLPTSTR*lpszArgv)
{
//注册控制请求句柄
g_hServiceStatus=RegisterServiceCtrlHandler(SERVICE_NAMEHandler);
if(g_hServiceStatus==NULL)
return ;
SetStatus(SERVICE_START_PENDING);
SetStatus(SERVICE_RUNNING);
//当Run函数返回时,服务已经结束
Run();
g_status.dwWin32ExitCode =S_OK;
g_status.dwCheckPoint =0;
g_status.dwWaitHint =0;
g_status.dwCurrentState =SERVICE_STOPPED;
//设置服务状态为停止,从而退出服务
SetServiceStatus(g_hServiceStatus&g_status);
}
//判断服务是否安装函数IsInstalled
BOOL IsInstalled()
{
BOOL bResult=FALSE;
//打开控制管理器
SC_HANDLE hSCM=OpenSCManager(NULLNULLSC_MANAGER_ALL_ACCESS);
if(hSCM!=NULL)
{
//打开服务
SC_HANDLE hService=OpenService(hSCMSERVICE_NAMESERVICE_QUERY_CONFIG);
if(hService!=NULL)
{
bResult=TRUE;
CloseServiceHandle(hService);
}
CloseServiceHandle(hSCM);
}
return bResult;
}
//封装安装服务的函数Install
BOOL Install()
{
if(IsInstalled())//服务安装则直接返回真
return TRUE;
//打开服务控制管理器
SC_HANDLE hSCM=OpenSCManager(NULLNULLSC_MANAGER_ALL_ACCESS);
if(hSCM==NULL)
{
return FALSE;
}
TCHAR szFilePath[MAX_PATH];
DWORD dwlen=GetModuleFileName(NULLszFilePathMAX_PATH);
//判断程序路径是否包含空格,如果包含则给路径加上引号
if(_tcschr(szFilePath' ')!=NULL)
{
dwlen+=3;
TCHAR*lpFilePath=new TCHAR[dwlen];
if(lpFilePath!=NULL)
{
_stprintf(lpFilePath_T(""""%s"""")szFilePath);
_tcscpy(szFilePathlpFilePath);
delete []lpFilePath;
}
}
//创建一个手动启动的服务
SC_HANDLE hService=CreateService(hSCMSERVICE_NAMESERVICE_NAMESERVICE_ALL_ACCESSSERVICE_WIN32_OWN_PROCESSSERVICE_DEMAND_STARTSERVICE_ERROR_NORMALszFilePathNULLNULL_T("""")NULLNULL);
if(hService==NULL)
{
CloseServiceHandle(hSCM);
return FALSE;
}
StartService(hService0NULL);
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
return TRUE;
}
//卸载服务的函数Uninstall
BOOL Uninstall()
{
if(!IsInstalled())
return TRUE;
SC_HANDLE hSCM=OpenSCManager(NULLNULLSC_MANAGER_ALL_ACCESS);
if(hSCM==NULL)
{
return FALSE;
}
SC_HANDLE hService =OpenService(hSCMSERVICE_NAMESERVICE_STOP|DELETE);
if(hService==NULL)
{
CloseServiceHandle(hSCM);
return FALSE;
}
SERVICE_STATUS status;
//首先停止服务,确保服务能立即被删除
ControlService(hServiceSERVICE_CONTROL_STOP&status);
//删除服务
BOOL bDelete=DeleteService(hService);
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
if(bDelete)
return TRUE;
return FALSE;
}
int _tmain(int argc _TCHAR argv[])
{
Init();//初始化服务数据信息
/* if(argv[1]!=NULL&&_tcsicmp(argv[1]_T(""install""))==0)
{
Install();
}*/
// if(argv[1]!=NULL&&_tcsicmp(argv[1]_T(""uninstall""))==0)
//{
//Uninstall();
//}
/*wchar_t a=new wchar_t[10];
scanf(""%s""a);
if(a!=NULL&&_tcsicmp(a_T(""install""))==0)
{
Install();
}
if(a!=NULL&&_tcsicmp(a_T(""uninstall""))==0)
{
Uninstall();
}///else//{Install(); SERVICE_TABLE_ENTRY st[]= { {SERVICE_NAMEServiceMain} {NULLNULL} }; StartServiceCtrlDispatcher(st);
// }
CloseHandle(g_hEvent);
return 0;
}