c++ - Why LoadUserProfile() fails with error 5 "Denied Access" in this code running in system service? -


i have piece of code below running in system level windows service. if add createenvironmentblock() , createprocessasuser() code, works. use "loaduserprofile()", fails error 5 should mean "access denied". please check missing. want retrieve user level registry value system service. comment in code way, failed retrieve user level registry value.

void getuserregistry() { #ifdef q_os_win      dword lasterror = 0;      dword sessionid = wtsgetactiveconsolesessionid();     qinfo() << "session id = " << sessionid;      wchar_t* ppusername[100];     dword sizeofusername;     wtsquerysessioninformation(wts_current_server_handle, sessionid, wtsusername, ppusername, &sizeofusername);     qinfo() << "windows user name = " << qstring::fromwchararray(*ppusername);      std::wstring strvalueofbindir = l"unknown value";     // long regopenresult = error_success;      handle husertoken = null;     handle hfaketoken = null;      if (wtsqueryusertoken(sessionid, &husertoken))     {          if (duplicatetokenex(husertoken, token_assign_primary | token_all_access, 0, securityimpersonation, tokenprimary, &hfaketoken) == true)          {             qinfo() << "before impersonateloggedonuser()......";             if (impersonateloggedonuser(hfaketoken))             {                 hkey hkey;                  // regopenresult = regopencurrentuser(key_read, &hkey);                  profileinfo profileinfo;                 zeromemory( &profileinfo, sizeof( profileinfo ) );                 profileinfo.dwsize = sizeof( profileinfo );                 profileinfo.lpusername = *ppusername;                 // wchar_t roamingpath[] = l"c:\\users\\finix";  // l"c:\\users\\finix\\appdata\\roaming";                 // profileinfo.lpprofilepath = roamingpath;                  if (loaduserprofile(hfaketoken, &profileinfo))                 {                     handle hprofile = profileinfo.hprofile;                      regopenkeyex(hkey_current_user,                                  text("software\\baidu\\baiduyunguanjia"),                                  0,                                  key_read,                                  &hkey);                      getstringregkey(hkey, text("installdir"), strvalueofbindir, text("unknown"));                      unloaduserprofile(hfaketoken, hprofile);                 }                 else                 {                     lasterror = getlasterror();                 }                  reverttoself();              }             else             {                 qcritical() << "failed impersonateloggedonuser...";             }              closehandle(hfaketoken);         }         else         {             qcritical() << "failed call duplicatetokenex...";         }          closehandle(husertoken);     }     else     {         qcritical() << "failed user token of session " << sessionid;     }      if (lasterror)     {         qcritical() << "failed loaduserprofile(), error " << lasterror;     }  //    if (regopenresult != error_success) //    { //        qcritical() << "failed call regopencurrentuser(), error " << regopenresult; //    }      qinfo() << "the value of registry " << qstring::fromwchararray( strvalueofbindir.c_str() );  #endif } 

the reason can't call loaduserprofile you're impersonating user. loaduserprofile requires admin privilege, i.e., should call in own context, not in impersonated context. isn't function user load own profile, ordinarily system calls on user's behalf.

the reason shouldn't call loaduserprofile user logged on, profile loaded. know user in question logged on, because wtsqueryusertoken cannot used obtain tokens users aren't.

(well, there may edge cases user being logged on or off. calling loaduserprofile function during logon/logoff unwise in case.)

if want open logged-on user's registry hive, use regopencurrentuser. (note do want use impersonation function, documented.)


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -