(Windows) Detect if exe is running from command/shell or as a service?

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

(Windows) Detect if exe is running from command/shell or as a service?

Post by jassing »

I have an exe that runs as a service. I want to determine if it's run not-as-a-service so I can offer to install it as a service.
Does anyone have an idea that doesn't involve a parameter to be passed?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: (Windows) Detect if exe is running from command/shell or as a service?

Post by RASHAD »

Hi jassing
Check if your application is running and if it's running it has Path if so it's not a SERVICE otherwise it's SERVICE
Egypt my love
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: (Windows) Detect if exe is running from command/shell or as a service?

Post by jassing »

SO simple!! Thank you!!! I was working on this way harder than I had to...
Hope you are well.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: (Windows) Detect if exe is running from command/shell or as a service?

Post by RASHAD »

Hi jassing
I am glad you are still around
I am OK
Thanks for asking
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: (Windows) Detect if exe is running from command/shell or as a service?

Post by RASHAD »

Simplify things [Service or Not]

Code: Select all

Global ImageName$,FilePath$

Prototype.i GetModuleFileNameExW(hProcess.l,hModule.l,*lpFilename,nSize.i)
Prototype.i GetModuleFileNameExA(hProcess.l,hModule.l,*lpFilename,nSize.i)

Lib = OpenLibrary(#PB_Any,"psapi.dll")
If Lib 
  CompilerIf #PB_Compiler_Unicode
    Global GetModuleFileNameEx.GetModuleFileNameExW = GetFunction(Lib,"GetModuleFileNameExW")
  CompilerElse
    Global GetModuleFileNameEx.GetModuleFileNameExA = GetFunction(Lib,"GetModuleFileNameExA")
  CompilerEndIf  
Else
  MessageRequester("Warning", "Can not load Psapi.dll" ,#MB_ICONWARNING)
  End  
EndIf

Procedure CheckRunningExe(Process$)
  Proc32.PROCESSENTRY32
  Proc32\dwSize = SizeOf(Proc32)
 
  snap = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0) 
  If Snap
    If Process32First_(snap, @Proc32)
      While Process32Next_(snap, @Proc32)
          ImageName$ = PeekS(@Proc32\szExeFile)
          ;Debug ImageName$
          If LCase(ImageName$) = Process$   ;Your exe file name here in lower case characters 
              FilePath$ = Space(1024)
              hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, 0, Proc32\th32ProcessID)
                If hProcess
                    GetModuleFileNameEx(hProcess, 0, @FilePath$, Len(FilePath$))
                    CloseHandle_(hProcess)
                EndIf
              Break
          EndIf
          ImageName$ = ""            
      Wend
    EndIf
    CloseHandle_(Snap)
  EndIf
EndProcedure

CheckRunningExe("ctfmon.exe")     ;The name of the application 

If ImageName$  
  If GetPathPart(FilePath$) = ""  
    Debug "It's a service"
  Else
    Debug "It's not a service"
  EndIf
Else
  Debug "No such EXE running"
EndIf

Egypt my love
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: (Windows) Detect if exe is running from command/shell or as a service?

Post by jassing »

Thank you, my friend, very much appreciated.

FWIW, here's the version I ended up... (mostly your code)

Code: Select all

DeclareModule RunningAs
  Enumeration 
    #NotFound
    #NotAService
    #Service
  EndEnumeration
  
  Declare     type(p.s="")
  Declare.s  typeS(p.s="")
EndDeclareModule

Module RunningAs
  EnableExplicit
  Define lib
  
  ;- Initialization
  Lib = OpenLibrary(#PB_Any,"psapi.dll")
  If Lib 
    CompilerIf #PB_Compiler_Unicode
      Prototype.i GetModuleFileNameExW(hProcess.l,hModule.l,*lpFilename,nSize.i)
      Global GetModuleFileNameEx.GetModuleFileNameExW = GetFunction(Lib,"GetModuleFileNameExW")
    CompilerElse
      Prototype.i GetModuleFileNameExA(hProcess.l,hModule.l,*lpFilename,nSize.i)
      Global GetModuleFileNameEx.GetModuleFileNameExA = GetFunction(Lib,"GetModuleFileNameExA")
    CompilerEndIf  
  Else
    Debug "Failed to load PSAPI.DLL"
    End  
  EndIf
  
  Procedure CheckRunningExe(ProcessName.s)
    Protected ProcessID.PROCESSENTRY32
    Protected snap , retval, ImageName.s, FileName.s, hProcess
    
    ProcessID\dwSize = SizeOf(ProcessID)
    ProcessName = LCase(ProcessName)
    snap = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0) 
    retval = #NotFound
    If Snap
      If Process32First_(snap, @ProcessID)
        While Process32Next_(snap, @ProcessID)
          With ProcessID
            ImageName = PeekS(@\szExeFile)
              If LCase(ImageName) = ProcessName    
                FileName = Space(1024)
                hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, 0, \th32ProcessID)
                  If hProcess
                      GetModuleFileNameEx(hProcess, 0, @FileName, Len(FileName))
                      CloseHandle_(hProcess)
                      If GetFilePart(ImageName) = ""
                        retval = #service
                      Else
                        retval = #NotAService
                      EndIf 
                  EndIf
                Break
              EndIf
           EndWith
        Wend
      EndIf
      CloseHandle_(Snap)
    EndIf
    ProcedureReturn retval
  EndProcedure
  Procedure type(prog.s="")
    If prog = "" : prog = ProgramFilename() : EndIf 
    ProcedureReturn CheckRunningExe(GetFilePart(prog))  
  EndProcedure
  Procedure.s typeS(prog.s="")
    Protected retval.s
    If prog = "" : prog = ProgramFilename() : EndIf 
    Select CheckRunningExe(GetFilePart(prog))
      Case #service 
        retval = "As a service"
      Case #NotAService
        retval = "Not a service"
      Default
        retval = "Invalid"
    EndSelect
    ProcedureReturn retval
  EndProcedure
EndModule

CompilerIf #PB_Compiler_IsMainFile
  Debug "Not found/invalid = "+Str(RunningAs::#NotFound)
  Debug "Not a service = "+Str(RunningAs::#NotAService)
  Debug "As a service = "+Str(RunningAs::#Service)
  Debug "This program is = "+Str(RunningAs::type())
  Debug "or in english = "+RunningAs::typeS()
CompilerEndIf 
Post Reply