I used the following code to imperonate the normal user. It is returning true. But when i am writing into the reigstry it is returning the error code 5(access denied). is my code right?
bool ImpersonateUser::Logon(char* userName, char* domain, char* password) { if(init_) Logoff();
if(userName == NULL) // Must at least specify a username { errorCode_ = ERROR_BAD_ARGUMENTS; return false; }
// Attempt to log on as that user BOOL bLoggedOn = FALSE;
if(domain == NULL ) // Domain name was specified bLoggedOn = LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &userToken_); else bLoggedOn = LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,/*LOGON32_LOGON_NEW_CREDENTIALS*/LOGON32_PROVIDER_WINNT50, &userToken_);
if(!bLoggedOn) { errorCode_ = GetLastError(); return false; }
// Now impersonate them if(!ImpersonateLoggedOnUser(userToken_)) { errorCode_ = GetLastError(); return false; }
init_ = true; return true; }
//it is returning true
LONG lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("Software\\Wonderware\\InTouch\\Installation"),0,KEY_SET_VALUE,&hKey);
lRet ::RegSetValueEx(hKey,"InstallPath",0,REG_SZ,(BYTE*)("C:\\Windows"),dwSize); //it is retunging 5(access denied)
is something wrong with impersonation code
Thanks |