source: trunk/Utilities/Miscellaneous/RunAsUser/CommandLineAsUser/CommandLineAsUser.cpp

Last change on this file was 540, checked in by roman, 7 years ago
File size: 2.3 KB
Line 
1#include "stdafx.h"
2#include <atlsecurity.h>
3
4int _tmain(int argc, _TCHAR* argv[])
5{
6        _ATLTRY
7        {
8                //CAccessToken ProcessToken;
9                //if(!ProcessToken.GetProcessToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES))
10                //      AtlThrowLastWin32();
11                //CTokenPrivileges IncreaseQuotaNameTokenPrivileges, AssignPrimaryTokenTokenPrivileges;
12                //if(!ProcessToken.EnablePrivilege(SE_INCREASE_QUOTA_NAME, &IncreaseQuotaNameTokenPrivileges))
13                //      AtlThrowLastWin32();
14                //if(!ProcessToken.EnablePrivilege(SE_ASSIGNPRIMARYTOKEN_NAME, &AssignPrimaryTokenTokenPrivileges))
15                //      AtlThrowLastWin32();
16
17                //CAccessToken Token;
18                //if(!Token.LogonUser(_T("Test"), _T("."), _T("tset")))
19                //      AtlThrowLastWin32();
20                ////if(!Token.ImpersonateLoggedOnUser())
21                ////    AtlThrowLastWin32();
22
23                TCHAR pszCurrentDirectory[MAX_PATH] = { 0 };
24                GetCurrentDirectory(_countof(pszCurrentDirectory), pszCurrentDirectory);
25
26                STARTUPINFO StartupInformation;
27                ZeroMemory(&StartupInformation, sizeof StartupInformation);
28                StartupInformation.cb = sizeof StartupInformation;
29                PROCESS_INFORMATION ProcessInformation;
30                ZeroMemory(&ProcessInformation, sizeof ProcessInformation);
31
32                #define A _T("C:\\Windows\\System32\\cmd.exe")
33                //#define A _T("C:\\Windows\\System32\\Taskmgr.exe")
34
35                //if(!CreateProcess(A, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &StartupInformation, &ProcessInformation))
36                //if(!CreateProcessAsUser(Token.GetHandle(), A, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &StartupInformation, &ProcessInformation))
37                //if(!Token.CreateProcessAsUser(A, NULL, &ProcessInformation, &StartupInformation, CREATE_NEW_CONSOLE, TRUE))
38                //      AtlThrowLastWin32();
39
40                if(!CreateProcessWithLogonW(_T("Test"), _T("."), _T("tset"), LOGON_WITH_PROFILE, A, NULL, CREATE_NEW_CONSOLE, NULL, pszCurrentDirectory, &StartupInformation, &ProcessInformation))
41                        AtlThrowLastWin32();
42
43                _tprintf(_T("Process Identifier: %d (0x%04X)\n"), ProcessInformation.dwProcessId, ProcessInformation.dwProcessId);
44                _tprintf(_T("Thread Identifier: %d (0x%04X)\n"), ProcessInformation.dwThreadId, ProcessInformation.dwThreadId);
45                reinterpret_cast<CHandle&>(ProcessInformation.hThread).Close();
46                reinterpret_cast<CHandle&>(ProcessInformation.hProcess).Close();
47        }
48        _ATLCATCH(Exception)
49        {
50                _tprintf(_T("Error: 0x%08X\n"), (HRESULT) Exception);
51        }
52        return 0;
53}
54
Note: See TracBrowser for help on using the repository browser.