VC写入注册表,使程序开机启动

时间:2022-09-04 23:33:18


 if(autoopendlp == 1)
 {
  HKEY hKey;
  LPCTSTR lpRun = "Software//Microsoft//Windows//CurrentVersion//Run";
  long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey);
  if(lRet == ERROR_SUCCESS)
  {
   char pFileName[MAX_PATH] = {0};
   DWORD dwRet = GetModuleFileName(GetModuleHandle(AfxGetApp()-> m_pszAppName), pFileName, MAX_PATH);
   lRet = RegSetValueEx(hKey, "ctrl", 0, REG_SZ, (LPBYTE)pFileName, strlen(pFileName));

   MessageBox(pFileName);
   if(lRet != ERROR_SUCCESS)
   {
    AfxMessageBox("系统参数错误,不能随系统启动");
   }
  }else
   RegDeleteValue(hKey,"ctrl");
  RegCloseKey(hKey);
 }