Solved

CreateProcess for Currently Logged On User from Service running System Account?

Posted on 2004-03-24
6
3,552 Views
Last Modified: 2012-12-12
The problem I am having is this:

There is a service that runs using the Local System Account, it needs to run in this context to prevent users from closing it.  This service monitors an application that MUST always remain open, this application has to run as the currently logged on user.  When I use CreateProcess from within the service to restart the application if it has been terminated by the user, CreateProcess runs the application within the Local System Account, this creates a problem since the application must run in the users account for reasons I wont go into here.

Question:

How do I use CreateProcess to launch an instance of specified application from the Currently Logged On users account.  I do not want to enter username and password information by using LogOnUser because the currently logged on user is already authenticated I should not have to do this, also this is not an option since the user does not interact with this program.  I believe what I will need to use is ImpersonateLoggedOnUser, and obtaining the current users process handle by using OpenProcessToken but after messing with this for nearly 2 weeks now I cannot figure out how to get this to work...  Any help would be appreciated!!
0
Comment
Question by:Magadass
  • 3
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 10668990
>>I believe what I will need to use is ImpersonateLoggedOnUser, and obtaining the
>>current users process handle by using OpenProcessToken

Indeed. E.g. like using

ImpersonateInteractiveUser()
{
   HANDLE hToken = NULL;                
   HANDLE hProcess = NULL;

   DWORD processID = GetExplorerProcessID();
   if( processID)
    {
    hProcess =
         OpenProcess(  
               PROCESS_ALL_ACCESS,
         TRUE,
          processID );

    if( hProcess)
        {
        if( OpenProcessToken(
                    hProcess,
              TOKEN_EXECUTE |
             TOKEN_READ |
             TOKEN_QUERY |
             TOKEN_ASSIGN_PRIMARY |
             TOKEN_QUERY_SOURCE |
             TOKEN_WRITE |
             TOKEN_DUPLICATE,
             &hToken))
        {
         ImpersonateLoggedOnUser( hToken);
          CloseHandle( hToken );
        }
        CloseHandle( hProcess );
    }
   }
}

Then, call 'CreateProcess()' to launch any application in the context of the logged on user and call 'RevertToSelf()'

BTW, 'GetExplorerProcessID();' can be implemented using http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q175/0/30.ASP&NoWebContent=1 ("HOWTO: Enumerate Applications Using Win32 APIs")
0
 
LVL 86

Expert Comment

by:jkr
ID: 10669196
0
 

Author Comment

by:Magadass
ID: 10669652
Is this sufficient jkr?

DWORD GetExplorerProcessID()
{
      HANDLE hSnapshot;
      PROCESSENTRY32 pe32;
      ZeroMemory(&pe32,sizeof(pe32));
      DWORD temp;

    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
      
      pe32.dwSize = sizeof(PROCESSENTRY32);

      if(Process32First(hSnapshot,&pe32))
      {
            do
            {
                  if(!strcmp(pe32.szExeFile,"explorer.exe"))
                  {
                        MessageBox(0,pe32.szExeFile,"test",0);
                        temp = pe32.th32ProcessID;
                        break;
                  }

            }while(Process32Next(hSnapshot,&pe32));
      }
return temp;
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Magadass
ID: 10669761
Wow that worked great man...  Very awesome, thanks for your help you are da man!!!
0
 

Author Comment

by:Magadass
ID: 10669762
Wow that worked great man...  Very awesome, thanks for your help you are da man!!!
0
 
LVL 86

Expert Comment

by:jkr
ID: 10669800
You're welcome :o)

Sorry for the delay, but my notifs don't seem to be working...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now