[Solved] get pid of program started with ShellExecuteEx_

Just starting out? Need help? Post your questions and find answers here.
novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

[Solved] get pid of program started with ShellExecuteEx_

Post by novablue »

Hello,
I am using this code to run a program with admin privileges. But i am not sure how to get the pid of the started program.

Code: Select all

Procedure.i RunProgramElevated(Exe.s, Parameters.s)
	Define ExecFile.SHELLEXECUTEINFO

	ExecFile\cbSize 		= SizeOf(ExecFile)
	ExecFile\fMask  		= #Null
	ExecFile\hwnd  		= 0
	ExecFile\lpVerb 		= @"runas"
	ExecFile\lpFile 		= @Exe
	ExecFile\lpParameters	= @Parameters
	ExecFile\lpDirectory	= #Null
	ExecFile\nShow		= #SW_NORMAL
	ExecFile\hInstApp	= #Null
	
	ProcedureReturn ShellExecuteEx_(ExecFile)
EndProcedure

RunProgramElevated("notepad.exe")
Last edited by novablue on Wed Mar 31, 2021 8:31 am, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How to get pid of program started with ShellExecuteEx_ ?

Post by netmaestro »

typedef struct _SHELLEXECUTEINFOA {
DWORD cbSize;
ULONG fMask;
HWND hwnd;
LPCSTR lpVerb;
LPCSTR lpFile;
LPCSTR lpParameters;
LPCSTR lpDirectory;
int nShow;
HINSTANCE hInstApp;
void *lpIDList;
LPCSTR lpClass;
HKEY hkeyClass;
DWORD dwHotKey;
union {
HANDLE hIcon;
HANDLE hMonitor;
} DUMMYUNIONNAME;
HANDLE hProcess;
} SHELLEXECUTEINFOA, *LPSHELLEXECUTEINFOA;
I don't use it much myself but I believe the function will fill this structure when executed. You should find the PID in *IpIDList.
BERESHEIT
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: How to get pid of program started with ShellExecuteEx_ ?

Post by RASHAD »

Hi

Code: Select all

Procedure.i RunProgramElevated(Exe.s, Parameters.s)
   Define ExecFile.SHELLEXECUTEINFO

   ExecFile\cbSize       = SizeOf(ExecFile)
   ExecFile\fMask        = #Null
   ExecFile\hwnd        = 0
   ExecFile\lpVerb       = @"runas"
   ExecFile\lpFile       = @Exe
   ExecFile\lpParameters   = @Parameters
   ExecFile\lpDirectory   = #Null
   ExecFile\nShow      = #SW_NORMAL
   ExecFile\hInstApp   = #Null
   
   ProcedureReturn ShellExecuteEx_(ExecFile)
EndProcedure

RunProgramElevated("notepad.exe","")
Repeat
hWnd = FindWindow_("notepad",#Null)
Until hwnd
If GetWindowThreadProcessId_(hWnd,@PID)
  MessageRequester("","PID: "+Str(PID),#MB_SYSTEMMODAL)
EndIf
Egypt my love
novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

Re: How to get pid of program started with ShellExecuteEx_ ?

Post by novablue »

Thanks i found a solution:

Code: Select all

Procedure.i RunProgramElevated(Exe.s, Parameters.s = "") 
	Define ExecFile.SHELLEXECUTEINFO
	Define Result.i, Lib.i, *GetProcessId
	
	ExecFile\cbSize 		= SizeOf(ExecFile)
	ExecFile\fMask  		= #SEE_MASK_NOCLOSEPROCESS
	ExecFile\hwnd  			= 0
	ExecFile\lpVerb 		= @"runas"
	ExecFile\lpFile 		= @Exe
	ExecFile\lpParameters	= @Parameters
	ExecFile\lpDirectory	= #Null
	ExecFile\nShow			= #SW_NORMAL
	ExecFile\hInstApp		= #Null
	
	Result = ShellExecuteEx_(ExecFile)
	
	If ExecFile\hProcess
		Lib = OpenLibrary(#PB_Any, "kernel32.dll")
		If Lib
			Result = CallFunction(Lib, "GetProcessId", ExecFile\hProcess)
			CloseLibrary(Lib)
		EndIf
	EndIf
	
	ProcedureReturn Result
EndProcedure
	
Debug RunProgramElevated("notepad.exe")
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: How to get pid of program started with ShellExecuteEx_ ?

Post by BarryG »

novablue, don't forget you'll need to specify the working directory for some processes to run properly (not for Notepad, though). So here's a fixed version of your code to allow for that:

Code: Select all

Procedure.i RunProgramElevated(Exe.s, Parameters.s = "")
  Define ExecFile.SHELLEXECUTEINFO
  Define Result.i, Lib.i, *GetProcessId
  Define Dir.s = GetPathPart(Exe)
  
  ExecFile\cbSize       = SizeOf(ExecFile)
  ExecFile\fMask        = #SEE_MASK_NOCLOSEPROCESS
  ExecFile\hWnd         = 0
  ExecFile\lpVerb       = @"runas"
  ExecFile\lpFile       = @Exe
  ExecFile\lpParameters = @Parameters
  ExecFile\lpDirectory  = @Dir
  ExecFile\nShow        = #SW_NORMAL
  ExecFile\hInstApp     = #Null
  
  Result = ShellExecuteEx_(ExecFile)
  
  If ExecFile\hProcess
    Lib = OpenLibrary(#PB_Any, "kernel32.dll")
    If Lib
      Result = CallFunction(Lib, "GetProcessId", ExecFile\hProcess)
      CloseLibrary(Lib)
    EndIf
  EndIf
  
  ProcedureReturn Result
EndProcedure

Debug RunProgramElevated("notepad.exe")
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: How to get pid of program started with ShellExecuteEx_ ?

Post by BarryG »

BTW, how can I add the "hMonitor" parameter to the SHELLEXECUTEINFO structure of my code above? "ExecFile\hMonitor" isn't recognized by PureBasic.

More info here -> https://docs.microsoft.com/en-us/window ... ecuteinfoa

PureBasic doesn't recognize #SEE_MASK_HMONITOR either, but I found out it's $200000.

My goal is to open Notepad on a monitor of my choice. Thanks!
Post Reply