Win8上Service程序及外部App调用此Service

本文介绍在Windows 8上搭建与管理服务程序的方法,包括解决权限问题、使用API进行服务安装、启动、停止等操作,并展示了如何通过外部程序控制服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Win8上Service程序及外部App调用此Service

tp://blog.youkuaiyun.com/lincyang/article/details/25305311 一.Service

借助MSDN上Win7 Service的Demo和《用VC++建立Service服务应用程序》,在Win8上经历各种磨难,终于跑起来自己改装的服务程序了。http://blog.youkuaiyun.com/lincyang/article/details/25305311

原来API基本没变,我所困惑的是Win7上直接运行都没有问题,在Win8上不可以。

报错:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. OpenSCManager failed w/err 0x00000005  

原来是Win8上权限的问题,也许我自己的Win7一启动就拥有了Admin权限吧。


下面直接进入正题,我整合了一下代码,共三个文件:main.c,Service.h, Service.cpp。(项目是控制台程序。)

main.c只是程序的入口,运行时接受参数。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #pragma region "Includes"  
  2. #include <stdio.h>  
  3. #include <windows.h>  
  4. #include "Service.h"  
  5. #pragma endregion  
  6.   
  7.   
  8. int wmain(int argc, wchar_t* argv[])  
  9. {  
  10.     if ((argc > 1) && ((*argv[1] == L'-' || (*argv[1] == L'/'))))  
  11.     {  
  12.         if (_wcsicmp(L"install", argv[1] + 1) == 0)  
  13.         {  
  14.             SvcInstall();  
  15.         }  
  16.         else if (_wcsicmp(L"remove", argv[1] + 1) == 0)  
  17.         {  
  18.             SvcUninstall();  
  19.         }  
  20.         else if (_wcsicmp(L"query", argv[1] + 1) == 0)  
  21.         {  
  22.             SvcQueryConfig();  
  23.         }  
  24.         else if(_wcsicmp(L"start",argv[1] + 1) == 0)  
  25.         {  
  26.             SvcStart();   
  27.         }  
  28.         else if(_wcsicmp(L"stop",argv[1] + 1) == 0)  
  29.         {  
  30.             SvcStopNow();   
  31.         }  
  32.     }  
  33.     else  
  34.     {  
  35.         _putws(L"Parameters:");  
  36.         _putws(L" -install    to install the service (require admin permission)");  
  37.         _putws(L" -remove     to remove the service (require admin permission)");  
  38.         _putws(L" -query      to query the configuration of the service");  
  39.                 _putws(L" -start      to start the service");  
  40.                 _putws(L" -stop       to stop the service");  
  41.   
  42.         RunService();  
  43.     }  
  44.   
  45.     return 0;  
  46. }  

代码中已经写的很清楚了,我的项目名称为Win8Service,只要运行Win8Service.exe -install,服务就会被安装。

注意:cmd必须要用admin启动。win8下做法:WIN+Q键,打开Search panel,输入cmd,右击Command Prompt,选择Run as administrator。


下面看看这几个函数的实现:

Service.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #pragma once  
  2.   
  3. // Internal name of the service  
  4. #define SERVICE_NAME             L"CppWin8Service"  
  5.   
  6. // Displayed name of the service  
  7. #define SERVICE_DISPLAY_NAME     L"CppWin8Service Demo"  
  8.   
  9. // List of service dependencies - "dep1\0dep2\0\0"  
  10. #define SERVICE_DEPENDENCIES     L""  
  11.   
  12. // The name of the account under which the service should run  
  13. #define SERVICE_ACCOUNT          L"NT AUTHORITY\\LocalService"  
  14.   
  15. // The password to the service account name  
  16. #define SERVICE_PASSWORD         NULL  
  17.   
  18. VOID RunService();  
  19. VOID SvcInstall();  
  20. VOID SvcUninstall();  
  21. VOID SvcQueryConfig();  
  22. BOOL SvcStart();  
  23. VOID SvcStopNow();  


