设置开机自动运行
来源:互联网 发布:大学宿舍网络怎么样 编辑:程序博客网 时间:2024/04/30 08:08
<span style="font-size:18px;"> //响应用户的设置void CautoRunWhenResetSystemDlg::OnBnClickedButton1(){ // TODO: 在此添加控件通知处理程序代码 static BOOL lb_Set=FALSE; SetAutoRun(!lb_Set); lb_Set=!lb_Set; if (lb_Set==TRUE) { ((CButton*)GetDlgItem(IDC_BUTTON1))->SetWindowText("取消开机自动运行"); } else { ((CButton*)GetDlgItem(IDC_BUTTON1))->SetWindowText("设置开机自动运行"); }} //设置注册表项void CautoRunWhenResetSystemDlg::SetAutoRun(BOOL bAutoRun){ BOOL bStart=bAutoRun; HKEY hKey; CString dir=_T("SoftWare\\Microsoft\\Windows"); if(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, dir, 0, KEY_WRITE, &hKey)==ERROR_SUCCESS) { HKEY hCurVer; if(::RegCreateKeyEx(hKey, _T("CurrentVersion"), 0, NULL, 0, KEY_WRITE, NULL, &hCurVer, NULL)==ERROR_SUCCESS) { HKEY hRun; if(::RegCreateKeyEx(hCurVer, _T("Run"), 0, NULL, 0, KEY_WRITE, NULL, &hRun, NULL)==ERROR_SUCCESS) { if(bStart) { TCHAR sPath[MAX_PATH]; ::GetModuleFileName(NULL,sPath,MAX_PATH); CString value=sPath; //value+=_T(" /b"); ::RegSetValueEx(hRun, _T("autoRunWhenResetSystem"), NULL, REG_SZ, (PBYTE)value.GetBuffer(0), (value.GetLength()+1)*2);//autoRunWhenResetSystem是应用程序的名字 AfxMessageBox("success to set autoRun!"); } else { ::RegDeleteValue(hRun, _T("autoRunWhenResetSystem")); AfxMessageBox("success to cancel autoRun!"); } ::RegCloseKey(hRun); } ::RegCloseKey(hCurVer); } ::RegCloseKey(hKey); } } //设置成功后重启电脑即可,此时会发现程序自动运行</span>