bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Delphi 小巧的系統服務程式
查看: 5708|回復: 0
go

小巧的系統服務程式 [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2006-10-17 13:41 |只看該作者 |倒序瀏覽 |打印
  1. program DemoSrv;

  2. {Windows NT Service Demo Program for Delphi 3
  3. By Tom Lee , Taiwan , Repubilc of China ( Tomm.bbs@csie.nctu.edu.tw )
  4. JUL 8 1997
  5. ver 1.01
  6. The service will beep every 10 second .}

  7. uses SysUtils, Windows, WinSvc;

  8. const
  9.   ServiceName = 'TomDemoService';
  10.   ServiceDisplayName = 'd99 test Service';
  11.   SERVICE_WIN32_OWN_PROCESS = $00000010;
  12.   SERVICE_DEMAND_START = $00000003;
  13.   SERVICE_ERROR_NORMAL = $00000001;
  14.   EVENTLOG_ERROR_TYPE = $0001;

  15.   declare global variable
  16. var
  17.   ServiceStatusHandle SERVICE_STATUS_HANDLE;
  18.   ssStatus TServiceStatus;
  19.   dwErr DWORD;
  20.   ServiceTableEntry array[0..1] of TServiceTableEntry;
  21.   hServerStopEvent THandle;

  22.   Get error message

  23. function GetLastErrorText string;
  24. var
  25.   dwSize DWORD;
  26.   lpszTemp LPSTR;
  27. begin
  28.   dwSize = 512;
  29.   lpszTemp = nil;
  30.   try
  31.     GetMem(lpszTemp, dwSize);
  32.     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_ARGUMENT_ARRAY, nil, GetLastError, LANG_NEUTRAL, lpszTemp, dwSize, nil);
  33.   finally
  34.     Result = StrPas(lpszTemp);
  35.     FreeMem(lpszTemp);
  36.   end;
  37. end;

  38. //Write error message to Windows NT Event Log
  39. procedure AddToMessageLog(sMsg string);
  40. var
  41.   sString array[0..1] of string;
  42.   hEventSource THandle;
  43. begin
  44.   hEventSource = RegisterEventSource(nil, ServiceName);

  45.   if hEventSource 0 then
  46.   begin
  47.     sString[0] = ServiceName + ' error ' + IntToStr(dwErr);
  48.     sString[1] = sMsg;
  49.     ReportEvent(hEventSource, EVENTLOG_ERROR_TYPE, 0, 0, nil, 2, 0, @sString, nil);
  50.     DeregisterEventSource(hEventSource);
  51.   end;
  52. end;

  53. function ReportStatusToSCMgr(dwState, dwExitCode, dwWait DWORD) BOOL;
  54. begin
  55.   Result = True;
  56.   with ssStatus do
  57.   begin
  58.     if (dwState = SERVICE_START_PENDING) then
  59.       dwControlsAccepted = 0
  60.     else
  61.       dwControlsAccepted = SERVICE_ACCEPT_STOP;

  62.     dwCurrentState = dwState;
  63.     dwWin32ExitCode = dwExitCode;
  64.     dwWaitHint = dwWait;

  65.     if (dwState = SERVICE_RUNNING) or (dwState = SERVICE_STOPPED) then
  66.       dwCheckPoint = 0
  67.     else
  68.       inc(dwCheckPoint);
  69.   end;

  70.   Result = SetServiceStatus(ServiceStatusHandle, ssStatus);
  71.   if not Result then
  72.     AddToMessageLog('SetServiceStauts');
  73. end;

  74. procedure ServiceStop;
  75. begin
  76.   if (hServerStopEvent 0) then
  77.   begin
  78.     SetEvent(hServerStopEvent);
  79.   end;
  80. end;

  81. procedure ServiceStart;
  82. var
  83.   dwWait DWORD;
  84. begin
  85.   //Report Status
  86.   if not ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000) then exit;

  87.   //this event when it receives the stop control code.
  88.   hServerStopEvent = createEvent(nil, TRUE, False, nil);
  89.   if hServerStopEvent = 0 then
  90.   begin
  91.     AddToMessageLog('createEvent');
  92.     exit;
  93.   end;

  94.   if not ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0) then
  95.   begin
  96.     CloseHandle(hServerStopEvent);
  97.     exit;
  98.   end;

  99.   //Service now running , perform work until shutdown
  100.   while True do
  101.   begin
  102.     //Wait for Terminate
  103.     MessageBeep(1);
  104.     dwWait = WaitforSingleObject(hServerStopEvent, 1);
  105.     if dwWait = WAIT_OBJECT_0 then
  106.     begin
  107.       CloseHandle(hServerStopEvent);
  108.       exit;
  109.     end;
  110.     Sleep(1000 10);
  111.   end;
  112. end;

  113. procedure Handler(dwCtrlCode DWORD); stdcall;
  114. begin
  115.   //Handle the requested control code.
  116.   case dwCtrlCode of

  117.   SERVICE_CONTROL_STOP
  118.   begin
  119.     ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0);
  120.     ServiceStop;
  121.     ReportStatusToSCMgr(SERVICE_STOPPED, GetLastError, 0);
  122.     exit;
  123.   end;

  124.   SERVICE_CONTROL_INTERROGATE
  125.   begin
  126.   end;

  127.   SERVICE_CONTROL_PAUSE
  128.   begin
  129.   end;

  130.   SERVICE_CONTROL_CONTINUE
  131.   begin
  132.   end;

  133.   SERVICE_CONTROL_SHUTDOWN
  134.   begin
  135.   end;

  136.   //invalid control code
  137.   else
  138. end;

  139.   //update the service status.
  140.   ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0);
  141. end;

  142. procedure ServiceMain;
  143. begin
  144.   //Register the handler function with dispatcher;
  145.   ServiceStatusHandle = RegisterServiceCtrlHandler(ServiceName, ThandlerFunction(@Handler));
  146.   if ServiceStatusHandle = 0 then
  147.   begin
  148.     ReportStatusToSCMgr(SERVICE_STOPPED, GetLastError, 0);
  149.     exit;
  150.   end;

  151.   ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  152.   ssStatus.dwServiceSpecificExitCode = 0;
  153.   ssStatus.dwCheckPoint = 1;

  154.   //Report current status to SCM (Service Control Manager)
  155.   if not ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000) then
  156.   begin
  157.     ReportStatusToSCMgr(SERVICE_STOPPED, GetLastError, 0);
  158.     exit;
  159.   end;

  160.   Start Service
  161.   ServiceStart;
  162. end;

  163. procedure InstallService;
  164. var
  165.   schService SC_HANDLE;
  166.   schSCManager SC_HANDLE;
  167.   lpszPath LPSTR;
  168.   dwSize DWORD;
  169. begin
  170.   dwSize = 512;
  171.   GetMem(lpszPath, dwSize);
  172.   if GetModuleFileName(0, lpszPath, dwSize) = 0 then
  173.   begin
  174.     FreeMem(lpszPath);
  175.     Writeln('Unable to install ' + ServiceName + ',GetModuleFileName Fail.');
  176.     exit;
  177.   end;
  178.   FreeMem(lpszPath);

  179.   schSCManager = OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  180.   if (schSCManager 0) then
  181.   begin
  182.     schService = createService(schSCManager, ServiceName, ServiceDisplayName,
  183.     SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START,
  184.     SERVICE_ERROR_NORMAL, pchar(ParamStr(0)), nil, nil, nil, nil, nil);
  185.     if (schService 0) then
  186.     begin
  187.       Writeln('Install Ok.');
  188.       CloseServiceHandle(schService);
  189.     end
  190.     else
  191.       Writeln('Unable to install ' + ServiceName + ',createService Fail.');
  192.   end
  193.   else
  194.     Writeln('Unable to install ' + ServiceName + ',OpenSCManager Fail.');
  195. end;

  196. procedure UnInstallService;
  197. var
  198.   schService SC_HANDLE;
  199.   schSCManager SC_HANDLE;
  200. begin
  201.   schSCManager = OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  202.   if (schSCManager 0) then
  203.   begin
  204.     schService = OpenService(schSCManager, ServiceName, SERVICE_ALL_ACCESS);
  205.     if (schService 0) then
  206.     begin
  207.       //Try to stop service at first
  208.       if ControlService(schService, SERVICE_CONTROL_STOP, ssStatus) then
  209.       begin
  210.         Write('Stopping Service ');
  211.         Sleep(1000);
  212.         while (QueryServiceStatus(schService, ssStatus)) do
  213.         begin
  214.           if ssStatus.dwCurrentState = SERVICE_STOP_PENDING then
  215.           begin
  216.             Write('.');
  217.             Sleep(1000);
  218.           end
  219.           else
  220.             break;
  221.           end;
  222.           writeln;

  223.           if ssStatus.dwCurrentState = SERVICE_STOPPED then
  224.             Writeln('Service Stop Now')
  225.           else
  226.           begin
  227.             CloseServiceHandle(schService);
  228.             CloseServiceHandle(schSCManager);
  229.             Writeln('Service Stop Fail');
  230.             exit;
  231.           end;
  232.         end;

  233.         //Remove the service
  234.         if (deleteService(schService)) then
  235.           Writeln('Service Uninstall Ok.')
  236.         else
  237.           Writeln('deleteService fail (' + GetLastErrorText + ').');
  238.          
  239.         CloseServiceHandle(schService);
  240.       end
  241.       else
  242.         Writeln('OpenService fail (' + GetLastErrorText + ').');

  243.       CloseServiceHandle(schSCManager);
  244.     end
  245.     else
  246.       Writeln('OpenSCManager fail (' + GetLastErrorText + ').');
  247. end;

  248. //Main Program Begin
  249. begin
  250.   if (ParamCount = 1) then
  251.   begin
  252.     if ParamStr(1) = '' then
  253.     begin
  254.       Writeln('----------------------------------------');
  255.       Writeln('DEMOSRV usage help');
  256.       Writeln('----------------------------------------');
  257.       Writeln('DEMOSRV install to install the service');
  258.       Writeln('DEMOSRV remove to uninstall the service');
  259.       Writeln('DEMOSRV Help');
  260.       Halt;
  261.     end;

  262.     if Uppercase(ParamStr(1)) = 'INSTALL' then
  263.     begin
  264.       InstallService;
  265.       Halt;
  266.     end;

  267.     if Uppercase(ParamStr(1)) = 'REMOVE' then
  268.     begin
  269.       UnInstallService;
  270.       Halt;
  271.     end;
  272.   end;

  273. //Setup service table which define all services in this process
  274.   with ServiceTableEntry[0] do
  275.   begin
  276.     lpServiceName = ServiceName;
  277.     lpServiceProc = @ServiceMain;
  278.   end;

  279. //Last entry in the table must have nil values to designate the end of the table
  280.   with ServiceTableEntry[1] do
  281.   begin
  282.     lpServiceName = nil;
  283.     lpServiceProc = nil;
  284.   end;

  285.   if not StartServiceCtrlDispatcher(ServiceTableEntry[0]) then
  286.   begin
  287.     AddToMessageLog('StartServiceCtrlDispatcher Error!');
  288.     Halt;
  289.   end;
  290. end.
複製代碼
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-5-5 07:09 , Processed in 0.016001 second(s), 10 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.