windows - create process in user session from service -



windows - create process in user session from service -

i trying create service create process in opened session in windows. have code:

sessionid =wtsgetactiveconsolesessionid(); if (wtsqueryusertoken(sessionid,&dummy)) { if (!duplicatetokenex(dummy, token_all_access, null, securitydelegation, tokenprimary, &token)) { closehandle(dummy); homecoming false; } closehandle(dummy); // create process user desktop myfile = fopen("c:\\temp\\test123.txt", "a"); fprintf(myfile, "before create!!!!\n"); fclose(myfile); if (!createprocessasuser(token, null,null, null, null, false, create_new_console, null, null, &si, &pi)) { // "new console" necessary. otherwise process can hang our main process closehandle(token); myfile = fopen("c:\\temp\\test123.txt", "a"); fprintf(myfile, " create failed!\n"); fclose(myfile); homecoming false; } closehandle(token); } else { myfile = fopen("c:\\temp\\test123.txt", "a"); fprintf(myfile, "dummy fail\n"); fprintf(myfile, "last error %d \n", getlasterror()); fclose(myfile); } //int ret = createprocess(file_exec, null, null, null, false, 0, null, null, &si, &pi);

if utilize lastly lin e(that commented) in install of service works fine because runs when service been installed happens within user session when want service it fails, sessionid ok, failure starts @ :

if (wtsqueryusertoken(sessionid,&dummy)) {

i know wtsqueryusertoken function should run service, sessionid 1 (and real number cmd check) , dummy suppose hold user token after reason fails.... ideas?

i utilize code similar yours in own service , works fine. there things need taken business relationship code showed not doing:

when calling wtsqueryusertoke(), have create sure service process has se_tcb_name privilege enabled. utilize adjusttokenprivileges() that.

the session id returned wtsgetactiveconsolesessionid() may not right session need run spawned process on! returns session id attached physical console (screen/keyboard/mouse) of local machine, if any. session may displaying secure winlogon desktop, meaning no user logged in physical machine, calling wtsqueryusertoken() on session id fail error_no_token error. user can log in on remote desktop connection, instance, in case connection running in different session console. if want spawned process run in session has user logged in, need utilize wtsenumeratesessions() find session in wtsactive state. , then, wtsqueryusertoken() may not homecoming token depending on how user logged in, need phone call wtsqueryusertoken() on each active session find until find 1 gives token.

when calling duplicatetokenex(), utilize securityidentification instead of securitydelegation.

when calling createprocessasuser(), can phone call createenvironmentblock() first create environment appropriate specific user , pass pointer createprocessasuser(). otherwise, spawned process utilize service's environment instead. step optional, depending on particular needs of spawned app.

windows winapi service

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -