关机和自启动.pdfVIP

  • 1
  • 0
  • 约1.16万字
  • 约 16页
  • 2023-05-20 发布于四川
  • 举报
VC 实现开机自启动 2008-12-10 14:39 很多监控软件要求软件能够在系统重新启动后不用用户去点击图标启动项 目,而是直接能够启动运行,方法是写注册表 Software\\Microsoft\\Windows\\CurrentVersion\\Run 。 参考程序可以见下: (查找程序目录的执行文件,存在则进行添加注册表操作) //实用代码一 int C***Dlg::CreateRun(void) { //添加以下代码 HKEY RegKey; CString sPath; GetModuleFileName(NU ,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PAT H); sPath.ReleaseBuffer(); int nPos; nPos=sPath.ReverseFind(\\); sPath=sPath.Left(nPos); CString lpszFile=sPath+[url=file://getip.exe%22;//]\\getip.exe; //[/url]这里加上你要查找的执行文件名称 CFileFind fFind; BOOL bSuccess; bSuccess=fFind.FindFile(lpszFile); fFind.Close(); if(bSuccess) { CString fullName; fullName=lpszFile; RegKey=NULL; RegOpenKey(HKEY_LOCAL_MACHINE,Software\\Microsoft\\Windows\\Curren tVersion\\Run,RegKey); RegSetValueEx(RegKey,getip,0,REG_SZ, (const unsigned char*)(LPCTSTR)fullName,fullName.GetLength());//这里 加上你需要在注册表中注册的内容 this-UpdateData(FALSE); } else { //theApp.SetMainSkin(); ::AfxMessageBox(没找到执行程序,自动运行失败); exit(0); } return 0; } //把上面的 getip (共2 处)替换成自己想启动程序的名字。 ================================================ 实用代码二: //写入注册表,开机自启动 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,并设置值 // 下面的getip是应用程序名字(不加后 缀.exe) lRet = RegSetValueEx(hKey, getip, 0, REG_SZ, (BYTE *)pFileName, dwRet); //关闭注册表 RegCloseKey(hKey); if(lRet != ERROR_SUCCESS) { AfxMessageBox(系统参数错误,不能随系统启动); } }

文档评论(0)

1亿VIP精品文档

相关文档