- //开机启动函数
- void CCommWizardDlg::OnBnClickedCheckStart()
- {
- CString str; //添加注册表路径
- WCHAR* CurrentPath= new WCHAR[sizeof(WCHAR)*100];//程序当前路径
- HKEY hRegKey; //注册key
- BOOL bResult; //打开注册表返回值
- str=_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");//注册表
- GetModuleFileName(NULL,(LPSTR)CurrentPath,MAX_PATH); //获得运用程序路径
- if ( BST_CHECKED == IsDlgButtonChecked( IDC_CHECK_START ) )
- {
- // 勾选
- if(bResult=RegOpenKey(HKEY_LOCAL_MACHINE, str, &hRegKey) != ERROR_SUCCESS)//打开注册表
- {
- //free(CurrentPath); //打开不成功就释放内存并返回
- delete[] CurrentPath;
- return;
- }
- if(bResult=::RegSetValueEx(hRegKey,_T("StarBySelf"),0,REG_SZ,
- (const unsigned char *)CurrentPath,
- MAX_PATH)!= ERROR_SUCCESS) //打开成功写信息到注册表
- { //写入失败
- RegCloseKey(hRegKey); //关闭注册表key
- // free(CurrentPath); //释放内存资源
- delete[] CurrentPath;
- return; //返回
- }
- else
- {
- //写入成功后执行释放内存
- ifChecked = true;
- RegCloseKey(hRegKey);
- // free(CurrentPath);
- delete[] CurrentPath;
- }
- }
- else
- {
- if(bResult=RegOpenKey(HKEY_LOCAL_MACHINE, str, &hRegKey) != ERROR_SUCCESS)//打开注册表
- {
- //free(CurrentPath); //打开不成功就释放内存并返回
- delete[] CurrentPath;
- return;
- }
- if(!RegDeleteValue(hRegKey,_T("StarBySelf")))
- {
- // MessageBox(_T("删除注册表成功!"));
- ifChecked = false;
- RegCloseKey(hRegKey);
- delete[] CurrentPath;
- }
- else
- {
- MessageBox(_T("注册表删除失败!"));
- RegCloseKey(hRegKey);
- delete[] CurrentPath;
- }
- }
- }
MFC 开机启动
最新推荐文章于 2023-11-17 22:44:04 发布