将自身写入自启动

来源:互联网 发布:python中文版下载 编辑:程序博客网 时间:2024/06/05 09:20

void WriteStart()
{
 //写入注册表,开机自启动
 HKEY hKey;
 //找到系统的启动项
 LPCTSTR lpRun = "Software//Microsoft//Windows//CurrentVersion//Run";
 //打开启动项Key
 long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey);
 if(lRet == ERROR_SUCCESS)
 {
  char pFileName[MAX_PATH] = {0};
  //得到程序自身的全路径
  DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PATH);
  //添加一个子Key,并设置值
  lRet = RegSetValueEx(hKey, "Shutdown", 0, REG_SZ, (BYTE *)pFileName, dwRet);
  //关闭注册表
  RegCloseKey(hKey);
  if(lRet != ERROR_SUCCESS)
  {
   AfxMessageBox("系统参数错误,不能随系统启动");
  }
 }
}

原创粉丝点击