在Delphi里
1.想得到注册表中Run的列表,就像优化大师哪边可以,取消开机起动时要不要一起起动,
可以直接对它进行修改,删除.
2.想得到系统的服务列表,也是跟优化大师哪样的
可以直接用它来起动服务和关闭服务!

解决方案 »

  1.   

    服务:
    procedure TSer.GetServicesInfo;
    var
      tmpDisplayList: TStrings;
      i:integer;
      tmpStr:String;
    begin
            tmpDisplayList := TStringList.Create;
            ServiceGetList('',SERVICE_WIN32, SERVICE_STATE_ALL, tmpDisplayList );
            with lvService.Items do
            begin
              BeginUpdate;
              Clear;
              EndUpdate;
            end;
            for i:=0 to tmpDisplayList.Count -1 do
            begin
                   with lvService.Items.Add do
                   begin
                        Caption := tmpDisplayList[i];
                        tmpStr :=  ServiceGetKeyName('',tmpDisplayList[i]);
                        SubItems.Add(tmpStr);
                        if (ServiceStopped('',tmpStr)) then
                        begin
                            ImageIndex := 0;
                            SubItems.Add('Stop');
                        end
                        else
                        begin
                            ImageIndex := 1;
                            SubItems.Add('Run');
                        end;
                   end;
            end;
            tmpDisplayList.free;
    end;读取:GetServicesInfo直接调用就可以了.
    run:
      ServiceStart('', lvService.Selected.SubItems[0]);
      GetServicesInfo;Stop:
      ServiceStop('', lvService.Selected.SubItems[0]);
      GetServicesInfo;使用的时候增加下面这个单元文件://unit UProcessService: List Service and Operate Service;
    unit UProcessService;interfaceuses WinSvc,Classes,Windows,Sysutils;function ServiceGetList(
      sMachine : string;
      dwServiceType,
      dwServiceState : Dword;
      slServicesList : TStrings )
      : boolean;                            //List servicefunction ServiceGetKeyName(
      sMachine,
      sServiceDispName : string ) : string;    //convert displayname to keynamefunction ServiceGetDisplayName(
      sMachine,
      sServiceKeyName : string ) : string;     //conver keyname to displaynamefunction ServiceGetStatus(
      sMachine,
      sService : string ) : DWord;           //Get service statusfunction ServiceRunning(
      sMachine,
      sService : string ) : boolean;function ServiceStopped(
      sMachine,
      sService : string ) : boolean;function ServiceStart(
      sMachine,
      sService : string ) : boolean;function ServiceStop(
      sMachine,
      sService : string ) : boolean;
      implementation
    const
      //
      // Service Types
      //
      SERVICE_KERNEL_DRIVER       = $00000001;
      SERVICE_FILE_SYSTEM_DRIVER  = $00000002;
      SERVICE_ADAPTER             = $00000004;
      SERVICE_RECOGNIZER_DRIVER   = $00000008;  SERVICE_DRIVER              =
        (SERVICE_KERNEL_DRIVER or
         SERVICE_FILE_SYSTEM_DRIVER or
         SERVICE_RECOGNIZER_DRIVER);  SERVICE_WIN32_OWN_PROCESS   = $00000010;
      SERVICE_WIN32_SHARE_PROCESS = $00000020;
      SERVICE_WIN32               =
        (SERVICE_WIN32_OWN_PROCESS or
         SERVICE_WIN32_SHARE_PROCESS);  SERVICE_INTERACTIVE_PROCESS = $00000100;  SERVICE_TYPE_ALL            =
        (SERVICE_WIN32 or
         SERVICE_ADAPTER or
         SERVICE_DRIVER  or
         SERVICE_INTERACTIVE_PROCESS);//-------------------------------------
    // Get a list of services
    //
    // return TRUE if successful
    //
    // sMachine:
    //   machine name, ie: \\SERVER
    //   empty = local machine
    //
    // dwServiceType
    //   SERVICE_WIN32,
    //   SERVICE_DRIVER or
    //   SERVICE_TYPE_ALL
    //
    // dwServiceState
    //   SERVICE_ACTIVE,
    //   SERVICE_INACTIVE or
    //   SERVICE_STATE_ALL
    //
    // slServicesList
    //   TStrings variable to storage
    //
    {----在WindowsNT下,各种Service都存在service control manager database中,因此我们可以通过对service control manager database进行操作来实现对Service的编程。下面介绍常用的函数:1:SC_HANDLE OpenSCManager(LPCTSTR lpszMachineName,
                            LPCTSTR lpszDatabaseName,
                            DWORD fdwDesiredAccess);----Open SCManager 函数打开指定计算机上的service control manager database。其中参数lpszMachineName指定计算机名,若为空则指定为本机。参数lpszDatabaseName指定要打开的service control manager database,默认为空。----参数fdwDesiredAccess指定操作的权限,可以为下面取值之一SC_MANAGER_ALL_ACCESS        //所有权限
    SC_MANAGER_CONNECT           //允许连接service control manager
    SC_MANAGER_CREATE_SERVICE    //允许创建服务对象并把它加入service control manager database
    SC_MANAGER_ENUMERATE_SERVICE //允许枚举service control manager database中的服务SC_MANAGER_LOCK              //允许锁住service control manager database
    SC_MANAGER_QUERY_LOCK_STATUS //允许获取servicecontrolmanagerdatabase的封锁信息----函数返回值:函数执行成功则返回一个指向service control manager database的句柄,失败则返回NULL。2:SC_HANDLE OpenService(SC_HANDLE schSCManager,
                            LPCTSTR lpszServiceName,                        DWORD fdwDesiredAccess);----OpenService函数打开指定的Service。----其中参数schSCManager是指向service control manager database的句柄,由OpenSCManager函数返回。----参数lpszServiceName要打开的服务的名字,注意大小写。----参数fdwDesiredAccess指定操作的权限,可以为下面取值之一SERVICE_ALL_ACCESS            //所有权限
    SERVICE_CHANGE_CONFIG         //允许更改服务的配置
    SERVICE_ENUMERATE_DEPENDENTS  //允许获取依赖于该服务的其他服务
    SERVICE_INTERROGATE            //允许立即获取服务状态
    SERVICE_PAUSE_CONTINUE        //允许暂停和唤醒服务
    SERVICE_QUERY_CONFIG           //允许获取服务配置
    SERVICE_QUERY_STATU            //允许通过访问service control manager获取服务状态
    SERVICE_START                  //允许启动服务
    SERVICE_STOP                   //允许停止服务
    SERVICE_USER_DEFINE_CONTROL    //允许用户指定特殊的服务控制码
    ----函数返回值:函数执行成功则返回指向某项服务的句柄,失败则返回NULL。3:BOOL QueryServiceStatus(SC_HANDLE schService,LPSERVICE_STATUS lpssServiceStatus);----QueryServiceStatus函数返回指定服务的当前状态。----其中参数schService是指向某项服务的句柄,由OpenService函数返回,且必须SERVICE_QUERY_STATUS的权限。----参数lpssServiceStatus中存放返回的服务状态信息,结构如下
    typedefstruct_SERVICE_STATUS{
        DWORD dwServiceType;        //服务类型
        DWORD dwCurrentState;        //当前状态
        DWORD dwControlsAccepted;    //服务可接受的控制码
        DWORD dwWin32ExitCode;        //Win32出错代码
        DWORD dwServiceSpecificExitCode;//服务出错代码
        DWORD dwCheckPoint;            //用于跟踪服务长时间操作
        DWORD dwWaitHint;             //服务某一操作的最大允许时间,以毫秒为单位
    }//SERVICE_STATUS,*LPSERVICE_STATUS;{----函数返回值:函数执行成功则返回True,失败则返回False。4:BOOLStartService(SC_HANDLE schService,DWORD dwNumServiceArgs,LPCTSTR * lpszServiceArgs);
      

  2.   

    ----StartService函数启动指定的服务。
    ----其中参数schService是指向某项服务的句柄,由OpenService函数返回,且必须有SERVICE_START的权限。
    ----dwNumServiceArgs为启动服务所需的参数的个数。
    ----lpszServiceArgs为启动服务所需的参数。函数返回值:函数执行成功则返回True,失败则返回False。5:BOOL ControlService(SC_HANDLE hService,DWORD dwControl,LPSERVICE_STATUS lpServiceStatus);----ControlService函数向Win32service发送控制码。
    ----其中参数hService是指向某项服务的句柄,由OpenService函数返回。
    ----参数dwControl为控制码,常用的有
        SERVICE_CONTROL_STOP     //停止服务
        SERVICE_CONTROL_PAUSE    //暂停服务
        SERVICE_CONTROL_CONTINUE    //唤醒暂停的服务
        SERVICE_CONTROL_INTERROGATE//刷新某服务的状态
    ----参数lpServiceStatus指向SERVICE_STATUS结构,用于存放该服务最新的状态信息。
    ----函数返回值:函数执行成功则返回True,失败则返回False。6:BOOL EnumServicesStatus(SC_HANDLE hSCManager,
                            DWORD dwServiceType,
                            DWORD dwServiceState,
                            LPENUM_SERVICE_STATUS lpServices,
                            DWORD cbBufSize,
                            LPDWORD pcbBytesNeeded,
                            LPDWORD lpServicesReturned,
                            LPDWORD lpResumeHandle);----EnumServicesStatus函数用于枚举NT下存在的Service。
    ----其中参数hSCManager是指向service control manager database的句柄,由OpenSCManager函数返回,且必须有SC_MANAGER_ENUMERATE_SERVICE的权限。
    ----参数dwServiceType指定按服务的类型枚举。
    ----参数dwServiceState指定按服务的状态枚举。
    ----参数lpServices指向ENUM_SERVICE_STATUS结构,用于存放返回的服务的名字和状态信息。
    ----参数cbBufSize返回参数lpServices的长度,以字节为单位。
    ----参数pcbBytesNeeded返回获取剩余的Service所需字节的个数。
    ----参数lpServicesReturned返回服务的个数。
    ----参数lpResumeHandle,当第一次调用时该参数为0,当该函数再次被调用以获取另外的信息时,该参数表示下一个被读的Service。
    ----函数返回值:函数执行成功则返回True,失败则返回False。
    ----值得注意的是通常情况下该函数返回的结果为FALSE,我们可以调用GetLastError()来获取进一步信息。因为一台机器上有多种服务存在,所以GetLastError()应为ERROR_MORE_DATA,此时应再次调用EnumServicesStatus函数以获取正确的Service列表。
    }
    function ServiceGetList(
      sMachine : string;
      dwServiceType,
      dwServiceState : Dword;
      slServicesList : TStrings )
      : boolean;
    const
      //
      // assume that the total number of
      // services is less than 4096.
      // increase if necessary
      cnMaxServices = 4096;type
      TSvcA = array[0..cnMaxServices]
              of TEnumServiceStatus;
      PSvcA = ^TSvcA;
              
    var
      //
      // temp. use
      j : integer;  //
      // service control
      // manager handle
      schm          : SC_Handle;  //
      // bytes needed for the
      // next buffer, if any
      nBytesNeeded,  //
      // number of services
      nServices,  //
      // pointer to the
      // next unread service entry
      nResumeHandle : DWord;  //
      // service status array
      ssa : PSvcA;
    begin
      Result := false;  // connect to the service
      // control manager
      schm := OpenSCManager(
        PChar(sMachine),
        Nil,
        SC_MANAGER_ALL_ACCESS);  // if successful...
      if(schm > 0)then
      begin
        nResumeHandle := 0;    New(ssa);    EnumServicesStatus(
          schm,
          dwServiceType,
          dwServiceState,
          ssa^[0],
          SizeOf(ssa^),
          nBytesNeeded,
          nServices,
          nResumeHandle );    //
        // assume that our initial array
        // was large enough to hold all
        // entries. add code to enumerate
        // if necessary.
        //
        
        for j := 0 to nServices-1 do
        begin
          slServicesList.
            Add( StrPas(
              ssa^[j].lpDisplayName ) );
        end;    Result := true;    Dispose(ssa);    // close service control
        // manager handle
        CloseServiceHandle(schm);
      end;
    end;
    function ServiceGetKeyName(
      sMachine,
      sServiceDispName : string ) : string;
    var
      //
      // service control
      // manager handle
      schm          : SC_Handle;  //
      // max key name len
      nMaxNameLen   : DWord;  //
      // temp. string
      psServiceName : PChar;
    begin
      Result := '';  // expect a service key
      // name shorter than 255
      // characters
      nMaxNameLen := 255;  // connect to the service
      // control manager
      schm := OpenSCManager(
        PChar(sMachine),
        Nil,
        SC_MANAGER_CONNECT);  // if successful...
      if(schm > 0)then
      begin
        psServiceName :=
          StrAlloc(nMaxNameLen+1);    if(nil <> psServiceName)then
        begin
          if( GetServiceKeyName(schm,PChar(sServiceDispName),
          psServiceName, //返回值
          nMaxNameLen ) )then
          begin
            psServiceName
              [nMaxNameLen] := #0;        Result :=
              StrPas( psServiceName );
          end;      StrDispose(psServiceName);
        end;    // close service control
        // manager handle
        CloseServiceHandle(schm);
      end;
    end;
    function ServiceGetDisplayName(
      sMachine,
      sServiceKeyName : string ) : string;
    var
      //
      // service control
      // manager handle
      schm          : SC_Handle;  //
      // max display name len
      nMaxNameLen   : DWord;  //
      // temp. string
      psServiceName : PChar;
    begin
      Result := '';  // expect a service display
      // name shorter than 255
      // characters
      nMaxNameLen := 255;  // connect to the service
      // control manager
      schm := OpenSCManager(
        PChar(sMachine),
        Nil,
        SC_MANAGER_CONNECT);  // if successful...
      if(schm > 0)then
      begin
        psServiceName :=
          StrAlloc(nMaxNameLen+1);    if(nil <> psServiceName)then
        begin
          if( GetServiceDisplayName(
            schm,
            PChar(sServiceKeyName),
            psServiceName, //返回值
            nMaxNameLen ) )then    // 长度
          begin
            psServiceName
              [nMaxNameLen] := #0;        Result :=
              StrPas( psServiceName );
          end;
          
          StrDispose(psServiceName);
        end;    // close service control
        // manager handle
        CloseServiceHandle(schm);
      end;
    end;
      

  3.   

    //-------------------------------------
    // get service status
    //
    // return status code if successful
    // -1 if not
    //
    // return codes:
    //   SERVICE_STOPPED
    //   SERVICE_RUNNING
    //   SERVICE_PAUSED
    //
    // following return codes
    // are used to indicate that
    // the service is in the
    // middle of getting to one
    // of the above states:
    //   SERVICE_START_PENDING
    //   SERVICE_STOP_PENDING
    //   SERVICE_CONTINUE_PENDING
    //   SERVICE_PAUSE_PENDING
    //
    // sMachine:
    //   machine name, ie: \\SERVER
    //   empty = local machine
    //
    // sService
    //   service name, ie: Alerter
    //
    function ServiceGetStatus(
      sMachine,
      sService : string ) : DWord;
    var
      //
      // service control
      // manager handle
      schm,
      //
      // service handle
      schs   : SC_Handle;
      //
      // service status
      ss     : TServiceStatus;
      //
      // current service status
      dwStat : DWord;
    begin
      dwStat := 0;  // connect to the service
      // control manager
      schm := OpenSCManager(
        PChar(sMachine),
        Nil,
        SC_MANAGER_CONNECT);  // if successful...
      if(schm > 0)then
      begin
        // open a handle to
        // the specified service
        schs := OpenService(
          schm,
          PChar(sService),
          // we want to
          // query service status
          SERVICE_QUERY_STATUS);    // if successful...
        if(schs > 0)then
        begin
          // retrieve the current status
          // of the specified service    
          if(QueryServiceStatus(
               schs,
               ss))then
          begin
            dwStat := ss.dwCurrentState;
          end;
          
          // close service handle
          CloseServiceHandle(schs);
        end;    // close service control
        // manager handle
        CloseServiceHandle(schm);
      Result := dwStat;
        end
        else Result := SERVICE_STOPPED;
    end;
    //-------------------------------------
    // return TRUE if the specified
    // service is running, defined by
    // the status code SERVICE_RUNNING.
    // return FALSE if the service
    // is in any other state, including
    // any pending states
    //
    function ServiceRunning(
      sMachine,
      sService : string ) : boolean;
    begin
      Result := SERVICE_RUNNING =
        ServiceGetStatus(
          sMachine, sService );
    end;
    //-------------------------------------
    // return TRUE if the specified
    // service was stopped, defined by
    // the status code SERVICE_STOPPED.
    //
    function ServiceStopped(
      sMachine,
      sService : string ) : boolean;
    begin
      Result := SERVICE_STOPPED =
        ServiceGetStatus(
          sMachine, sService );
    end;//
    // start service
    //
    // return TRUE if successful
    //
    // sMachine:
    //   machine name, ie: \\SERVER
    //   empty = local machine
    //
    // sService
    //   service name, ie: Alerter
    //
    function ServiceStart(
      sMachine,
      sService : string ) : boolean;
    var
      //
      // service control
      // manager handle
      schm,
      //
      // service handle
      schs   : SC_Handle;
      //
      // service status
      ss     : TServiceStatus;
      //
      // temp char pointer
      psTemp : PChar;
      //
      // check point
      dwChkP : DWord;
    begin
      ss.dwCurrentState := 0;
      
      // connect to the service
      // control manager
      schm := OpenSCManager(
        PChar(sMachine),
        Nil,
        SC_MANAGER_CONNECT);  // if successful...
      if(schm > 0)then
      begin
        // open a handle to
        // the specified service
        schs := OpenService(
          schm,
          PChar(sService),
          // we want to
          // start the service and
          SERVICE_START or
          // query service status
          SERVICE_QUERY_STATUS);    // if successful...
        if(schs > 0)then
        begin
          psTemp := Nil;
          if(StartService(
               schs,
               0,
               psTemp))then
          begin
            // check status
            if(QueryServiceStatus(
                 schs,
                 ss))then
            begin
              while(SERVICE_RUNNING
                <> ss.dwCurrentState)do
              begin
                //
                // dwCheckPoint contains a
                // value that the service
                // increments periodically
                // to report its progress
                // during a lengthy
                // operation.
                //
                // save current value
                //
                dwChkP := ss.dwCheckPoint;            //
                // wait a bit before
                // checking status again
                //
                // dwWaitHint is the
                // estimated amount of time
                // the calling program
                // should wait before calling
                // QueryServiceStatus() again
                //
                // idle events should be
                // handled here...
                //
                Sleep(ss.dwWaitHint);            if(not QueryServiceStatus(
                     schs,
                     ss))then
                begin
                  // couldn't check status
                  // break from the loop
                  break;
                end;            if(ss.dwCheckPoint <
                  dwChkP)then
                begin
                  // QueryServiceStatus
                  // didn't increment
                  // dwCheckPoint as it
                  // should have.
                  // avoid an infinite
                  // loop by breaking
                  break;
                end;
              end;
            end;
          end;      // close service handle
          CloseServiceHandle(schs);
        end;    // close service control
        // manager handle
        CloseServiceHandle(schm);
      end;
      

  4.   

    // return TRUE if
      // the service status is running
      Result :=
        SERVICE_RUNNING =
          ss.dwCurrentState;
    end;
    //
    // stop service
    //
    // return TRUE if successful
    //
    // sMachine:
    //   machine name, ie: \\SERVER
    //   empty = local machine
    //
    // sService
    //   service name, ie: Alerter
    //
    function ServiceStop(
      sMachine,
      sService : string ) : boolean;
    var
      //
      // service control
      // manager handle
      schm,
      //
      // service handle
      schs   : SC_Handle;
      //
      // service status
      ss     : TServiceStatus;
      //
      // check point
      dwChkP : DWord;
    begin
      // connect to the service
      // control manager
      schm := OpenSCManager(
        PChar(sMachine),
        Nil,
        SC_MANAGER_CONNECT);  // if successful...
      if(schm > 0)then
      begin
        // open a handle to
        // the specified service
        schs := OpenService(
          schm,
          PChar(sService),
          // we want to
          // stop the service and
          SERVICE_STOP or
          // query service status
          SERVICE_QUERY_STATUS);    // if successful...
        if(schs > 0)then
        begin
          if(ControlService(
               schs,
               SERVICE_CONTROL_STOP,
               ss))then
          begin
            // check status
            if(QueryServiceStatus(
                 schs,
                 ss))then
            begin
              while(SERVICE_STOPPED
                <> ss.dwCurrentState)do
              begin
                //
                // dwCheckPoint contains a
                // value that the service
                // increments periodically
                // to report its progress
                // during a lengthy
                // operation.
                //
                // save current value
                //
                dwChkP := ss.dwCheckPoint;            //
                // wait a bit before
                // checking status again
                //
                // dwWaitHint is the
                // estimated amount of time
                // the calling program
                // should wait before calling
                // QueryServiceStatus() again
                //
                // idle events should be
                // handled here...
                //
                Sleep(ss.dwWaitHint);            if(not QueryServiceStatus(
                     schs,
                     ss))then
                begin
                  // couldn't check status
                  // break from the loop
                  break;
                end;            if(ss.dwCheckPoint <
                  dwChkP)then
                begin
                  // QueryServiceStatus
                  // didn't increment
                  // dwCheckPoint as it
                  // should have.
                  // avoid an infinite
                  // loop by breaking
                  break;
                end;
              end;
            end;
          end;      // close service handle
          CloseServiceHandle(schs);
        end;    // close service control
        // manager handle
        CloseServiceHandle(schm);
      end;  // return TRUE if
      // the service status is stopped
      Result :=
        SERVICE_STOPPED =
          ss.dwCurrentState;
    end;end.上面是服务的代码...
      

  5.   

    自运行程序其实就很简单了,刚刚在上面帖了一大把代码.不好意思啊
    我把你的两个要求的代码打包放到我的主页上,你去载吧:www.ksaiy.com/run.rar你下载以后告诉我一声.我删的.
      

  6.   


    To ksaiy(消失在人海) 
    谢谢,我下下来了,
    对了,我想做个杀毒的,不知道要怎么编的,可以引导一下吗
    对内存,进程要做很多的处理的
    且要对硬盘进行搜索!
    你有没有这一方面的资料,可以提供一下吗