create process in user session from service

后端 未结 2 896
执笔经年
执笔经年 2020-12-05 08:59

i am trying to make a service create a process in opened session in windows. i have this code:

    sessionId =WTSGetActiveConsoleSessionId();
if (WTSQueryUse         


        
2条回答
  •  一生所求
    2020-12-05 09:21

    The code is correct....make sure that you include "wtsapi.h" header file and this "#pragma comment(lib, "WtsApi32.lib")",because it is important to load the dll,else you will get linking error(error LNK2019: unresolved external symbol). And also duplicating token is not necessary...and privileges setting is not required because when the process is called from the service in your user account..the default session id will be 0 i.e,. SYSTEM account..which has all the privileges which a user can have...

    my code works properly when process is called from a service:

       LPCWSTR path=L"C:\\Windows\\System32\\notepad.exe";//change the path accordingly
    
         PROCESS_INFORMATION pi;
            STARTUPINFO si;
             DWORD nsid=1;
    
              ZeroMemory(&si, sizeof(si));
              si.cb = sizeof( si );
              HANDLE htoken;
    
              DWORD sessionId;
    
             sessionId =WTSGetActiveConsoleSessionId();
             WTSQueryUserToken(sessionId,&htoken);
             si.wShowWindow=TRUE;
             if (CreateProcessAsUser(htoken, path, NULL, NULL,
                          NULL, FALSE, 0, NULL, NULL, &si, &pi ))
              {
                 /* Process has been created; work with the process and wait for it to
                 terminate. */
                    WaitForSingleObject(pi.hProcess, INFINITE);
                   CloseHandle(pi.hThread);
                     CloseHandle(pi.hProcess);
               }
    
             CloseHandle(htoken);
    

    for clear idea about service refer here .Download the source code and the code is perfect. Just insert this piece of code in the 'ServiceWorkerThread' function.this piece of code is for opening an .exe (Eg.File Explorer,Notepad,etc.,) file from your service.The .exe file will open when you start your service from services.msc( windows application).

提交回复
热议问题