MFC通过注册表 实现程序开机自动运行
2015-09-15 15:14
537 查看
1.创建一个成员函数:
void CAutoRunByRegistryDlg::SetAutoRun(bool bAutoRun)
{
HKEY hKey;
CString strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";//find the system's startup
if (bAutoRun)
{
if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) //open running item
{
TCHAR szModule[_MAX_PATH];
GetModuleFileName(NULL, szModule, _MAX_PATH);//Get oneself program's name
RegSetValueEx(hKey,"AutoRunByRegistry", 0, REG_SZ, (const BYTE*)(LPCSTR)szModule, strlen(szModule));
//add a child Key,and set a value="AutoRunByRegistry",it is a program name (remove .exe),of course KeyVaule can set anyone.
RegCloseKey(hKey); //close Registry
}
else
{
AfxMessageBox("the system parameters error,AutoRun fail!");
}
}
else
{
if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
RegDeleteValue (hKey,"AutoRunByRegistry"); //Del KeyVaule="AutoRunByRegistry", so program AutoRunByRegistry.exe will can AutoRun.
RegCloseKey(hKey);
}
}
}
2.增加函数响应:
以下代码,可以放到初始化函数OnInitDialog(),或则按钮响应函数都可以。
SetAutoRun(true); //add registrykey value
//SetAutoRun(false); //remove registrykey value
3.效果可以查看注册表:在运行中输入:regedt32.exe
然后找到路径为:SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run,就可以看到注册的程序。
以上程序实现了开机启动运行自身的程序,如果想开机启动其他的应用程序,只需要在RegSetValueEx函数中把程序的完整路径名字设置OK,就可以了。
void CAutoRunByRegistryDlg::SetAutoRun(bool bAutoRun)
{
HKEY hKey;
CString strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";//find the system's startup
if (bAutoRun)
{
if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) //open running item
{
TCHAR szModule[_MAX_PATH];
GetModuleFileName(NULL, szModule, _MAX_PATH);//Get oneself program's name
RegSetValueEx(hKey,"AutoRunByRegistry", 0, REG_SZ, (const BYTE*)(LPCSTR)szModule, strlen(szModule));
//add a child Key,and set a value="AutoRunByRegistry",it is a program name (remove .exe),of course KeyVaule can set anyone.
RegCloseKey(hKey); //close Registry
}
else
{
AfxMessageBox("the system parameters error,AutoRun fail!");
}
}
else
{
if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
RegDeleteValue (hKey,"AutoRunByRegistry"); //Del KeyVaule="AutoRunByRegistry", so program AutoRunByRegistry.exe will can AutoRun.
RegCloseKey(hKey);
}
}
}
2.增加函数响应:
以下代码,可以放到初始化函数OnInitDialog(),或则按钮响应函数都可以。
SetAutoRun(true); //add registrykey value
//SetAutoRun(false); //remove registrykey value
3.效果可以查看注册表:在运行中输入:regedt32.exe
然后找到路径为:SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run,就可以看到注册的程序。
以上程序实现了开机启动运行自身的程序,如果想开机启动其他的应用程序,只需要在RegSetValueEx函数中把程序的完整路径名字设置OK,就可以了。
相关文章推荐
- The project cannot be built until build path errors are resolved错误。
- Hive入门指南
- Hive入门指南
- c#绘制表格
- 使用secureCRT的时候,如何让vim编辑器显示彩色代码
- hibernate关联关系笔记
- 排序占坑(待更)
- JStorm-介绍
- 新唐N32926调试--wifi模块调试
- final和static 的 区别是什么?
- Mysql一些变态的sql写法
- WPF中画虚线
- activiti入门学习demo
- hdu-5442(后缀数组,2015网络赛长春赛区)
- 设计模式之原型模式
- css-边框透明
- Qt 事件过滤器问题
- File not found
- Python--os模块
- LR中的一些技巧(首次修改密码、同一账号重复登录、验证码处理)