Service.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #pragma region "Includes"  
  2. #include <stdio.h>  
  3. #include <windows.h>  
  4. #include "Service.h"  
  5. #pragma endregion  
  6.   
  7.   
  8. SERVICE_STATUS          g_ssSvcStatus;         // Current service status  
  9. SERVICE_STATUS_HANDLE   g_sshSvcStatusHandle;  // Current service status handle  
  10. HANDLE                  g_hSvcStopEvent;  
  11.   
  12.   
  13. VOID WINAPI SvcMain(DWORD dwArgc, LPWSTR* lpszArgv);  
  14. VOID WINAPI SvcCtrlHandler(DWORD dwCtrl);  
  15. VOID SvcInit(DWORD dwArgc, LPWSTR* lpszArgv);  
  16. VOID SvcStop();  
  17. VOID SvcReportStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint);  
  18. VOID SvcReportEvent(LPWSTR lpszFunction, DWORD dwErr = 0);  
  19.   
  20.   
  21. VOID RunService()  
  22. {  
  23.     // You can add any additional services for the process to this table.  
  24.     SERVICE_TABLE_ENTRY dispatchTable[] =   
  25.     {  
  26.         { SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)SvcMain },   
  27.         { NULL, NULL }  
  28.     };  
  29.   
  30.     // This call returns when the service has stopped.  
  31.     // The process should simply terminate when the call returns.  
  32.     if (!StartServiceCtrlDispatcher(dispatchTable))  
  33.     {  
  34.         SvcReportEvent(L"StartServiceCtrlDispatcher", GetLastError());  
  35.     }  
  36. }  
  37.   
  38.   
  39. VOID WINAPI SvcMain(DWORD dwArgc, LPWSTR* lpszArgv)  
  40. {  
  41.     SvcReportEvent(L"Enter SvcMain");  
  42.   
  43.     // Register the handler function for the service  
  44.     g_sshSvcStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME,   
  45.         SvcCtrlHandler);  
  46.     if (!g_sshSvcStatusHandle)  
  47.     {  
  48.         SvcReportEvent(L"RegisterServiceCtrlHandler", GetLastError());  
  49.         return;   
  50.     }   
  51.   
  52.     // These SERVICE_STATUS members remain as set here  
  53.     g_ssSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;   
  54.     g_ssSvcStatus.dwServiceSpecificExitCode = 0;  
  55.   
  56.     // Report initial status to the SCM  
  57.     SvcReportStatus(SERVICE_START_PENDING, NO_ERROR, 3000);  
  58.   
  59.     // Perform service-specific initialization and work.  
  60.     SvcInit(dwArgc, lpszArgv);  
  61. }  
  62.   
  63.   
  64. VOID WINAPI SvcCtrlHandler(DWORD dwCtrl)  
  65. {  
  66.     // Handle the requested control code.  
  67.     switch(dwCtrl)   
  68.     {    
  69.     case SERVICE_CONTROL_STOP:   
  70.         // Stop the service  
  71.   
  72.         // SERVICE_STOP_PENDING should be reported before setting the Stop   
  73.         // Event - g_hSvcStopEvent - in SvcStop(). This avoids a race   
  74.         // condition which may result in a 1053 - The Service did not   
  75.         // respond... error.  
  76.         SvcReportStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);  
  77.   
  78.         SvcStop();  
  79.   
  80.         SvcReportStatus(g_ssSvcStatus.dwCurrentState, NO_ERROR, 0);  
  81.   
  82.         return;  
  83.   
  84.     case SERVICE_CONTROL_INTERROGATE:   
  85.         break;   
  86.   
  87.     default:  
  88.         break;  
  89.     }   
  90.   
  91. }  
  92.   
  93.   
  94. VOID SvcInit(DWORD dwArgc, LPWSTR* lpszArgv)  
  95. {  
  96.     SvcReportEvent(L"Enter SvcInit");  
  97.   
  98.     /////////////////////////////////////////////////////////////////////////  
  99.     // Service initialization.  
  100.     //   
  101.   
  102.     // Declare and set any required variables. Be sure to periodically call   
  103.     // ReportSvcStatus() with SERVICE_START_PENDING. If initialization fails,   
  104.     // call ReportSvcStatus with SERVICE_STOPPED.  
  105.   
  106.     // Create a manual-reset event that is not signaled at first. The control   
  107.     // handler function, SvcCtrlHandler, signals this event when it receives   
  108.     // the stop control code.  
  109.     g_hSvcStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);  
  110.     if (g_hSvcStopEvent == NULL)  
  111.     {  
  112.         SvcReportStatus(SERVICE_STOPPED, NO_ERROR, 0);  
  113.         return;  
  114.     }  
  115.   
  116.     // Report running status when initialization is complete.  
  117.     SvcReportStatus(SERVICE_RUNNING, NO_ERROR, 0);  
  118.   
  119.   
  120.     /////////////////////////////////////////////////////////////////////////  
  121.     // Perform work until service stops.  
  122.     //   
  123.   
  124.     while(TRUE)  
  125.     {  
  126.         // Perform work ...  
  127.   
  128.         // Check whether to stop the service.  
  129.         WaitForSingleObject(g_hSvcStopEvent, INFINITE);  
  130.   
  131.         SvcReportStatus(SERVICE_STOPPED, NO_ERROR, 0);  
  132.         return;  
  133.     }  
  134. }  
  135.   
  136.   
  137. VOID SvcStop()  
  138. {  
  139.     SvcReportEvent(L"Enter SvcStop");  
  140.   
  141.     // Signal the service to stop.  
  142.     if (g_hSvcStopEvent)  
  143.     {  
  144.         SetEvent(g_hSvcStopEvent);  
  145.     }  
  146. }  
  147.   
  148.   
  149. VOID SvcReportStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,   
  150.                      DWORD dwWaitHint)  
  151. {  
  152.     static DWORD dwCheckPoint = 1;  
  153.   
  154.     // Fill in the SERVICE_STATUS structure.  
  155.   
  156.     g_ssSvcStatus.dwCurrentState = dwCurrentState;  
  157.     g_ssSvcStatus.dwWin32ExitCode = dwWin32ExitCode;  
  158.     g_ssSvcStatus.dwWaitHint = dwWaitHint;  
  159.   
  160.     g_ssSvcStatus.dwControlsAccepted =   
  161.         (dwCurrentState == SERVICE_START_PENDING) ?   
  162.         0 : SERVICE_ACCEPT_STOP;  
  163.   
  164.     g_ssSvcStatus.dwCheckPoint =   
  165.         ((dwCurrentState == SERVICE_RUNNING) ||   
  166.         (dwCurrentState == SERVICE_STOPPED)) ?   
  167.         0 : dwCheckPoint++;  
  168.   
  169.     // Report the status of the service to the SCM.  
  170.     SetServiceStatus(g_sshSvcStatusHandle, &g_ssSvcStatus);  
  171. }  
  172.   
  173.   
  174. VOID SvcReportEvent(LPWSTR lpszFunction, DWORD dwErr)   
  175. {  
  176.     HANDLE hEventSource;  
  177.     LPCWSTR lpszStrings[2];  
  178.     wchar_t szBuffer[80];  
  179.   
  180.     hEventSource = RegisterEventSource(NULL, SERVICE_NAME);  
  181.     if (NULL != hEventSource)  
  182.     {  
  183.         WORD wType;  
  184.         if (dwErr == 0)  
  185.         {  
  186.             swprintf_s(szBuffer, ARRAYSIZE(szBuffer), lpszFunction);  
  187.             wType = EVENTLOG_INFORMATION_TYPE;  
  188.         }  
  189.         else  
  190.         {  
  191.             swprintf_s(szBuffer, ARRAYSIZE(szBuffer), L"%s failed w/err 0x%08lx",   
  192.                 lpszFunction, dwErr);  
  193.             wType = EVENTLOG_ERROR_TYPE;  
  194.         }  
  195.   
  196.         lpszStrings[0] = SERVICE_NAME;  
  197.         lpszStrings[1] = szBuffer;  
  198.   
  199.         ReportEvent(hEventSource,  // Event log handle  
  200.             wType,                 // Event type  
  201.             0,                     // Event category  
  202.             0,                     // Event identifier  
  203.             NULL,                  // No security identifier  
  204.             2,                     // Size of lpszStrings array  
  205.             0,                     // No binary data  
  206.             lpszStrings,           // Array of strings  
  207.             NULL);                 // No binary data  
  208.   
  209.         DeregisterEventSource(hEventSource);  
  210.     }  
  211. }  
  212.   
  213. VOID SvcInstall()  
  214. {  
  215.     wchar_t szPath[MAX_PATH];  
  216.     if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)) == 0)  
  217.     {  
  218.         wprintf(L"GetModuleFileName failed w/err 0x%08lx\n", GetLastError());  
  219.         return;  
  220.     }  
  221.   
  222.     // Open the local default service control manager database  
  223.     SC_HANDLE schSCManager = OpenSCManager(NULL, NULL,   
  224.         SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE);  
  225.     if (!schSCManager)  
  226.     {  
  227.         wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());  
  228.         return;  
  229.     }  
  230.   
  231.     // Install the service into SCM by calling CreateService  
  232.     SC_HANDLE schService = CreateService(  
  233.         schSCManager,                   // SCManager database  
  234.         SERVICE_NAME,                   // Name of service  
  235.         SERVICE_DISPLAY_NAME,           // Name to display  
  236.         SERVICE_CHANGE_CONFIG,          // Desired access  
  237.         SERVICE_WIN32_OWN_PROCESS,      // Service type  
  238.         SERVICE_DEMAND_START,           // Start type  
  239.         SERVICE_ERROR_NORMAL,           // Error control type  
  240.         szPath,                         // Service's binary  
  241.         NULL,                           // No load ordering group  
  242.         NULL,                           // No tag identifier  
  243.         SERVICE_DEPENDENCIES,           // Dependencies  
  244.         SERVICE_ACCOUNT,                // Service running account  
  245.         SERVICE_PASSWORD);              // Password of the account  
  246.   
  247.     if (NULL != schService)  
  248.     {  
  249.         wprintf(L"%s installed.\n", SERVICE_DISPLAY_NAME);  
  250.   
  251.         CloseServiceHandle(schService);  
  252.     }  
  253.     else  
  254.     {  
  255.         wprintf(L"CreateService failed w/err 0x%08lx\n", GetLastError());  
  256.     }  
  257.   
  258.     CloseServiceHandle(schSCManager);  
  259. }  
  260.   
  261. VOID SvcUninstall()  
  262. {  
  263.     // Open the local default service control manager database  
  264.     SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);  
  265.     if (!schSCManager)  
  266.     {  
  267.         wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());  
  268.         return;  
  269.     }  
  270.   
  271.     // Open the service with delete, stop and query status permissions  
  272.     SC_HANDLE schService = OpenService(schSCManager, SERVICE_NAME,   
  273.         DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);  
  274.   
  275.     if (NULL != schService)  
  276.     {  
  277.         // Try to stop the service  
  278.         SERVICE_STATUS ssSvcStatus;  
  279.         if (ControlService(schService, SERVICE_CONTROL_STOP, &ssSvcStatus))  
  280.         {  
  281.             wprintf(L"Stopping %s.", SERVICE_DISPLAY_NAME);  
  282.             Sleep(1000);  
  283.   
  284.             while (QueryServiceStatus(schService, &ssSvcStatus))  
  285.             {  
  286.                 if (ssSvcStatus.dwCurrentState == SERVICE_STOP_PENDING)  
  287.                 {  
  288.                     wprintf(L".");  
  289.                     Sleep(1000);  
  290.                 }  
  291.                 else break;  
  292.             }  
  293.   
  294.             if (ssSvcStatus.dwCurrentState == SERVICE_STOPPED)  
  295.             {  
  296.                 wprintf(L"\n%s stopped.\n", SERVICE_DISPLAY_NAME);  
  297.             }  
  298.             else  
  299.             {  
  300.                 wprintf(L"\n%s failed to stop.\n", SERVICE_DISPLAY_NAME);  
  301.             }  
  302.         }  
  303.   
  304.         // Now remove the service by calling DeleteService  
  305.         if (DeleteService(schService))  
  306.         {  
  307.             wprintf(L"%s removed.\n", SERVICE_DISPLAY_NAME);  
  308.         }  
  309.         else  
  310.         {  
  311.             wprintf(L"DeleteService failed w/err 0x%08lx\n", GetLastError());  
  312.         }  
  313.   
  314.         CloseServiceHandle(schService);  
  315.     }  
  316.     else  
  317.     {  
  318.         wprintf(L"OpenService failed w/err 0x%08lx\n", GetLastError());  
  319.     }  
  320.   
  321.     CloseServiceHandle(schSCManager);  
  322. }  
  323.   
  324. VOID SvcQueryConfig()  
  325. {  
  326.     // Open the local default service control manager database  
  327.     SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);  
  328.     if (!schSCManager)  
  329.     {  
  330.         wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());  
  331.         return;  
  332.     }  
  333.   
  334.     // Try to open the service to query its status and config  
  335.     SC_HANDLE schService = OpenService(schSCManager, SERVICE_NAME,   
  336.         SERVICE_QUERY_STATUS | SERVICE_QUERY_CONFIG);  
  337.   
  338.     if (NULL != schService)  
  339.     {  
  340.         wprintf(L"%s was installed.\n", SERVICE_DISPLAY_NAME);  
  341.   
  342.         DWORD cbBytesNeeded;  
  343.   
  344.         //   
  345.         // Query the status of the service  
  346.         //   
  347.   
  348.         SERVICE_STATUS_PROCESS ssp;  
  349.         if (QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp,   
  350.             sizeof(ssp), &cbBytesNeeded))  
  351.         {  
  352.             wprintf(L"Service status: ");  
  353.             switch (ssp.dwCurrentState)  
  354.             {  
  355.             case SERVICE_STOPPED: _putws(L"Stopped"); break;  
  356.             case SERVICE_RUNNING: _putws(L"Running"); break;  
  357.             case SERVICE_PAUSED: _putws(L"Paused"); break;  
  358.             case SERVICE_START_PENDING:  
  359.             case SERVICE_STOP_PENDING:  
  360.             case SERVICE_CONTINUE_PENDING:  
  361.             case SERVICE_PAUSE_PENDING: _putws(L"Pending"); break;  
  362.             }  
  363.         }  
  364.         else  
  365.         {  
  366.             wprintf(L"QueryServiceStatusEx failed w/err 0x%08lx\n", GetLastError());  
  367.         }  
  368.           
  369.         CloseServiceHandle(schService);  
  370.     }  
  371.     else  
  372.     {  
  373.         DWORD dwErr = GetLastError();  
  374.         if (dwErr == ERROR_SERVICE_DOES_NOT_EXIST)  
  375.         {  
  376.             wprintf(L"%s was not installed.\n", SERVICE_DISPLAY_NAME);  
  377.         }  
  378.         else  
  379.         {  
  380.             wprintf(L"OpenService failed w/err 0x%08lx\n", dwErr);  
  381.         }  
  382.     }  
  383.   
  384.     CloseServiceHandle(schSCManager);  
  385. }  
  386.   
  387. BOOL SvcStart()   
  388. {   
  389.     // run service with given name  
  390.     SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS);   
  391.     if (schSCManager==0)   
  392.     {  
  393.         long nError = GetLastError();  
  394.         wprintf(L"OpenSCManager failed, error code = %d\n", nError);  
  395.     }  
  396.     else  
  397.     {  
  398.         // open the service  
  399.         SC_HANDLE schService = OpenService( schSCManager, SERVICE_NAME, SERVICE_ALL_ACCESS);  
  400.         if (schService==0)   
  401.         {  
  402.             long nError = GetLastError();  
  403.             wprintf(L"OpenService failed, error code = %d\n", nError);  
  404.         }  
  405.         else  
  406.         {  
  407.             // call StartService to run the service  
  408.             if(StartService(schService, 0, (LPCWSTR*)NULL))  
  409.             {  
  410.                 wprintf(L"%s started.\n", SERVICE_DISPLAY_NAME);  
  411.                 CloseServiceHandle(schService);   
  412.                 CloseServiceHandle(schSCManager);   
  413.                 return TRUE;  
  414.             }  
  415.             else  
  416.             {  
  417.                 long nError = GetLastError();  
  418.                 wprintf(L"StartService failed, error code = %d\n", nError);  
  419.             }  
  420.             CloseServiceHandle(schService);   
  421.         }  
  422.         CloseServiceHandle(schSCManager);   
  423.     }  
  424.     return FALSE;  
  425. }  
  426.   
  427. VOID SvcStopNow()   
  428. {  
  429.     // Open the local default service control manager database  
  430.     SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);  
  431.     if (!schSCManager)  
  432.     {  
  433.         wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());  
  434.         return;  
  435.     }  
  436.   
  437.     // Open the service with delete, stop and query status permissions  
  438.     SC_HANDLE schService = OpenService(schSCManager, SERVICE_NAME,   
  439.         DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);  
  440.   
  441.     if (NULL != schService)  
  442.     {  
  443.         // Try to stop the service  
  444.         SERVICE_STATUS ssSvcStatus;  
  445.         if (ControlService(schService, SERVICE_CONTROL_STOP, &ssSvcStatus))  
  446.         {  
  447.             wprintf(L"Stopping %s.", SERVICE_DISPLAY_NAME);  
  448.             Sleep(1000);  
  449.   
  450.             while (QueryServiceStatus(schService, &ssSvcStatus))  
  451.             {  
  452.                 if (ssSvcStatus.dwCurrentState == SERVICE_STOP_PENDING)  
  453.                 {  
  454.                     wprintf(L".");  
  455.                     Sleep(1000);  
  456.                 }  
  457.                 else break;  
  458.             }  
  459.   
  460.             if (ssSvcStatus.dwCurrentState == SERVICE_STOPPED)  
  461.             {  
  462.                 wprintf(L"\n%s stopped.\n", SERVICE_DISPLAY_NAME);  
  463.             }  
  464.             else  
  465.             {  
  466.                 wprintf(L"\n%s failed to stop.\n", SERVICE_DISPLAY_NAME);  
  467.             }  
  468.         }  
  469.   
  470.         CloseServiceHandle(schService);  
  471.     }  
  472.     else  
  473.     {  
  474.         wprintf(L"OpenService failed w/err 0x%08lx\n", GetLastError());  
  475.     }  
  476.   
  477.     CloseServiceHandle(schSCManager);  
  478. }  

