• <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 會飛的兔子 閱讀(2875) 評論(0)  編輯 收藏 引用 所屬分類: 系統API,底層技術
            精品国产日韩久久亚洲| 72种姿势欧美久久久久大黄蕉| 国产精品成人无码久久久久久| 香蕉aa三级久久毛片| 亚洲色大成网站www久久九| 国产精品久久久天天影视| 久久精品成人影院| 国产成人久久精品一区二区三区 | 欧美熟妇另类久久久久久不卡 | 99久久免费国产精品特黄| .精品久久久麻豆国产精品| 久久国产精品波多野结衣AV| 伊人久久久AV老熟妇色| 久久露脸国产精品| 九九久久99综合一区二区| 伊人久久大香线蕉综合影院首页| 一本色道久久88加勒比—综合| 国产精品99久久久精品无码| 激情五月综合综合久久69| 久久精品国产69国产精品亚洲 | 国产—久久香蕉国产线看观看| 亚洲精品乱码久久久久66| 久久夜色精品国产| 国产ww久久久久久久久久| 好久久免费视频高清| 久久精品aⅴ无码中文字字幕重口| 色综合久久中文字幕综合网| 99久久伊人精品综合观看| 久久亚洲综合色一区二区三区| 奇米影视7777久久精品| 亚洲色大成网站www久久九| 久久精品中文字幕一区| 99久久做夜夜爱天天做精品| 亚洲精品高清一二区久久| 久久婷婷五月综合色99啪ak| 理论片午午伦夜理片久久 | 日韩亚洲欧美久久久www综合网| 久久国产亚洲高清观看| 国产国产成人精品久久| 蜜桃麻豆WWW久久囤产精品| 7777久久久国产精品消防器材|