2 dreamgirl zdz dreamgirl_zdz 于 2013.10.28 10:46 提问

服务程序不执行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(NULL,TRUE,TRUE,_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(4,tcBuffer);//第一次执行,获得实际内存。
if(dwNeededSize>4)
{
TCHAR pBuffer=new TCHAR[dwNeededSize];
DWORD dwNeededSize=GetLogicalDriveStrings(dwNeededSize,pBuffer);//获得路基驱动字符串
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(tcFile,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
hFile=CreateFile(tcFile,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwWrite=0;
WriteFile(hFile,_T("hello"),5,&dwWrite,NULL);
}
CloseHandle(hFile);
}
ipreLength+=_tcslen(pDrive);//跳过当前驱动器
ipreLength+=sizeof(TCHAR);//跳过“\0”
}
Sleep(500);//暂停500ms继续扫描
//如果g_hEvent无信号则暂停
WaitForSingleObject(g_hEvent,INFINITE);
}
}
}
void WINAPI ServiceMain(DWORD dwArgc,LPTSTR*lpszArgv)
{
//注册控制请求句柄
g_hServiceStatus=RegisterServiceCtrlHandler(SERVICE_NAME,Handler);
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(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(hSCM!=NULL)
{
//打开服务
SC_HANDLE hService=OpenService(hSCM,SERVICE_NAME,SERVICE_QUERY_CONFIG);
if(hService!=NULL)
{
bResult=TRUE;
CloseServiceHandle(hService);
}
CloseServiceHandle(hSCM);
}
return bResult;
}
//封装安装服务的函数Install
BOOL Install()
{
if(IsInstalled())//服务安装则直接返回真
return TRUE;
//打开服务控制管理器
SC_HANDLE hSCM=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(hSCM==NULL)
{
return FALSE;
}
TCHAR szFilePath[MAX_PATH];
DWORD dwlen=GetModuleFileName(NULL,szFilePath,MAX_PATH);
//判断程序路径是否包含空格,如果包含则给路径加上引号
if(_tcschr(szFilePath,' ')!=NULL)
{
dwlen+=3;
TCHAR*lpFilePath=new TCHAR[dwlen];
if(lpFilePath!=NULL)
{
_stprintf(lpFilePath,_T("\"%s\""),szFilePath);
_tcscpy(szFilePath,lpFilePath);
delete []lpFilePath;
}
}
//创建一个手动启动的服务
SC_HANDLE hService=CreateService(hSCM,SERVICE_NAME,SERVICE_NAME,SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,szFilePath,NULL,NULL,_T(""),NULL,NULL);
if(hService==NULL)
{
CloseServiceHandle(hSCM);
return FALSE;
}
StartService(hService,0,NULL);
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
return TRUE;
}
//卸载服务的函数Uninstall
BOOL Uninstall()
{
if(!IsInstalled())
return TRUE;
SC_HANDLE hSCM=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(hSCM==NULL)
{
return FALSE;
}
SC_HANDLE hService =OpenService(hSCM,SERVICE_NAME,SERVICE_STOP|DELETE);
if(hService==NULL)
{
CloseServiceHandle(hSCM);
return FALSE;
}
SERVICE_STATUS status;
//首先停止服务,确保服务能立即被删除
ControlService(hService,SERVICE_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_NAME,ServiceMain},
        {NULL,NULL}
    };
    StartServiceCtrlDispatcher(st);

// }
CloseHandle(g_hEvent);
return 0;
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!