在NT服务中,怎么样得到服务程序所在的路径?

解决方案 »

  1.   

    方法1:使用全局变量ParamStr(0)
    方法2:使用GetModuleFileName()函数
      

  2.   

    AnsiString __fastcall GetServicePath(AnsiString ServiceName/*????*/)
    {
       LPQUERY_SERVICE_CONFIG lpcnfg;
       DWORD Size=0;
       HANDLE SvcMgr;
       int iLen=0;
       HANDLE Svc;
       AnsiString Result="";
       SvcMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
       if(!SvcMgr)
          RaiseLastOSError();
       try
       {
           Svc=OpenService(SvcMgr,ServiceName.c_str(),SERVICE_ALL_ACCESS);
           if(!Svc)RaiseLastOSError();
           try
           {          lpcnfg= (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096);
              if(!lpcnfg)return Result;
              memset(lpcnfg,0,4096);
              DWORD qResult=QueryServiceConfig(Svc,lpcnfg,4096,&Size);
              if(!qResult)
                 return Result;
              switch(qResult)
              {
                 case ERROR_ACCESS_DENIED:
                      return "ERROR_ACCESS_DENIED";
                 case ERROR_INVALID_HANDLE:
                      return "ERROR_INVALID_HANDLE";
                 case ERROR_INSUFFICIENT_BUFFER:
                      LocalFree(lpcnfg);
                      LocalAlloc(LPTR,Size);
                      memset(lpcnfg,0,Size);
                      qResult=QueryServiceConfig(Svc,lpcnfg,Size,&Size);
              }
              iLen=strlen(lpcnfg->lpBinaryPathName);
              Result.SetLength(iLen);
              memcpy(Result.c_str(),lpcnfg->lpBinaryPathName,iLen);          LocalFree(lpcnfg);       }
           __finally
           {
              CloseServiceHandle(Svc);
           }
       }
       __finally
       {
          CloseServiceHandle(SvcMgr);
       }
       return Result;
    }
      

  3.   

    uses
    SvcMgr,winsvc;
    function GetServicePath(const ServiceName:string):string;
    var
       lpcnfg:PQueryServiceConfig;
       nSize:DWORD;
       SvcMgr:THandle;
       iLen:Integer;
       Svc:THANDLE;
       qResult:boolean;
    begin
       Result:='';
       SvcMgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
       if SvcMgr=0 then
          RaiseLastOSError;
       try
           Svc:=OpenService(SvcMgr,PChar(ServiceName),SERVICE_ALL_ACCESS);
           if Svc=0 then RaiseLastOSError;
           try          lpcnfg:= PQueryServiceConfig( LocalAlloc(LPTR, 4096));
              if not Assigned(lpcnfg) then exit;
              ZeroMemory(lpcnfg,4096);
              nSize:=4096;
              qResult:=QueryServiceConfig(Svc,lpcnfg,4096,nSize);
              if not qResult then exit;
              iLen:=Length(lpcnfg.lpBinaryPathName);
              SetLength(Result,iLen);
              CopyMemory(PChar(Result),PChar(lpcnfg.lpBinaryPathName),iLen);          LocalFree(HLOCAL(lpcnfg));       finally
              CloseServiceHandle(Svc);
           end;   finally
          CloseServiceHandle(SvcMgr);
       end;
    end;
      

  4.   

    汗汗汗,连Delphi和CB都分不清了