forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUAC_Helper.cpp
99 lines (77 loc) · 2.1 KB
/
UAC_Helper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "stdafx.h"
#include "UAC_Helper.h"
#include "Misc.h"
CUAC_Helper::CUAC_Helper(void)
{
}
CUAC_Helper::~CUAC_Helper(void)
{
}
bool CUAC_Helper::PasteAsAdmin(HWND hWnd)
{
bool runningElevated = AmIRunningElevated();
bool theirRunningElevated = AreTheyRunningElevated(hWnd);
bool pasteAsAdmin = false;
if (runningElevated == false &&
theirRunningElevated)
{
pasteAsAdmin = true;
}
Log(StrF(_T("I'm running elevated: %d, They are running elevated: %d, PASTE AS ADMIN: %d"), runningElevated, theirRunningElevated, pasteAsAdmin));
return pasteAsAdmin;
}
bool CUAC_Helper::AmIRunningElevated()
{
bool ret = RunningElevated(GetCurrentProcess(), true);
return ret;
}
bool CUAC_Helper::AreTheyRunningElevated(HWND hWnd)
{
DWORD pid;
GetWindowThreadProcessId(hWnd, &pid);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, TRUE, pid);
bool ret = RunningElevated(hProcess, true);
return ret;
}
bool CUAC_Helper::RunningElevated(HANDLE hProcess, bool defaultValue)
{
bool fIsElevated = defaultValue;
DWORD dwError = ERROR_SUCCESS;
HANDLE hToken = NULL;
TOKEN_ELEVATION elevation;
if (hProcess == NULL)
{
dwError = GetLastError();
Log(StrF(_T("RunningElevated, initial Handle is NULL, Last Error: %d"), dwError));
}
else
{
if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hToken))
{
dwError = GetLastError();
Log(StrF(_T("RunningElevated, OpenProcessToken failed, Last Error: %d"), dwError));
}
else
{
DWORD dwSize;
if (!GetTokenInformation(hToken, TokenElevation, &elevation, sizeof(elevation), &dwSize))
{
// When the process is run on operating systems prior to Windows
// Vista, GetTokenInformation returns FALSE with the
// ERROR_INVALID_PARAMETER error code because TokenElevation is
// not supported on those operating systems.
dwError = GetLastError();
Log(StrF(_T("RunningElevated, GetTokenInformation failed, Last Error: %d"), dwError));
}
else
{
fIsElevated = elevation.TokenIsElevated == 1;
}
}
}
if (hProcess != NULL)
CloseHandle(hProcess);
if (hToken != NULL)
CloseHandle(hToken);
return fIsElevated;
}