-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAddToPath.cpp
More file actions
437 lines (354 loc) · 9.62 KB
/
AddToPath.cpp
File metadata and controls
437 lines (354 loc) · 9.62 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* AddToPath plugin for NSIS
* Add and remove record from PATH environment variable
* Copyright (C) 2015 Victor Spirin <vvs13@mail.ru>
* some defintions I took from AccessControl plugin by Mathias Hasselmann
*/
#define WIN32_LEAN_AND_MEAN
#ifdef _WIN64
#define WINVER 0x502
#else
#define WINVER 0x0600
#endif
#include <windows.h>
#ifdef UNICODE
#include "nsis_unicode/pluginapi.h"
#else
#include "nsis_ansi/pluginapi.h"
#endif
#include <aclapi.h>
#include <sddl.h>
#include <tchar.h>
#include <stdlib.h>
#include <winnls.h>
/*****************************************************************************
GLOBAL VARIABLES
*****************************************************************************/
HINSTANCE g_hInstance = NULL;
int g_string_size = 1024;
extra_parameters* g_extra = NULL;
/*****************************************************************************
UTILITIES
*****************************************************************************/
#define SIZE_OF_ARRAY(Array) (sizeof((Array)) / sizeof(*(Array)))
#define ARRAY_CONTAINS(Array, Index) (Index >= 0 && Index < SIZE_OF_ARRAY(Array))
void* LocalAllocZero(size_t cb) { return LocalAlloc(LPTR, cb); }
inline void* LocalAlloc(size_t cb) { return LocalAllocZero(cb); }
/*****************************************************************************
PLUG-IN HANDLING
*****************************************************************************/
#define PUBLIC_FUNCTION(Name) \
extern "C" void __declspec(dllexport) __cdecl Name(HWND hWndParent, int string_size, TCHAR* variables, stack_t** stacktop, extra_parameters* extra) \
{ \
EXDLL_INIT(); \
g_string_size = string_size; \
g_extra = extra;
#define PUBLIC_FUNCTION_END \
}
//only for test
bool getPath(TCHAR *buf, DWORD* buflen)
{
HKEY phk;
LONG ret;
ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"),0,
KEY_READ, // security access mask :KEY_ALL_ACCESS
&phk // address of handle of open key
);
if(ret!=ERROR_SUCCESS){
wsprintf(buf, TEXT("Error 1. Error code: %d"), ret);
return false;
}
DWORD dw;
dw=REG_EXPAND_SZ;
//DWORD tmp=len;
TCHAR *str= (TCHAR*)LocalAlloc(8001*sizeof(TCHAR));
DWORD n;
n=8000;
ret=RegQueryValueEx(
phk, // handle of key to query
TEXT("Path"), // address of name of value to query
NULL, // reserved
&dw,//REG_DWORD, // address of buffer for value type
//(BYTE *)buf, // address of value data
//(LPDWORD)buflen // size of value data
(BYTE *)str, // address of value data
(LPDWORD)&n // size of value data
);
if(ret!=ERROR_SUCCESS){
*buf=0;
RegCloseKey(phk);
wsprintf(buf, TEXT("Error 2. Error code: %d"), ret);
LocalFree(str);
return false;
}
lstrcpyn(buf,str,*buflen);
LocalFree(str);
RegCloseKey(phk);
return true;
}
//for test
PUBLIC_FUNCTION(GetPathString)
{
TCHAR *name = (TCHAR*)LocalAlloc(string_size*sizeof(TCHAR)), *retstr = TEXT("error");
DWORD dwName = string_size;
//if (name && getPath(name, &dwName)) retstr = name;
if (name && getPath(name, &dwName)) retstr = name;
pushstring(retstr);
LocalFree(name);
}
PUBLIC_FUNCTION_END
char *_strstr(char *i, const char *s)
{
if (lstrlen(i)>=lstrlen(s)) while (i[lstrlen(s)-1])
{
int l=lstrlen(s)+1;
char *ii=i;
const char *is=s;
while (--l>0)
{
if (*ii != *is) break;
ii++;
is++;
}
if (l==0) return i;
i++;
}
return NULL;
}
bool ChangePath(TCHAR *path, bool isAdd,bool allUser)
{
HKEY phk;
LONG ret;
if(lstrlen(path)>256) return false;
if(allUser){
ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"),
0,
KEY_ALL_ACCESS, // security access mask :KEY_ALL_ACCESS
&phk // address of handle of open key
);
}
else{
ret=RegOpenKeyEx(HKEY_CURRENT_USER,
TEXT("Environment"),
0,
KEY_READ, // security access mask :KEY_ALL_ACCESS
&phk // address of handle of open key
);
}
if(ret!=ERROR_SUCCESS){
return false;
}
DWORD dw;
dw=REG_EXPAND_SZ;
TCHAR *str= (TCHAR*)LocalAlloc(8500*sizeof(TCHAR));
DWORD n=8192;
ret=RegQueryValueEx(
phk, // handle of key to query
TEXT("Path"), // address of name of value to query
NULL, // reserved
&dw,//REG_DWORD, // address of buffer for value type
//(BYTE *)buf, // address of value data
//(LPDWORD)buflen // size of value data
(BYTE *)str, // address of value data
(LPDWORD)&n // size of value data
);
if(ret!=ERROR_SUCCESS || str[0] == 0){
RegCloseKey(phk);
LocalFree(str);
return false;
}
//find path
//TCHAR *p = _tcsstr(str,path);
TCHAR *p = _strstr(str,path);
//MessageBox(0,str,path,MB_OK);
if(p!=NULL){//exist
//MessageBox(0,p,"NOT NULL",MB_OK);
if(isAdd){
LocalFree(str);
RegCloseKey(phk);
return true;
}
//remove
//TCHAR *tmp=(TCHAR*)LocalAlloc(8500*sizeof(TCHAR));
//if(tmp){
*p=0;
if(p!=str && (*(p-1)==';')) *(p-1)=0;
lstrcat(str,p+lstrlen(path));
}
else{
//MessageBox(0,"NULL","",MB_OK);
if(!isAdd){
LocalFree(str);
RegCloseKey(phk);
return true;
}
int l=lstrlen(str);
if(l>0 && str[l-1]!=';') lstrcat(str,";");
lstrcat(str,path);
//}
}
//MessageBox(0,str,"",MB_OK);
ret=RegSetValueEx(phk, // handle of key to set value for
TEXT("Path"),// address of value to set
0, // reserved
REG_EXPAND_SZ, // flag for value type
(BYTE *)str, // address of value data
lstrlen(str)+1 // size of value data
);
if(ret!=ERROR_SUCCESS){
LocalFree(str);
RegCloseKey(phk);
return false;
}
//SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment");
DWORD dwReturnValue;
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
(LPARAM) "Environment", SMTO_ABORTIFHUNG,
2000, &dwReturnValue);
LocalFree(str);
RegCloseKey(phk);
return true;
}
PUBLIC_FUNCTION(AddToPath)
{
TCHAR *retstr = TEXT("error");
TCHAR* param = (TCHAR*)LocalAlloc(g_string_size*sizeof(TCHAR));
popstring(param);
if(param && ChangePath(param, true,true)) retstr = TEXT("ok");
LocalFree(param);
pushstring(retstr);
}
PUBLIC_FUNCTION_END
PUBLIC_FUNCTION(RemoveFromPath)
{
TCHAR *retstr = TEXT("error");
TCHAR* param = (TCHAR*)LocalAlloc(g_string_size*sizeof(TCHAR));
popstring(param);
if(param && ChangePath(param, false,true)) retstr = TEXT("ok");
LocalFree(param);
pushstring(retstr);
}
PUBLIC_FUNCTION_END
PUBLIC_FUNCTION(TestPath)
{
TCHAR *retstr = TEXT("error");
TCHAR* param = (TCHAR*)LocalAlloc(g_string_size*sizeof(TCHAR));
popstring(param);
TCHAR* param2 = (TCHAR*)LocalAlloc(g_string_size*sizeof(TCHAR));
popstring(param2);
MessageBox(0,param,param2,MB_OK);
LocalFree(param2);
LocalFree(param);
pushstring(retstr);
}
PUBLIC_FUNCTION_END
PUBLIC_FUNCTION(SetEnvVar)
{
TCHAR *retstr = TEXT("error");
TCHAR* param = (TCHAR*)LocalAlloc(g_string_size*sizeof(TCHAR));
if(popstring(param)){
LocalFree(param);
pushstring(retstr);
return;
}
TCHAR* param2 = (TCHAR*)LocalAlloc(g_string_size*sizeof(TCHAR));
if(popstring(param2)){
LocalFree(param2);
LocalFree(param);
pushstring(retstr);
return;
}
LocalFree(param2);
LocalFree(param);
pushstring(retstr);
}
PUBLIC_FUNCTION_END
//convert WideChar to ANSI
static char * convWcToLocal(wchar_t *wstr)
{
if (wstr == NULL) return NULL;
int len;
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
if (len == 0) return NULL;
//char *p = (char*)malloc(len + 1);
char *p = (char*)LocalAlloc(len + 1);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, p, len + 1, NULL, NULL);
return p;
}
static HWND id=0;
#define MAXLOCALE 1000
static char* locales[MAXLOCALE];
static int currId = 0;
BOOL _stdcall myEnumLocalesProc(_In_ LPTSTR lpLocaleString)
{
LCID lcid;
char* endptr = 0;
if (id == 0) return FALSE;
lcid = strtol(lpLocaleString, &endptr, 16);
WCHAR strNameBuffer[LOCALE_NAME_MAX_LENGTH];
DWORD error = ERROR_SUCCESS;
// Get the name for locale
if (LCIDToLocaleName(lcid, strNameBuffer, LOCALE_NAME_MAX_LENGTH, 0) == 0)//LOCALE_ALLOW_NEUTRAL_NAMES
{
// There was an error
error = GetLastError();
}
else
{
// Success, display the locale name we found, add string to array
char *p = convWcToLocal(strNameBuffer);//convert name to ANSI, convWcToLocal function allocates memory
if (p){
//SendMessage(id, CB_ADDSTRING, 0, (LPARAM)((LPSTR)p));
if (currId < MAXLOCALE){
locales[currId] = p;
currId++;
}
}
}
return TRUE;
}
//callback sort function
int pcompare(const void *arg1, const void *arg2)
{
/* Compare all of both strings: */
return _stricmp(*(char**)arg1, *(char**)arg2);
}
/*
setLocaleList function fill locale values to ComboBox
parameter - HWND of ComboBox ( returns NSD_CreateDropList)
Example:
${NSD_CreateDropList} 72u 30u 100u 12u ""
Pop $Locale
AddToPath::setLocaleList "$Locale"
*/
PUBLIC_FUNCTION(setLocaleList)
{
TCHAR *retstr = TEXT("error");
TCHAR* param = (TCHAR*)LocalAlloc(g_string_size*sizeof(TCHAR));
popstring(param);
//popint();
if (param){
id = (HWND)myatoi(param);
if (id > 0){
currId = 0;//current index in the locales array
EnumSystemLocalesA(myEnumLocalesProc, LCID_INSTALLED);//fill locales array
qsort((void *)locales, (size_t)currId, sizeof(char *), pcompare);// in the Windows Server 2008 the array is not sorted
for(int i = 0; i < currId; i++){
SendMessage(id, CB_ADDSTRING, 0, (LPARAM)((LPSTR)locales[i]));// fill ComboBox
LocalFree(locales[i]);//free memory for locale name
}
}
retstr = TEXT("ok");
}
LocalFree(param);
pushstring(retstr);
}
PUBLIC_FUNCTION_END
#ifdef _VC_NODEFAULTLIB
#define DllMain _DllMainCRTStartup
#endif
EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
g_hInstance = (HINSTANCE)hInst;
return TRUE;
}