关闭

vc编写系统服务

752人阅读 评论(0) 收藏 举报
#include
<
afx.h
>


void WINAPI ServiceMain(); // 服务主函数
void WINAPI ServiceStrl(DWORD); // 服务控制
void Init(); // 初始化
BOOL IsInstalled(); // 判断服务是否已安装
BOOL Install(); // 安装服务
BOOL UnInstall(); // 卸载服务
void Action(); // 服务任务
char chServiceName[] = " MyService " ; // 服务名称
char chServiceDisplayName[] = " MyService " ; // 服务显示名称
char chServiceDescription[] = " My service description. " ; // 服务描述
SERVICE_STATUS_HANDLE hServiceStatus; // 服务句柄
SERVICE_STATUS ServiceStatus; // 服务状态
DWORD dwThreadId; // 进程ID

// 程序主函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd)
{

// 初始化
Init();

// 获取进程ID
dwThreadId = ::GetCurrentThreadId();

// 判断命令行参数,决定动作
if (stricmp(lpCmdLine, " /install " ) == 0 )
if ( ! IsInstalled())
if ( ! Install())
return 1 ;
else if (stricmp(lpCmdLine, " /uninstall " ) == 0 )
if (IsInstalled())
if ( ! UnInstall())
return 1 ;
else
{
SERVICE_TABLE_ENTRY ServiceTableEntry[]
=
{
{chServiceName,(LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL,NULL}
};
::StartServiceCtrlDispatcher(ServiceTableEntry);
}
return 0 ;
}

// 初始化
void Init()
{
hServiceStatus
= NULL;
ServiceStatus.dwServiceType
= SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwCurrentState
= SERVICE_STOPPED;
ServiceStatus.dwControlsAccepted
= SERVICE_ACCEPT_STOP;
ServiceStatus.dwWin32ExitCode
= 0 ;
ServiceStatus.dwServiceSpecificExitCode
= 0 ;
ServiceStatus.dwCheckPoint
= 0 ;
ServiceStatus.dwWaitHint
= 0 ;
}

// 服务主函数
void WINAPI ServiceMain()
{
ServiceStatus.dwCurrentState
= SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted
= SERVICE_ACCEPT_STOP;

// 注册服务控制函数
hServiceStatus = RegisterServiceCtrlHandler(chServiceName,ServiceStrl);
if ( ! hServiceStatus)
{
MessageBox(NULL,
" 注册控制服务失败! " , "" ,MB_OK);
return ;
}

// 设置服务状态
SetServiceStatus(hServiceStatus, & ServiceStatus);

ServiceStatus.dwWin32ExitCode
= S_OK;
ServiceStatus.dwCheckPoint
= 0 ;
ServiceStatus.dwWaitHint
= 0 ;
ServiceStatus.dwCurrentState
= SERVICE_RUNNING;
SetServiceStatus(hServiceStatus,
& ServiceStatus);

// 服务任务部分
Action();

// 退出服务
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hServiceStatus,
& ServiceStatus);
}

// 服务控制函数
void WINAPI ServiceStrl(DWORD dwMsg)
{
switch (dwMsg)
{
case SERVICE_CONTROL_STOP:
ServiceStatus.dwCurrentState
= SERVICE_STOP_PENDING;
SetServiceStatus(hServiceStatus,
& ServiceStatus);
PostThreadMessage(dwThreadId,WM_CLOSE,
0 , 0 );
break ;
case SERVICE_CONTROL_PAUSE:
break ;
case SERVICE_CONTROL_CONTINUE:
break ;
case SERVICE_CONTROL_INTERROGATE:
break ;
case SERVICE_CONTROL_SHUTDOWN:
break ;
case SERVICE_CONTROL_PARAMCHANGE:
break ;
default :
break ;
}
}

// 安装服务
BOOL Install()
{
// 打开服务控制管理器
SC_HANDLE hSCM = ::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if ( ! hSCM)
return FALSE;

char chFilePathName[MAX_PATH];
CString strFileName;
char chSystemPath[MAX_PATH];
char * chNewFileName;

// 获取自身程序文件名和系统目录
::GetModuleFileName(NULL,chFilePathName,MAX_PATH);
strFileName
= chFilePathName;
while (strFileName.Find( " // " , 0 ) !=- 1 )
strFileName
= strFileName.Right(strFileName.GetLength() - strFileName.Find( " // " , 0 ) - 1 );

// 截取系统目录字符串,设置为system目录
::GetSystemDirectory(chSystemPath,MAX_PATH);
for ( int n = 0 ;(unsigned int )n < strlen(chSystemPath);n ++ )
{
if (chSystemPath[n] == ' 3 ' )
chSystemPath[n]
= ' /0 ' ;
}
chNewFileName
= strcat(chSystemPath, " // " );
chNewFileName
= strcat(chSystemPath,strFileName);

// 将自身复制到系统system目录下
::CopyFile(chFilePathName,chNewFileName,FALSE);

// 创建服务
SC_HANDLE hService = ::CreateService(hSCM,chServiceName,chServiceDisplayName,
SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,
chNewFileName,NULL,NULL,NULL,NULL,NULL);

// 设置服务描述
SERVICE_DESCRIPTION ServiceDescription;
ServiceDescription.lpDescription
= chServiceDescription;
::ChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,
& ServiceDescription);

// 释放句柄
CloseServiceHandle(hSCM);

if ( ! hService)
return FALSE;

// 释放句柄
CloseServiceHandle(hService);

MessageBox(NULL,
" Install done! " , "" ,MB_OK);

return TRUE;
}

// 判断服务是否已安装
BOOL IsInstalled()
{
// 打开服务控制管理器
SC_HANDLE hSCM = ::OpenSCManager(NULL,NULL,SERVICE_ALL_ACCESS);
if ( ! hSCM)
return FALSE;

// 打开服务
SC_HANDLE hService = ::OpenService(hSCM,chServiceName,SERVICE_QUERY_CONFIG);
if ( ! hService)
return FALSE;

// 释放句柄
CloseServiceHandle(hSCM);
CloseServiceHandle(hService);

return TRUE;
}

// 卸载服务
BOOL UnInstall()
{
// 打开服务控制管理器
SC_HANDLE hSCM = ::OpenSCManager(NULL,NULL,SERVICE_ALL_ACCESS);
if ( ! hSCM)
return FALSE;

// 打开服务
SC_HANDLE hService = ::OpenService(hSCM,chServiceName,SERVICE_STOP | DELETE);
if ( ! hService)
return FALSE;

// 停止服务
SERVICE_STATUS ServiceStatus;
::ControlService(hService,SERVICE_CONTROL_STOP,
& ServiceStatus);

// 删除服务
if (::DeleteService(hService))
return FALSE;

// 释放句柄
CloseServiceHandle(hSCM);
CloseServiceHandle(hService);

MessageBox(NULL,
" UnInstall done! " , "" ,MB_OK);

return TRUE;
}

// 服务任务
void Action()
{
MessageBox(NULL,
" Service running! " , " NULL " ,MB_OK);
Sleep(
10000 );
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14463次
    • 积分:254
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论