-
Notifications
You must be signed in to change notification settings - Fork 16
/
Main.cpp
134 lines (112 loc) · 3.56 KB
/
Main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#include "./MiniThing/Core/MiniThingCore.h"
#include <QtWidgets/QApplication>
#include "./MiniThing/Qt/MiniThingQt.h"
//==========================================================================
// Static Functions //
//==========================================================================
static bool IsRunAsAdmin(void)
{
BOOL isRunAsAdmin = FALSE;
DWORD dwError = ERROR_SUCCESS;
PSID pAdminGroup = NULL;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
if (!AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pAdminGroup))
{
dwError = GetLastError();
goto Exit;
}
if (!CheckTokenMembership(NULL, pAdminGroup, &isRunAsAdmin))
{
dwError = GetLastError();
goto Exit;
}
Exit:
if (pAdminGroup)
{
FreeSid(pAdminGroup);
pAdminGroup = NULL;
}
if (ERROR_SUCCESS != dwError)
{
throw dwError;
}
return isRunAsAdmin;
}
static void GetAdminPrivileges(CString strApp, std::wstring args)
{
SHELLEXECUTEINFO executeInfo;
memset(&executeInfo, 0, sizeof(executeInfo));
executeInfo.lpFile = strApp;
executeInfo.cbSize = sizeof(executeInfo);
executeInfo.lpVerb = _T("runas");
executeInfo.fMask = SEE_MASK_NO_CONSOLE;
executeInfo.nShow = SW_SHOWDEFAULT;
executeInfo.lpParameters = args.c_str();
ShellExecuteEx(&executeInfo);
WaitForSingleObject(executeInfo.hProcess, INFINITE);
}
static void CleanRegValue(void)
{
HKEY hKey;
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey))
{
LSTATUS ret = RegDeleteKey(hKey, L"MiniThing");
RegCloseKey(hKey);
}
}
//==========================================================================
// Main Entry //
//==========================================================================
int main(int argc, char *argv[])
{
// Check if current process run as admin
// if not, create a new process run as admin
// and exit current process
if (!IsRunAsAdmin())
{
WCHAR path[MAX_PATH] = { 0 };
GetModuleFileName(NULL, path, MAX_PATH);
// Pass down args
std::wstring args;
// Pass the first one cause is exe name itself
for (int i = 1; i < argc; i++)
{
std::string tmpStr = argv[i];
args += StringToWstring(tmpStr);
args += L" ";
}
GetAdminPrivileges(path, args);
return 0;
}
cmdline::parser parser;
// add specified type of variable.
// 1st argument is long name
// 2nd argument is short name (no short name if '\0' specified)
// 3rd argument is description
// 4th argument is mandatory (optional. default is false)
// 5th argument is default value (optional. it used when mandatory is false)
// parser.add<std::string>("clean", 'c', "clean app data", false, "false", cmdline::oneof<std::string>("true", "false"));
parser.add("clean", 'c', "clean app data");
// Parse command line
parser.parse_check(argc, argv);
if (parser.exist("clean"))
{
CleanRegValue();
return 0;
}
QApplication app(argc, argv);
MiniThingQt miniThingQt;
miniThingQt.show();
return app.exec();
}