MFC应用程序开机自启动

时间:2020-12-03 23:36:33

void CGestureRecognitionDlg::AutoStart(BOOL IsAutoStart){HKEY hKey;CString strRegPath = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");//找到系统的启动项  if (IsAutoStart){if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) //打开启动项       {TCHAR szModule[MAX_PATH];GetModuleFileName(NULL, szModule, MAX_PATH);                 //得到本程序自身的全路径  RegSetValueEx(hKey, _T("EasyLiu-GestureRecognition"), 0, REG_SZ, (LPBYTE)szModule, (lstrlen(szModule) + 1)*sizeof(TCHAR));//添加一个子Key,并设置值,"Demo"是应用程序名字(不加后缀.exe)  RegCloseKey(hKey); //关闭注册表  }else{AfxMessageBox(_T("系统参数错误,不能随系统启动"));}}else{if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS){RegDeleteValue(hKey, _T("EasyLiu-GestureRecognition"));RegCloseKey(hKey);}}}