It is currently Mon Sep 23, 2019 8:58 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: How to determine running process that called/ran my program?
PostPosted: Thu Feb 14, 2019 12:16 am 
Offline
Enthusiast
Enthusiast

Joined: Thu May 17, 2007 8:35 pm
Posts: 112
Location: USA
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: How to determine running process that called/ran my prog
PostPosted: Thu Apr 18, 2019 10:21 am 
Offline
Moderator
Moderator
User avatar

Joined: Thu Dec 31, 2009 11:05 pm
Posts: 1098
Location: Germany::Berlin()
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:
;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:
;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

_________________
ImageImageImageImage Image


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye