Link to home
Start Free TrialLog in
Avatar of schiva
schiva

asked on

How to execute code with current user privilege on the service application.

Hello.
I made a serivce application. ( it is executed with System user account )

When i trying to some jobs on the service application, the code does not work.
Because of service application has limitted privileges.
So, i must made other win32 program which is executed by service application with current logon user system privileges.

I think this way is not efficient. Because i must made another program.

So, I looking for the solution that execute some procedure or function with current logon user privileges at the service application.

For Example)
// Service application body
begin
....
   do_excute_some_code( parameter 1 ); // this procedure will execute with current logon user privileges

end;

Is this possible ???
If you know how to do this, please let me know or give me some example code.

Thanks.
Avatar of SteveBay
SteveBay
Flag of United States of America image

I am almost certain you will not be able to change the account under which you are running while running.
Is there a reason why you can not just run the entire service from a user's account with all of the approriate priveleges?

As an alternative you could perhaps write two services and run them under different accounts and have service1 send a message to service2 that will then execute some code but it sounds like alot of work.
you need to change to the user you need with impersonate user and then run that task

  if LogonUser(user, domain or empty string , pass, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, h) then
  try
    if ImpersonateLoggedOnUser(h) then
    try
      do whatever as user
    finally
      if not RevertToSelf then// either this, or exit the thread
        showmessage(syserrormessage(getlasterror));
    end
    else
      showmessage(syserrormessage(getlasterror));
  finally
    closehandle(h);        
  end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Wegelin
Wegelin
Flag of Switzerland image

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
lol, mlmcc
accepting a VB solution in a Delphi zone

ahh, there is general programming languages too