将程序复制到系统目录,设置开机自启动

程序比较简单,用到了C语言获得自身路径和系统路径,修改注册表项等,某些函数不理解可以查看MSDN

 1 #include<stdio.h>
 2 #include<windows.h>
 3 
 4 char *GetFilename(char *p)  //得到一个路径的纯文件名
 5 {
 6     int x=strlen(p);
 7     char ch='\\';
 8     char *q=strrchr(p,ch);
 9     return q;
10 }
11 
12 
13 int main()
14 {
15     char *filepath;
16     char modlepath[256];
17     char syspath[256];
18 
19     //打开酷狗应用
20     filepath="C:\\Program^ Files\\KuGou\\KGMusic\\KuGou.exe";  
21     system(filepath);
22 
23     //将程序移动到系统目录下
24 
25     GetModuleFileName(0,modlepath,256); //获得自身路径
26     GetSystemDirectory(syspath,256);  //得到系统路径
27 
28     int ret=CopyFile(modlepath,strcat(syspath,GetFilename(modlepath)),1);//复制,CopyFile的第二个参数是目标文件名
29     if(ret)
30     {
31         printf("%s has been copyed to sys dir %s\n",modlepath,syspath);
32     }
33     else
34     {
35         printf("%s is exists",modlepath);
36     }
37 
38     //程序添加开机自启动
39     char regname[]="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
40     HKEY hKey;
41     ret=RegOpenKey(HKEY_LOCAL_MACHINE,regname,&hKey);       //打开注册表键
42     ret=RegSetValueEx(hKey,"MyProm",0,REG_EXPAND_SZ,(unsigned char*)strcat(syspath,GetFilename(modlepath)),25);  //设置键值
43 
44     if(ret==0)
45     {
46         printf("succes to write run key.\n");
47         RegCloseKey(hKey);
48     }
49     else
50     {
51         printf("failed to open regedit.%d\n",ret);
52         return 0;
53     }
54     
55     return 0;
56 }

附-参考:

http://zhidao.baidu.com/link?url=9jIBTUUs-AvKkD2QZAxsdnxYAfTjnJxMfbqF3_lXd-g9NJJyyW6x-wI31kyWlRkRuT9TM4arYlJXmsc5VehPYa

posted @ 2016-01-01 04:14  廖凡  阅读(610)  评论(0编辑  收藏  举报