PureBasic Forum https://www.purebasic.fr/english/ |
|
How to determine running process that called/ran my program? https://www.purebasic.fr/english/viewtopic.php?f=5&t=72283 |
Page 1 of 1 |
Author: | Mike Yurgalavage [ Thu Feb 14, 2019 12:16 am ] |
Post subject: | How to determine running process that called/ran my program? |
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! |
Author: | RSBasic [ Thu Apr 18, 2019 10:21 am ] |
Post subject: | Re: How to determine running process that called/ran my prog |
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 |
Page 1 of 1 | All times are UTC + 1 hour |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |