-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathautorun.c
72 lines (58 loc) · 1.5 KB
/
autorun.c
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
#include <string.h>
#include <windows.h>
#include <winreg.h>
#include "autorun.h"
#include "main.h"
#define RUN_KEY "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
#define VALUE_NAME "ClockTray"
#define PATH_MAX_SIZE 256
char loaded_exe_path[PATH_MAX_SIZE];
unsigned int loaded_exe_path_size = 0;
int autorun_init()
{
loaded_exe_path_size = GetModuleFileName(NULL, loaded_exe_path,
PATH_MAX_SIZE) + 1;
return GetLastError() == ERROR_SUCCESS;
}
int autorun_get(int * existsp)
{
HKEY hk;
char path[PATH_MAX_SIZE];
DWORD size = PATH_MAX_SIZE;
DWORD type;
int failed;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, RUN_KEY, 0, KEY_READ, &hk))
return 0;
failed = RegQueryValueEx(hk, VALUE_NAME, NULL, &type, (BYTE *) path,
&size);
RegFlushKey(hk);
RegCloseKey(hk);
if (existsp)
*existsp = !failed;
return !failed &&
type == REG_SZ &&
size == loaded_exe_path_size &&
path[size - 1] == '\0' &&
strcmp(path, loaded_exe_path) == 0;
}
void autorun_set(int enable)
{
HKEY hk;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, RUN_KEY, 0, KEY_SET_VALUE, &hk))
return;
if (enable) {
RegSetValueEx(hk, VALUE_NAME, 0, REG_SZ, (BYTE *) loaded_exe_path,
loaded_exe_path_size);
} else {
RegDeleteValue(hk, VALUE_NAME);
}
RegFlushKey(hk);
RegCloseKey(hk);
}
void autorun_fix()
{
int exists;
int match = autorun_get(&exists);
if (exists && !match)
autorun_set(1);
}