RunAs - built-in

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
ctg
User
User
Posts: 21
Joined: Mon Sep 15, 2008 1:43 am

RunAs - built-in

Post 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
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: RunAs - built-in

Post by c4s »

Maybe some functions posted in this thread will help you?!
-> http://www.purebasic.fr/english/viewtop ... 13&t=41952
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Re: RunAs - built-in

Post by Droopy »

SuperRunas() from the DroopyLib do this
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: RunAs - built-in

Post by Trond »

Everything you asked for can already be done. Look at the manual in the section "Process".
ctg
User
User
Posts: 21
Joined: Mon Sep 15, 2008 1:43 am

Re: RunAs - built-in

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: RunAs - built-in

Post 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.
ctg
User
User
Posts: 21
Joined: Mon Sep 15, 2008 1:43 am

Re: RunAs - built-in

Post 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...
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: RunAs - built-in

Post 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.
ctg
User
User
Posts: 21
Joined: Mon Sep 15, 2008 1:43 am

Re: RunAs - built-in

Post by ctg »

Thanks mate... I also need to wait until the app is closed too, so I need to get an app handle...

Cheers.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: RunAs - built-in

Post by Trond »

The handle you're looking for is probably pi\hProcess.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: RunAs - built-in

Post 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. :cry:
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: RunAs - built-in

Post 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...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: RunAs - built-in

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: RunAs - built-in

Post by mk-soft »

Same on my machine.

Program stopped because UAC Requester not visible.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: RunAs - built-in

Post 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. ;)
Post Reply