• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            兔子的技術博客

            兔子

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              202 Posts :: 0 Stories :: 43 Comments :: 0 Trackbacks

            留言簿(10)

            最新評論

            閱讀排行榜

            評論排行榜

            環境 VC6.0

            #include "windows.h"

            SERVICE_STATUS          gSvcStatus;  //服務狀態

            SERVICE_STATUS_HANDLE   gSvcStatusHandle; //服務狀態句柄

            HANDLE                  ghSvcStopEvent = NULL;//服務停止句柄

            #define SERVER_NAME    TEXT("my_server") //服務名

            VOID WINAPI Server_main( DWORD, LPTSTR *); //服務入口點

            void ServerReportEvent(LPTSTR szName,LPTSTR szFunction); //寫Windows日志

            VOID ReportSvcStatus( DWORD, DWORD, DWORD ); //服務狀態和SCM交互

            VOID WINAPI SvcCtrlHandler( DWORD );  //SCM回調函數

            VOID ServerProgram(DWORD, LPTSTR *); //服務主程序

            void main()



                SERVICE_TABLE_ENTRY DispatchTable[] =

                {

                    { SERVER_NAME, (LPSERVICE_MAIN_FUNCTION)Server_main },

                    { NULL, NULL }

                };

                

                if (!StartServiceCtrlDispatcher(DispatchTable))

                {

                     ServerReportEvent(SERVER_NAME,TEXT("StartServiceCtrlDispatcher"));

                }

            }

            static VOID WINAPI Server_main(DWORD dwArgc, LPTSTR *lpszArgv )

            {

                // Register the handler function for the service

                gSvcStatusHandle = RegisterServiceCtrlHandler( 

                    SERVER_NAME, 

                    SvcCtrlHandler);

                

                if( !gSvcStatusHandle )

                { 

                   ServerReportEvent(SERVER_NAME,TEXT("RegisterServiceCtrlHandler")); 

                    return

                } 

                

                // These SERVICE_STATUS members remain as set here

                gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; //只有一個單獨的服務

                gSvcStatus.dwServiceSpecificExitCode = 0;    

                

                // Report initial status to the SCM

                ReportSvcStatus( SERVICE_START_PENDING, NO_ERROR, 3000 );

                

                // Perform service-specific initialization and work.

                ghSvcStopEvent = CreateEvent(

                    NULL,    // default security attributes

                    TRUE,    // manual reset event

                    FALSE,   // not signaled

                    NULL);   // no name

                

                if ( ghSvcStopEvent == NULL)

                {

                    ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );

                    return;

                }

                

                // Report running status when initialization is complete.

                ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );

                

                // TO_DO: Perform work until service stops. 

                ServerProgram(dwArgc, lpszArgv); //你需要的操作在此添加代碼

                

                while(1)

                {

                      // Check whether to stop the service.

                  WaitForSingleObject(ghSvcStopEvent, INFINITE);

                  ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );

                  return;

                }  

            }

            void ServerReportEvent(LPTSTR szName,LPTSTR szFunction) 



                HANDLE hEventSource;

                LPCTSTR lpszStrings[2];

                unsigned int len = sizeof(szFunction);

                TCHAR *Buffer = new TCHAR[len];

                

                hEventSource = RegisterEventSource(NULL, szName);

                

                if( NULL != hEventSource )

                {

                    //StringCchPrintf(Buffer, 80, TEXT("%s failed with %d"), szFunction, GetLastError());

                    strcpy(Buffer,szFunction);

                    lpszStrings[0] = szName;

                    lpszStrings[1] = Buffer;

                    

                    ReportEvent(hEventSource,        // event log handle

                        EVENTLOG_ERROR_TYPE, // event type

                        0,                   // event category

                        SVC_ERROR,           // event identifier

                        NULL,                // no security identifier

                        2,                   // size of lpszStrings array

                        0,                   // no binary data

                        lpszStrings,         // array of strings

                        NULL);               // no binary data    

                    DeregisterEventSource(hEventSource);

                }

            }

            VOID ReportSvcStatus( DWORD dwCurrentState,

                                 DWORD dwWin32ExitCode,

                                 DWORD dwWaitHint)

            {

                static DWORD dwCheckPoint = 1;

                

                // Fill in the SERVICE_STATUS structure.

                

                gSvcStatus.dwCurrentState = dwCurrentState;

                gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;

                gSvcStatus.dwWaitHint = dwWaitHint;

                

                if (dwCurrentState == SERVICE_START_PENDING)

                    gSvcStatus.dwControlsAccepted = 0;

                else gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;

                

                if ( (dwCurrentState == SERVICE_RUNNING) ||

                    (dwCurrentState == SERVICE_STOPPED) )

                    gSvcStatus.dwCheckPoint = 0;

                else gSvcStatus.dwCheckPoint = dwCheckPoint++;

                

                // Report the status of the service to the SCM.

                SetServiceStatus( gSvcStatusHandle, &gSvcStatus );

            }

            VOID WINAPI SvcCtrlHandler( DWORD dwCtrl )

            {

                // Handle the requested control code. 

              switch(dwCtrl) 

                {  

                case SERVICE_CONTROL_STOP: 

                    ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);

                    

                    // Signal the service to stop.

                    

                    SetEvent(ghSvcStopEvent);

                    

                    return;

                    

                case SERVICE_CONTROL_INTERROGATE: 

                    // Fall through to send current status.

                    break

                    

                default

                    break;

                } 

                ReportSvcStatus(gSvcStatus.dwCurrentState, NO_ERROR, 0);

            }
            轉自:http://3140618.blog.163.com/blog/static/745179720109286165959/
            posted on 2013-09-26 15:24 會飛的兔子 閱讀(2874) 評論(0)  編輯 收藏 引用 所屬分類: 系統API,底層技術
            2020国产成人久久精品| 久久亚洲精品国产亚洲老地址 | 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久精品无码一区二区WWW| 高清免费久久午夜精品| 精品久久久久久无码免费| 久久久精品国产sm调教网站| 中文字幕精品久久久久人妻| 国产精品久久网| 欧美日韩精品久久久免费观看| 国产精品一久久香蕉国产线看| 久久影院亚洲一区| 久久国产成人| 久久无码人妻一区二区三区 | 99久久精品国产综合一区| 久久精品中文闷骚内射| 精品久久人人爽天天玩人人妻| 亚洲狠狠婷婷综合久久久久 | 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 丁香色欲久久久久久综合网| 亚洲AV伊人久久青青草原| 久久亚洲私人国产精品| 波多野结衣久久| 青春久久| 久久亚洲精品国产亚洲老地址| 久久国产视频网| 久久久www免费人成精品| 久久综合狠狠综合久久97色| 精品999久久久久久中文字幕| 久久综合香蕉国产蜜臀AV| 色综合久久久久无码专区| 99精品久久久久久久婷婷| 一本久久a久久精品亚洲| 久久综合鬼色88久久精品综合自在自线噜噜 | 精品午夜久久福利大片| 久久精品人成免费| 无码人妻久久一区二区三区免费丨 | 亚洲精品国产美女久久久| 久久久久国产一级毛片高清板| 亚洲综合久久综合激情久久| 国产精品99久久久久久猫咪 |