Page 1 of 2
RunAs - built-in
Posted: Mon Apr 26, 2010 11:28 pm
by ctg
G'day folks.
I have been using RunAs, the function in Droopy's library, for quite a while. Until mow it has served me quite well; however, I now find it limiting.
I need to be able to find out when the executed problem has closed, and get return codes. I have been using GetPIDProcess on the executed file, and waiting until the name can no longer be found in memory, but this has obvious drawbacks.
It does not have exactly the same functions as RunProgram, more specifically the #PB_Program_Hide and #PB_Program_Open. If possible, I'd love to have an inbuilt function added to the RunProgram switches, with the ability to run under a certain user account.
If I have missed an already existent function, please let me know.
Cheers
Re: RunAs - built-in
Posted: Tue Apr 27, 2010 11:29 am
by c4s
Maybe some functions posted in this thread will help you?!
->
http://www.purebasic.fr/english/viewtop ... 13&t=41952
Re: RunAs - built-in
Posted: Tue Apr 27, 2010 8:33 pm
by Droopy
SuperRunas() from the DroopyLib do this
Re: RunAs - built-in
Posted: Tue Apr 27, 2010 8:56 pm
by Trond
Everything you asked for can already be done. Look at the manual in the section "Process".
Re: RunAs - built-in
Posted: Tue Apr 27, 2010 11:25 pm
by ctg
Droopy wrote:SuperRunas() from the DroopyLib do this
Looked, but I can't find that function mate... what version did it get introduced in? Using v1.32 (15/12/2009).
Trond wrote:Everything you asked for can already be done. Look at the manual in the section "Process".
Uh, I've looked and can't find anything that would allow me to run an app under a different user account... any pointers?
Cheers
Re: RunAs - built-in
Posted: Wed Apr 28, 2010 12:16 pm
by Trond
ctg wrote:Trond wrote:Everything you asked for can already be done. Look at the manual in the section "Process".
Uh, I've looked and can't find anything that would allow me to run an app under a different user account... any pointers?
Cheers
Oups, I didn't see that, sorry. You can do that with API, but I'm not sure how you want to handle passwords.
Re: RunAs - built-in
Posted: Thu Apr 29, 2010 1:24 am
by ctg
Trond wrote:ctg wrote:Trond wrote:Everything you asked for can already be done. Look at the manual in the section "Process".
Uh, I've looked and can't find anything that would allow me to run an app under a different user account... any pointers?
Cheers
Oups, I didn't see that, sorry. You can do that with API, but I'm not sure how you want to handle passwords.
I don't mind throwing the password in a parameter, launched by the compiled exe.
Can this please be added natively into PureBasic? AutoIT has it but I don't want to have to include the dll with any app I create that needs this function...
Re: RunAs - built-in
Posted: Thu Apr 29, 2010 12:46 pm
by Trond
It's not as elegant as it would be native, but it seems to work:
Code: Select all
Prototype PCPWLW(A, B, C, D, E, F, G, H, I, J, K)
Advapi = OpenLibrary(#PB_Any, "advapi32.dll")
Global CreateProcessWithLogonW.PCPWLW = GetFunction(Advapi, "CreateProcessWithLogonW")
#LOGON_WITH_PROFILE = 1
Procedure RunAs(Program.s, User.s, Password.s)
pi.PROCESS_INFORMATION
Ok = CreateProcessWithLogonW(@User, 0, @Password, #LOGON_WITH_PROFILE, 0, @Program, 0, 0, 0, 0, @pi)
CloseHandle_(pi\hProcess)
CloseHandle_(pi\hThread)
ProcedureReturn Ok
EndProcedure
RunAs("notepad.exe", "testuser", "password")
I don't know how to read/write to the process though.
Re: RunAs - built-in
Posted: Mon May 03, 2010 4:14 am
by ctg
Thanks mate... I also need to wait until the app is closed too, so I need to get an app handle...
Cheers.
Re: RunAs - built-in
Posted: Mon May 03, 2010 12:09 pm
by Trond
The handle you're looking for is probably pi\hProcess.
Re: RunAs - built-in
Posted: Sun Dec 16, 2018 6:09 am
by Dude
Trond's simple and easy code above no longer works for me.

It used to! But now this just returns 0 (failure):
Code: Select all
Debug RunAs("C:\Windows\RegEdit.exe",admin$,password$) ; Opens RegEdit with admin rights.
Why would this suddenly stop working after all these years? One of my apps uses it and now it's failing.

Re: RunAs - built-in
Posted: Sun Dec 16, 2018 12:40 pm
by mk-soft
Perhaps OS Bug.
Windows 7 is work, but not on windows 10...
Link:
https://answers.microsoft.com/en-us/win ... 3933571bcb
Attention. The KB4462933 also has errors again...
Re: RunAs - built-in
Posted: Sun Dec 16, 2018 12:44 pm
by Dude
I only use Windows 7, and it used to work with 7, but not anymore.

Doesn't open RegEdit with admin rights here.
Re: RunAs - built-in
Posted: Sun Dec 16, 2018 1:03 pm
by mk-soft
Same on my machine.
Program stopped because UAC Requester not visible.
Re: RunAs - built-in
Posted: Fri Jan 04, 2019 1:28 pm
by Dude
After some testing, I've come to the conclusion that this is actually not an issue.
As mk-soft said, it's failing because UAC is not being accepted. Confirmed by Shift+RightClicking RegEdit and selecting "Run as different user" (not "Run as administrator"). Then I get a prompt for the user name and password, just like the code above does. But then, the UAC prompt
also appears, which stops everything dead.
I note with interest, however, that I can still use the code to run other "safe" apps as a different user, such as Notepad. These will appear with the different user name in Task Manager for the process. So maybe just "critical" apps that need UAC (like RegEdit) can't use it anymore.
So, all good.
