Link to home
Start Free TrialLog in
Avatar of flasht
flasht

asked on

Run as other user...

How do i run my application as other user? I mean so the application would load itself as other user on its profile like runas /profil ... but so my application would do it itself?
Avatar of TheRealLoki
TheRealLoki
Flag of New Zealand image

You need to use "ImpersonateUser"
take a look at this question
https://www.experts-exchange.com/questions/21027137/Impersonate-User-problems.html

I think your user needs some privileges set up
I can't quite recall if you need
       "Act as part of the operating system"  - SeTcbPrivilege
       "Increase quotas"                      - SeIncreaseQuota
       "Replace a process level token"        - SeAssignPrimaryToken
or any of them in fact, been so long since I had to do this sort of thing  :-)

Avatar of pcsentinel
pcsentinel

Try this

function CreateProcessWithLogon(user,  password, cmdLine: string) : DWord;
const
      LOGON_WITH_PROFILE=1;
var
 lsi: TStartupInfo;
 lpi: TProcessInformation;
begin
  ZeroMemory(@lsi, sizeOf(lsi));
  lsi.cb := sizeOf(lsi);
  ZeroMemory(@lpi, sizeOf(lpi));
  CreateProcessWithLogonW(PWideChar(wideString(user)),nil,PWideChar(wideString(password)),
            LOGON_WITH_PROFILE, nil,PWideChar(wideString(cmdLine)),CREATE_DEFAULT_ERROR_MODE or NORMAL_PRIORITY_CLASS,
            nil, nil, lsi, lpi);
      Result := GetLastError
end;
Avatar of flasht

ASKER

@TheRealLoki: I need exatly the same thing as making shortcut and in advanced options setting up "run with other privilages" (translated from polish - so it may be something else)

@pcsentinel: The problem is i would like my application to run itself as other user, not other process...
Avatar of flasht

ASKER

Bah... its seems that both examples aren't answer to my question...

Ok... the problem is more complicated than i thought anyway...
First of all i want my application to run as other user by double-clicking on executable of my application. For example im on Administrator account, double click on my application executable and its being run as OtherUser ... so everything would be from other user - IE settings, Cache Directory, Cookies Directory etc etc... But... my application has MadExcept in itself and on exception it restarts itself... So after restart it appears that it was run by this OtherUser that cant set any privilages because its normal user (not administrator). Hope you understand what the problem is... any suggestions on how to do that?

New Point Value: 200 pts
Sorry, i don't know how to do that. THere is source for making "SUser.exe" a commandline to start your app, or you could always tell your app to start itself again
e.g. your app starts, it knows it does not have anough access rights, so it doesthhe createprocess with the correct rights, running the same exe again, then the 1st instance closes.
the 2nd instance runs, sees it has enough access rights, and continues
Simplest answer is to have 2 apps, one which is a launcher for the other the launcher calls CreateProcessWithLogon
Avatar of flasht

ASKER

pcsentinel: your code aint working...

[Error] Unit1.pas(35): Undeclared identifier: 'CreateProcessWithLogonW'
ASKER CERTIFIED SOLUTION
Avatar of pcsentinel
pcsentinel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial