source: trunk/Utilities/Miscellaneous/RunAsUser/SetPriorityClassAsUser/SetPriorityClassAsUser.cpp @ 937

Last change on this file since 937 was 540, checked in by roman, 8 years ago
File size: 4.4 KB
Line 
1#include "stdafx.h"
2#include <VersionHelpers.h>
3#include <atlsecurity.h>
4//#include <conio.h>
5
6#define U _T("Roman")
7#define P _T("***")
8
9int _tmain(int argc, _TCHAR* argv[])
10{
11        _ATLTRY
12        {
13                TCHAR pszUserName[128] = { 0 };
14                DWORD nUserNameLength = _countof(pszUserName);
15                GetUserName(pszUserName, &nUserNameLength);
16                _tprintf(_T("User Name: %s\n"), pszUserName);
17                if(_tcsicmp(pszUserName, U) != 0)
18                {
19                        TCHAR pszModuleFileName[MAX_PATH] = { 0 };
20                        GetModuleFileName(NULL, pszModuleFileName, _countof(pszModuleFileName));
21                        TCHAR pszCurrentDirectory[MAX_PATH] = { 0 };
22                        GetCurrentDirectory(_countof(pszCurrentDirectory), pszCurrentDirectory);
23                        STARTUPINFO StartupInformation;
24                        ZeroMemory(&StartupInformation, sizeof StartupInformation);
25                        StartupInformation.cb = sizeof StartupInformation;
26                        PROCESS_INFORMATION ProcessInformation;
27                        ZeroMemory(&ProcessInformation, sizeof ProcessInformation);
28                        if(!CreateProcessWithLogonW(U, _T("."), P, LOGON_WITH_PROFILE, pszModuleFileName, NULL, 0, NULL, pszCurrentDirectory, &StartupInformation, &ProcessInformation))
29                                AtlThrowLastWin32();
30                        _tprintf(_T("Process Identifier: %d (0x%04X)\n"), ProcessInformation.dwProcessId, ProcessInformation.dwProcessId);
31                        WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
32                        reinterpret_cast<CHandle&>(ProcessInformation.hThread).Close();
33                        reinterpret_cast<CHandle&>(ProcessInformation.hProcess).Close();
34                        return 1;
35                }
36
37                CAccessToken ProcessToken;
38                if(!ProcessToken.GetProcessToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES))
39                        AtlThrowLastWin32();
40                _tprintf(_T("GetProcessToken Succeeded\n"));
41
42                if(IsWindowsVistaOrGreater())
43                {
44                        TOKEN_ELEVATION TokenElevation;
45                        DWORD nTokenElevationSize;
46                        if(!GetTokenInformation(ProcessToken.GetHandle(), TOKEN_INFORMATION_CLASS::TokenElevation, &TokenElevation, sizeof TokenElevation, &nTokenElevationSize))
47                                AtlThrowLastWin32();
48                        ATLASSERT(nTokenElevationSize == sizeof TokenElevation);
49                        _tprintf(_T("Process Token Elevation: %d\n"), TokenElevation.TokenIsElevated);
50                        if(!TokenElevation.TokenIsElevated)
51                        {
52                                TCHAR pszModuleFileName[MAX_PATH] = { 0 };
53                                GetModuleFileName(NULL, pszModuleFileName, _countof(pszModuleFileName));
54                                TCHAR pszCurrentDirectory[MAX_PATH] = { 0 };
55                                GetCurrentDirectory(_countof(pszCurrentDirectory), pszCurrentDirectory);
56                                SHELLEXECUTEINFO Information;
57                                ZeroMemory(&Information, sizeof Information);
58                                Information.cbSize = sizeof Information;
59                                Information.fMask = SEE_MASK_NOCLOSEPROCESS; 
60                                Information.lpVerb = _T("runas");
61                                Information.lpFile = pszModuleFileName;
62                                Information.nShow = SW_SHOWNORMAL; 
63                                //Information.lpParameters = ;
64                                if(!ShellExecuteEx(&Information))
65                                        AtlThrowLastWin32();
66                                WaitForSingleObject(Information.hProcess, INFINITE);
67                                reinterpret_cast<CHandle&>(Information.hProcess).Close();
68                                return 2;
69                        }
70                }               
71
72                //if(!ProcessToken.EnablePrivilege(SE_DEBUG_NAME))
73                //      AtlThrowLastWin32();
74                //_tprintf(_T("EnablePrivilege SE_DEBUG_NAME Succeeded\n"));
75
76                //CAccessToken Token;
77                //if(!Token.LogonUser(_T("Roman"), _T("."), _T("9692")))
78                //      AtlThrowLastWin32();
79                //_tprintf(_T("LogonUser Succeeded\n"));
80                //if(!Token.ImpersonateLoggedOnUser())
81                //      AtlThrowLastWin32();
82                //_tprintf(_T("ImpersonateLoggedOnUser Succeeded\n"));
83                //TCHAR pszUserName[128] = { 0 };
84                //DWORD nUserNameLength = _countof(pszUserName);
85                //GetUserName(pszUserName, &nUserNameLength);
86                //_tprintf(_T("User Name: %s\n"), pszUserName);
87
88                //MessageBox(GetActiveWindow(), _T("Debug"), _T("Debug"), MB_OK);
89
90                CHandle Process;
91                Process.Attach(OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION, FALSE, 9632));
92                if(!Process)
93                        AtlThrowLastWin32();
94                DWORD nPriorityClass = GetPriorityClass(Process);
95                _tprintf(_T("Priority Class: %d\n"), nPriorityClass);
96                nPriorityClass = (nPriorityClass == NORMAL_PRIORITY_CLASS) ? BELOW_NORMAL_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
97                if(!SetPriorityClass(Process, nPriorityClass))
98                        AtlThrowLastWin32();
99                nPriorityClass = GetPriorityClass(Process);
100                _tprintf(_T("Priority Class: %d\n"), nPriorityClass);
101        }
102        _ATLCATCH(Exception)
103        {
104                switch((HRESULT) Exception)
105                {
106                case E_ACCESSDENIED:
107                        _tprintf(_T("Error: E_ACCESSDENIED (0x%08X)\n"), (HRESULT) Exception);
108                        break;
109                default:
110                        _tprintf(_T("Error: 0x%08X\n"), (HRESULT) Exception);
111                }
112        }
113        //_tprintf(_T("Type any key to exit...\n"));
114        //_gettch();
115        return 0;
116}
117
Note: See TracBrowser for help on using the repository browser.