用delphi弄了个服务,在开启该服务时如何获取该服务所在目录的完全路径?

解决方案 »

  1.   

    SoftPath := ExtractFilePath(ParamStr(0));
      

  2.   

    function GetSvcFilePath(ASvcName: string): string;
    var
      hMgr,hSvc: integer;
      svcConf: PQueryServiceConfigA;
      i: Cardinal;
      s: string;
    begin
      hMgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
      if hMgr = 0 then RaiseLastOSError;
      try
        hSvc := OpenService(hMgr,pchar(ASvcName),SERVICE_ALL_ACCESS);
        if hSvc = 0 then RaiseLastOSError;
        try
          GetMem(svcConf,4096);
          if not QueryServiceConfig(hSvc,svcConf,4096,i) then RaiseLastOSError;
          s := svcConf^.lpBinaryPathName;
          result := s;
          FreeMem(svcConf,4096);
        finally
          if hSvc <> 0 then
            CloseServiceHandle(hSvc);
        end;
      finally
        if hMgr <> 0 then
          CloseServiceHandle(hMgr);
      end;
    end;
      

  3.   

    ExtractFilePath(ParamStr(0))
    ExtractFilePath(Application.ExeName)