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
#include <stdio.h>
#include <Windows.h>
#include <string.h>

int main() {
char regname[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
TCHAR path[] = "C:\\Windows\\System32\\door.exe";
HKEY hKey;
int ret;
ret = RegOpenKey(HKEY_LOCAL_MACHINE, regname, &hKey);
ret = RegSetValueEx(hKey, "door", 0, REG_EXPAND_SZ, (unsigned char *)path, strlen(path));
if (ret == ERROR_SUCCESS)
{
printf("success to write run key\n");
RegCloseKey(hKey);
}
else
{
printf("fail to set value. %d\n", ret);
system("pause");
return 0;
}

char modlepath[256];
char syspath[256];
GetModuleFileName(NULL, modlepath, 256);
//printf("%s\n", modlepath);
GetSystemDirectory(syspath, 256);
//printf("syspath:%s\n", syspath);

ret = CopyFile(modlepath, strcat(syspath, "\\door.exe"), 1);
if (true)
{
printf("%s has been copy to %s", modlepath, syspath);
}
else
{
printf("the file is exist");
}
//system("pause");
return 0;
}

效果

打赏专区