中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>系统函数>文章内容

delphi delphi实现服务开启与关闭

来源:国外 关于:Bertrand Goetzmann 发布时间:2007-07-03   [收藏] [推荐]
Problem/Question/Abstract:
The unit described in this article show how we can start or stop a NT service by programming, or getting the location of its binary implentation.
Answer:
unit SvcUtils;
// Written by Bertrand Goetzmann (http://www.object-everywhere.com)
// Keywords : Service, OpenSCManager, OpenService, CloseServiceHandle, QueryServiceConfig, StartService, QueryServiceStatus, ControlService
interface
// This function returns the entire path location of the implementation of the given name service
function GetBinaryPathName(const ServiceName: string): string;
// This function starts the service with the given service name
procedure StartService(const ServiceName: string);
// This function stops the service with the given service name
procedure StopService(const ServiceName: string);
implementation
uses SysUtils, WinSvc;
function GetBinaryPathName(const ServiceName: string): string;
var
  SvcMgr, Svc: Integer;
  QuerySvc: TQueryServiceConfig;
  BytesNeeded: Cardinal;
  Buffer: PQueryServiceConfig;
begin
  // Establish a connection to the service control manager
  SvcMgr := OpenSCManager(nil {*MachineName*}, nil {*DatabaseName*},
    SC_MANAGER_ALL_ACCESS);
  try
    if SvcMgr = 0 then
      RaiseLastOSError;
    Svc := OpenService(SvcMgr, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if Svc = 0 then
      RaiseLastOSError;
    try
      // Make a call to know the number of bytes needed
      QueryServiceConfig(Svc, @QuerySvc, 0, BytesNeeded);
      GetMem(Buffer, BytesNeeded);
      try
        if not QueryServiceConfig(Svc, Buffer, BytesNeeded, BytesNeeded) then
          RaiseLastOSError;
        Result := PQueryServiceConfig(Buffer).lpBinaryPathName;
      finally
        FreeMem(Buffer);
      end;
    finally
      CloseServiceHandle(Svc);
    end;
  finally
    CloseServiceHandle(SvcMgr);
  end;
end;
procedure StartService(const ServiceName: string);
var
  SvcMgr, Svc: Integer;
  ServiceArgVectors: PChar;
begin
  // Establish a connection to the service control manager
  SvcMgr := OpenSCManager(nil {*MachineName*}, nil {*DatabaseName*},
    SC_MANAGER_ALL_ACCESS);
  try
    if SvcMgr = 0 then
      RaiseLastOSError;
    Svc := OpenService(SvcMgr, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if Svc = 0 then
      RaiseLastOSError;
    try
      if not WinSvc.StartService(Svc, 0 {*NumServiceArgs*}, ServiceArgVectors) then
        RaiseLastOSError;
    finally
      CloseServiceHandle(Svc);
    end;
  finally
    CloseServiceHandle(SvcMgr);
  end;
end;
procedure StopService(const ServiceName: string);
var
  SvcMgr, Svc: Integer;
  ServiceStatus: _SERVICE_STATUS;
begin
  // Establish a connection to the service control manager
  SvcMgr := OpenSCManager(nil {*MachineName*}, nil {*DatabaseName*},
    SC_MANAGER_ALL_ACCESS);
  try
    if SvcMgr = 0 then
      RaiseLastOSError;
    Svc := OpenService(SvcMgr, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if Svc = 0 then
      RaiseLastOSError;
    try
      // if not QueryServiceStatus(Svc, ServiceStatus) then
      //  RaiseLastOSError;
      // You can test the ServiceStatus.dwCurrentState field
      if not ControlService(Svc, SERVICE_CONTROL_STOP, ServiceStatus) then
        RaiseLastOSError;
    finally
      CloseServiceHandle(Svc);
    end;
  finally
    CloseServiceHandle(SvcMgr);
  end;
end;
end.
FAQ:
As do to register my application as a service?
You can register your service application simply by executing it with the /INSTALL option.
How to pass multi-arguments to StartService?
You can pass some argument with the starting of a service with the call of the StartService function of the Win32 API. The StartService procedure of the SvcUtils unit makes a such call with 0 argument :
WinSvc.StartService(Svc, 0 (*NumServiceArgs*), ServiceArgVectors)
What is RaiseLastOSError?
RaiseLastOSError is a function from the SysUtils unit that permits to raise an exception for the last operating system error or library system error.
I did know how to manage this, but I'm looking for a way to have the list of all installed services. Do you have a solution for that?
I think that the EnumServicesStatus function from the Windows API is the solution.
The function enumerates services in the specified service control manager database. The name and status of each service are provided.
The code looks very nice, but one problem; what is it suposed to do?? Any insight on what this program can do to benefit us would be greatly apprecaited!
On the Windows NT plateform you can look all the installed services by executing MMC (Microsoft Managment Console). All these services can be handled by this GUI application (start, stop, suspend, etc.).
This article show the use of some of the Service Functions from the Window API to do the same things by programming

[浏览: 次]   
上一篇:delphi 多层数据访问   下一篇:delphi 清空回收站
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi中ShellExecute的妙用
·delphi 如何快速读取文本文件
·delphi 如何判断输入值是否中文
·delphi 在应用层截获键盘消息
·delphi 实时记录事件日志
·delphi 使MEMO自动滚动
·delphi 如何区分键盘两个Enter键
·delphi 切换界面的方法
·delphi 汉字输入法的编程及使用
·delphi Delphi程序输入法自动切换最简
·delphi 消息是由谁来发出又由谁来完成
·delphi 程序缩小为任务右下角的小图标
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932