c++ - CreateProcessAsUser fail,use GetLastError() to get the error code is 1314 -
i use win7 os , develop environment vs2005.
the situation want create process current account's priviledge.(such as: in normal account ,right click program choice "run admin" )
i refer other people's way: 1.get token of process explorer.exe; 2.improve priviledge; 3.use createprocessasuser create process.
but createprocessasuser failed,and use getlasterror() error code 1314.
because of that, think i'am crazy now. can tell me what's wrong in program. thank you!!!
#include <iostream> using namespace std; #include "windows.h" #include "tlhelp32.h" bool getprocesstokenbyname(handle &htoken, lptstr szprocessname) { // var init startupinfo st; process_information pi; processentry32 ps; handle hsnapshot; zeromemory(&st, sizeof(startupinfo)); zeromemory(&pi, sizeof(process_informatio n)); st.cb = sizeof(startupinfo); zeromemory(&ps,sizeof(processentry32)); ps.dwsize = sizeof(processentry32); // find explorer.exe hsnapshot = createtoolhelp32snapshot( th32cs_snapprocess, 0); if(hsnapshot == invalid_handle_value) { return false; } if(!process32first(hsnapshot,&ps)) { return false; } { wprintf(_t("%s , %u\n"), ps.szexefile, ps.th32processid); // compare process name if(lstrcmpi(ps.szexefile,szprocessname)==0) { // find //*lppid = ps.th32processid; //closehandle(hsnapshot); //return true; handle hprocess = openprocess(process_query_information, false, ps.th32processid); bool bret = false; handle tmptoken; if( openprocesstoken(hprocess, /*token_query*/token_all_access, &tmptoken) ) { bret = duplicatetokenex( tmptoken, //_in_ handle hexistingtoken, maximum_allowed, //_in_ dword dwdesiredaccess, null, //_in_opt_ lpsecurity_attributes lptokenattributes, securityidentification, //_in_ security_impersonation_level impersonationlevel, tokenprimary, //_in_ token_type tokentype, &htoken //_out_ phandle phnewtoken ); //dword dwsessionid = wtsgetactiveconsolesessionid(); //settokeninformation(htoken,tokensessionid,(void*)dwsessionid,sizeof(dword)); //setprivilege(htoken, se_assignprimarytoken_name, true); } else { printf("openprocesstoken error: %u\n", getlasterror()); } closehandle (hsnapshot); return (bret); } }while(process32next(hsnapshot,&ps)); // didn't find closehandle(hsnapshot); return false; } bool runasuser( ) { handle htoken; if( getprocesstokenbyname( htoken, _t("explorer.exe") ) ) { if( htoken != invalid_handle_value ) { startupinfo si; process_information pi; zeromemory(&si, sizeof(startupinfo)); si.cb= sizeof(startupinfo); si.lpdesktop = text("winsta0\\default"); { token_privileges tp; tp.privilegecount =1; if(!lookupprivilegevalue(null,se_assignprimarytoken_name/*se_debug_name*/,&tp.privileges[0].luid)) { printf("lookupprivilegevalue value error: %u\n",getlasterror()); } tp.privileges[0].attributes = se_privilege_enabled; if(!adjusttokenprivileges(htoken, false, &tp, sizeof(token_privileges), (ptoken_privileges)null, null) ) { printf("adjust privilege value error: %u\n",getlasterror()); } } printf("adjust privilege\n"); { token_privileges tp; tp.privilegecount =1; if(!lookupprivilegevalue(null,se_increase_quota_name/*se_debug_name*/,&tp.privileges[0].luid)) { printf("lookupprivilegevalue value error: %u\n",getlasterror()); } tp.privileges[0].attributes = se_privilege_enabled; if(!adjusttokenprivileges(htoken, false, &tp, sizeof(token_privileges), (ptoken_privileges)null, null) ) { printf("adjust privilege value error: %u\n",getlasterror()); } } bool bresult = createprocessasuser( htoken, //_in_opt_ handle htoken, _t("d:\\getmac.exe"), //_in_opt_ lpctstr lpapplicationname, null, //_inout_opt_ lptstr lpcommandline, null, //_in_opt_ lpsecurity_attributes lpprocessattributes, null, //_in_opt_ lpsecurity_attributes lpthreadattributes, false, //_in_ bool binherithandles, normal_priority_class, //_in_ dword dwcreationflags, null, //_in_opt_ lpvoid lpenvironment, null, //_in_opt_ lpctstr lpcurrentdirectory, &si, //_in_ lpstartupinfo lpstartupinfo, &pi //_out_ lpprocess_information lpprocessinformation ); closehandle(htoken); if( bresult ) { //succeed return true; } else { //fail dword dwerr = getlasterror(); printf( "error: %u\n", dwerr ); } } } else { printf("getprocesstokenbyname fail\n"); } return false; } int _tmain(int argc, _tchar* argv[]) { bool bret = runasuser(); printf("result: %d\n", bret); system("pause"); return 0; }
Comments
Post a Comment