Just starting out? Need help? Post your questions and find answers here.
novablue
Enthusiast
Posts: 165 Joined: Sun Nov 27, 2016 6:38 am
Post
by novablue » Wed Mar 31, 2021 5:21 am
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.
netmaestro
PureBasic Bullfrog
Posts: 8433 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Wed Mar 31, 2021 5:28 am
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
Posts: 4664 Joined: Sun Apr 12, 2009 6:27 am
Post
by RASHAD » Wed Mar 31, 2021 7:51 am
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
Posts: 165 Joined: Sun Nov 27, 2016 6:38 am
Post
by novablue » Wed Mar 31, 2021 8:11 am
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
Posts: 3330 Joined: Thu Apr 18, 2019 8:17 am
Post
by BarryG » Wed Mar 31, 2021 10:10 am
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
Posts: 3330 Joined: Thu Apr 18, 2019 8:17 am
Post
by BarryG » Wed Mar 31, 2021 11:16 am
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!