注册表的相关知识参考之前一篇 http://www.cnblogs.com/LexMoon/p/C_muma.html
现在的任务是将一个程序写入注册表中开机自启动的对应位置里面。
现在有一个简单的 hello.exe (路径 : D:hello.exe ),功能是在命令行显示输出 "hello world" 。
1 #include<iostream> 2 #include<stdio.h> 3 #include<tchar.h> 4 #include<windows.h> 5 6 using namespace std ; 7 8 int main(){ 9 HKEY hkey ; 10 long ret = RegOpenKeyEx(HKEY_CURRENT_USER,_T("SOFTWARE\Microsoft\Windows\CurrentVersion\Run"),0,KEY_QUERY_VALUE,&hkey) ; 11 if(ret==ERROR_SUCCESS){ 12 ret = RegCreateKey(HKEY_CURRENT_USER,_T("SOFTWARE\Microsoft\Windows\CurrentVersion\Run"),&hkey); 13 if(ret==ERROR_SUCCESS){ 14 ret = RegSetValueEx(hkey,_T("hello"),0,REG_SZ,(const BYTE*)("d:\hello.exe"),13) ; 15 if(ret==ERROR_SUCCESS){ 16 cout << "OK!!!" <<endl ; 17 } 18 } 19 } 20 return 0 ; 21 }
已经添加入开机自启动的位置。