下面看看运行结果如何。

WIN+R,调出RUN,输入services.msc调出Service管理。在这里你就会看见CppWin8Service Demo,点击一下,会发现其没有运行。

我们在刚刚install的控制台继续运行:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. D:\honeywell\workspace\Win8Service\Debug>Win8Service.exe -install  
  2. CppWin8Service Demo installed.  
  3.   
  4. D:\honeywell\workspace\Win8Service\Debug>Win8Service.exe -start  
  5. CppWin8Service Demo started.  

刷新一下Service,会看到此服务以及启动。

打开事件查看器(运行eventvwr.msc),Windows logs---》Application中可以看到CppWin8Service的infomation,比如:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CppWin8Service  
  2. Enter SvcInit  

二、外部程序启动和停止此服务

服务跑起来了,那么我们如何在外部程序中控制它呢?其实很简单,就是用上面的SvcStart和SvcStopNow方法来做就可以了。

我们新建一个MFC对话框程序,加两个button,一个启动一个停止。将两个函数拷进去,然后包含一下此头文件就可以了。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <winsvc.h>   


三、批处理安装服务程序

说白了,就是用sc命令来安装启动服务程序,用批处理来包装一下,注意运行批处理时也要用admin。

sc命令见sc命令创建启动服务

Service安装和启动的bat:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @echo. start service!  
  2. @echo off  
  3.   
  4. @sc create LincTestServer binPath= "D:\XXX\Debug\NewService.exe"  
  5.   
  6. @sc start LincTestServer   
  7.   
  8. @sc config LincTestServer start= AUTO  
  9.   
  10. @sc config LincTestServer displayname="linc service"  
  11. @echo off  
  12.   
  13. @echo. start ok!  
  14. @pause  


停止的bat:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @echo.stop service  
  2. @echo off  
  3.   
  4. @sc stop LincTestServer   
  5. @echo off  
  6.   
  7. @echo.service stoped  
  8.   
  9. @pause  


卸载的bat:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @echo.delete service  
  2. @echo off  
  3.   
  4. @sc delete LincTestServer   
  5. @echo off  
  6.   
  7. @echo.service deleted  
  8.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值