How to determine running process that called/ran my program?

Windows specific forum
Mike Yurgalavage
Enthusiast
Enthusiast
Posts: 118
Joined: Thu May 17, 2007 8:35 pm
Location: USA

How to determine running process that called/ran my program?

Post by Mike Yurgalavage »

I have software that uses a launcher .exe, then there is the actual program .exe. I will have certain parameters that do special things if they are sent to my program .exe via the launcher or with launch commands.

At any rate, some of those parameters are only for developers, so if someone were to look at my .exe and saw the launch parameters, they might be able to see what they are and then use them.

This is just ONE example of why I am asking the following.

Is there a way to determine from within a running process which other process ran it? For instance, in the example above, the program .exe would have an api call or some way to determine that it was the launcher .exe that ran it?

What commands or purebasic code is there to check to see if the current process was called/run by another process?

ProgramA runs ProgamB. ProgramB has function or api call to determine it was ProgramA that ran it?

In the example above, the parameters wouldn't even be checked if the program wasn't run by the launcher, for example.

I'd also like to keep the code ONLY within ProgramB or the program .exe, NOT the launcher .exe (i.e. ProgramA)

Any ideas or help is appreciated!
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: How to determine running process that called/ran my prog

Post by RSBasic »

Sorry for the late answer.
Yes, you can find out which parent process started your program. Provided the program that starts your program is still running:

Code: Select all

;YourProgram.exe
EnableExplicit

Procedure GetParentPID()
  Protected hSnapshot
  Protected PROCESSENTRY32.PROCESSENTRY32
  Protected bProcess
  
  hSnapshot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, #Null)
  If hSnapshot
    PROCESSENTRY32\dwsize = SizeOf(PROCESSENTRY32)
    bProcess = Process32First_(hSnapshot, @PROCESSENTRY32)
    If bProcess
      Repeat
        If PeekS(@PROCESSENTRY32\szExeFile, 260) = GetFilePart(ProgramFilename())
          CloseHandle_(hSnapshot)
          ProcedureReturn PROCESSENTRY32\th32ParentProcessID
        EndIf
        bProcess = Process32Next_(hSnapshot, @PROCESSENTRY32)
      Until Not bProcess
    EndIf 
    CloseHandle_(hSnapshot)
  EndIf
  
EndProcedure

Procedure.s GetProcessPath(PID)
  Protected hSnapshot
  Protected PROCESSENTRY32.PROCESSENTRY32
  Protected bProcess
  
  hSnapshot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, #Null)
  If hSnapshot
    PROCESSENTRY32\dwsize = SizeOf(PROCESSENTRY32)
    bProcess = Process32First_(hSnapshot, @PROCESSENTRY32)
    If bProcess
      Repeat
        If PROCESSENTRY32\th32ProcessID = PID
          CloseHandle_(hSnapshot)
          ProcedureReturn PeekS(@PROCESSENTRY32\szExeFile)
        EndIf
        bProcess = Process32Next_(hSnapshot, @PROCESSENTRY32)
      Until Not bProcess
    EndIf 
    CloseHandle_(hSnapshot)
  EndIf
  
EndProcedure

MessageRequester("Info", "Parent Process: " + GetProcessPath(GetParentPID()), 0)

If you start your program directly, then "explorer.exe". If you start your program with a separate program, then "AnotherProgram.exe":

Code: Select all

;AnotherProgram.exe
EnableExplicit

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  RunProgram("YourProgram.exe")
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
Image
Image
Post Reply