-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreg.cpp
More file actions
169 lines (155 loc) · 4.86 KB
/
Copy pathreg.cpp
File metadata and controls
169 lines (155 loc) · 4.86 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "reg.h"
bool Reg::is_ok()
{
ifstream ifs;
ifs.open("vk.ini", ios::in);
if (!ifs.is_open())
{
ifs.close();
return 0;
}
char c;
ifs >> c;
if (ifs.eof())
{
ifs.close();
return 0;
}
ifs.close();
return 1;
}
void Reg::StringToWstring(std::string& szDst, wchar_t*wchar)
{
wchar_t * wText = wchar;
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);//WideCharToMultiByte的运用
char *psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
psText = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);//WideCharToMultiByte的再次运用
szDst = psText;// std::string赋值
delete []psText;// psText的清除
}
void Reg::init()
{
ofstream tmp("vk.ini");
wchar_t dp[MAX_PATH];
CMyINI *tt=new CMyINI();
tt->SetValue("Orig","self-starting","false");
GetModuleFileName(NULL,dp,MAX_PATH);
string noname;
StringToWstring(noname,dp);
tt->SetValue("Orig","path",noname);
tt->SetValue("Orig","start","false");
tt->SetValue("Orig","show_message","true");
tt->SetValue("WinSetting","bgColor","#7c000000");
tt->SetValue("WinSetting","wdColor","#ffffffff");
tt->SetValue("WinSetting","posx","243");
tt->SetValue("WinSetting","posy","864");
tt->WriteINI("vk.ini");
}
Reg::Reg()
{
if(!is_ok()) init();
p=new CMyINI();
p->ReadINI("vk.ini");
string path,start,self_starting,np;
wchar_t npp[MAX_PATH];
path=p->GetValue("Orig","path");
self_starting=p->GetValue("Orig","self-starting");
GetModuleFileName(NULL,npp,MAX_PATH);
StringToWstring(np,npp);
if(np!=path){
p->SetValue("Orig","path",np);
if(self_starting=="true"){
change(0);
p->SetValue("Orig","self-starting","false");
}
}
p->WriteINI("vk.ini");
}
bool Reg::check(int what)
{
p->ReadINI("vk.ini");
switch(what){
case 0: return p->GetValue("Orig","start")==string("true");
case 1: return p->GetValue("Orig","self-starting")==string("true");
case 2: return p->GetValue("Orig","show_message")==string("true");
}
return 0;
}
string Reg::checkinf(int what)
{
p->ReadINI("vk.ini");
switch(what){
case 0: return p->GetValue("WinSetting","bgColor");
case 1: return p->GetValue("WinSetting","wdColor");
case 2: return p->GetValue("WinSetting","posx");
case 3: return p->GetValue("WinSetting","posy");
}
return "\0";
}
void Reg::change(bool to)
{
if(to){
HKEY hKey;
wchar_t strRegPath[] = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
//1、找到系统的启动项
if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) ///打开启动项
{
//2、得到本程序自身的全路径
TCHAR strExeFullDir[MAX_PATH];
GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
//3、判断注册表项是否已经存在
TCHAR strDir[MAX_PATH] = {};
DWORD nLength = MAX_PATH;
long result = RegGetValue(hKey, nullptr, L"VisualKeyboard", RRF_RT_REG_SZ, 0, strDir, &nLength);
//4、已经存在
if (result != ERROR_SUCCESS || _tcscmp(strExeFullDir, strDir) != 0)
{
//5、添加一个子Key,并设置值,"VisualKeyboard"是应用程序名字(不加后缀.exe)
RegSetValueEx(hKey, L"VisualKeyboard", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1)*sizeof(TCHAR));
//6、关闭注册表
RegCloseKey(hKey);
}
}
else
{
throw 1;
}
}
else{
HKEY hKey;
wchar_t strRegPath[] = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
//1、找到系统的启动项
if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
//2、删除值
RegDeleteValue(hKey, L"VisualKeyboard");
//3、关闭注册表
RegCloseKey(hKey);
}
}
}
BOOL Reg::EnableDebugPrivilege() {
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
return FALSE;
}
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue))
{
CloseHandle(hToken);
return false;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL))
{
CloseHandle(hToken);
return false;
}
return true;
}
Reg::~Reg() = default;