当前位置:编程学习 > VC++ >>

VC++ 创建Windows服务

       首先,附上Windows创建服务的源代码,这个很好用的,大家一般都是选择的这个使用。

[cpp]
#include"XXXX.h"  //包含的头文件  
 
//定义全局函数变量  
void Init(); 
BOOL IsInstalled(); 
BOOL Install(); 
BOOL Uninstall(); 
void LogEvent(LPCTSTR pszFormat, ...); 
void WINAPI ServiceMain(); 
void WINAPI ServiceStrl(DWORD dwOpcode); 
 
TCHAR szServiceName[] = _T("WatchDog"); 
BOOL bInstall; 
SERVICE_STATUS_HANDLE hServiceStatus; 
SERVICE_STATUS status; 
DWORD dwThreadID; 
 
int APIENTRY WinMain(HINSTANCE hInstance, 
                     HINSTANCE hPrevInstance, 
                     LPSTR     lpCmdLine, 
                     int       nCmdShow) 

    Init(); 
 
    dwThreadID = ::GetCurrentThreadId(); 
 
    SERVICE_TABLE_ENTRY st[] = 
    { 
        { szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain }, 
        { NULL, NULL } 
    }; 
 
    if (stricmp(lpCmdLine, "/install") == 0) 
        { 
           Install(); 
        } 
    else if (stricmp(lpCmdLine, "/uninstall") == 0) 
        { 
            Uninstall(); 
        } 
    else 
        { 
         if (!::StartServiceCtrlDispatcher(st)) 
        { 
            LogEvent(_T("Register Service Main Function Error!")); 
        } 
      } 
 
    return 0; 

//*********************************************************  
//Functiopn:            Init  
//Description:          初始化  
//Calls:                main          
//          <author>niying <time>2006-8-10      <version>     <desc>  
//*********************************************************  
void Init() 

    hServiceStatus = NULL; 
    status.dwServiceType = SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS; 
    status.dwCurrentState = SERVICE_START_PENDING; 
    status.dwControlsAccepted = SERVICE_ACCEPT_STOP; 
    status.dwWin32ExitCode = 0; 
    status.dwServiceSpecificExitCode = 0; 
    status.dwCheckPoint = 0; 
    status.dwWaitHint = 0; 

 
//*********************************************************  
//Functiopn:            ServiceMain  
//Description:          服务主函数,这在里进行控制对服务控制的注册  
//          <author>niying <time>2006-8-10      <version>     <desc>  
//*********************************************************  
void WINAPI ServiceMain() 

    // Register the control request handler  
    status.dwCurrentState = SERVICE_START_PENDING; 
    status.dwControlsAccepted = SERVICE_ACCEPT_STOP; 
 
    //注册服务控制  
    hServiceStatus = RegisterServiceCtrlHandler(szServiceName, ServiceStrl); 
    if (hServiceStatus == NULL) 
    { 
        LogEvent(_T("Handler not installed")); 
        return; 
    } 
    SetServiceStatus(hServiceStatus, &status); 
 
    status.dwWin32ExitCode = S_OK; 
    status.dwCheckPoint = 0; 
    status.dwWaitHint = 0; 
    status.dwCurrentState = SERVICE_RUNNING; 
    SetServiceStatus(hServiceStatus, &status); 
 
    //模拟服务的运行。应用时将主要任务放于此即可  
        //可在此写上服务需要执行的代码,一般为死循环  
      while(1) 
    { 
           //循环干什么  
     }     
    status.dwCurrentState = SERVICE_STOPPED; 
    SetServiceStatus(hServiceStatus, &status); 
    OutputDebugString(_T("Service stopped")); 

 
//*********************************************************  
//Functiopn:            ServiceStrl  
//Description:          服务控制主函数,这里实现对服务的控制,  
//                      当在服务管理器上停止或其它操作时,将会运行此处代码  
//Calls:  
//Called By:  
//Table Accessed:  
//Table Updated:  
//Input:                dwOpcode:控易做图务的状态  
//Output:  
//Return:  
//Others:  
//History:  
//          <author>niying <time>2006-8-10      <version>     <desc>  
//*********************************************************  
void WINAPI ServiceStrl(DWORD dwOpcode) 

    switch (dwOpcode)

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,