VC++精简版

Posted on 2009-09-05 18:41 卡洛shll 阅读(997) 评论(0)  编辑 收藏 引用

有时候,我们需要在计算机启动的时候就启动某些程序,不要人干预。这里,提供一种让程序开机自动运行的方法。见下面代码:
BOOL CXXX::SetAutoRun(CString strPath)
{
CString str;
HKEY hRegKey;
BOOL bResult;
str=_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
if(RegOpenKey(HKEY_LOCAL_MACHINE, str, &hRegKey) != ERROR_SUCCESS)
bResult=FALSE;
else
{
_splitpath(strPath.GetBuffer(0),NULL,NULL,str.GetBufferSetLength(MAX_PATH+1),NULL);
strPath.ReleaseBuffer();
str.ReleaseBuffer();
if(::RegSetValueEx( hRegKey,str,0,REG_SZ,(CONST BYTE *)strPath.GetBuffer(0),strPath.GetLength() )!=ERROR_SUCCESS)
bResult=FALSE;
else
bResult=TRUE;
strPath.ReleaseBuffer();
}

return bResult;
}

CString CXXX::GetMyPath()
{
CString strPath;
GetModuleFileName(NULL,strPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
strPath.ReleaseBuffer();
return strPath;
}


//把上面的两个函数加入在程序初始化的地方
CString mypath;
mypath=GetMyPath();
SetAutoRun(mypath);


****************************************************************************

  • vc实现开机自动运行的代码程序

CString sPath; 
 int nPos;
 HKEY RegKey;
 GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
 sPath.ReleaseBuffer();
 nPos=sPath.ReverseFind('\\');
 sPath=sPath.Left(nPos);
 CString lpszFile=sPath+"\\双机切换软件.exe";
 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\\CurrentVersion\\Run",&RegKey);
  RegSetValueEx(RegKey,"KJ65N自动切换软件",0,REG_SZ,(const unsigned char*)(LPCTSTR)fullName,fullName.GetLength());
  RegCloseKey(RegKey);
  this->UpdateData(FALSE);

  return TRUE;
 }
 else
 {
  ::AfxMessageBox("没找到执行程序,自动运行失败");
  exit(0);
  
  return FALSE;
 }
 
 return TRUE;

  • vc实现删除开机自动运行的代码程序

 CString sPath; 
 int nPos;
 HKEY RegKey;
 GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
 sPath.ReleaseBuffer();
 nPos=sPath.ReverseFind('\\');
 sPath=sPath.Left(nPos);
 CString lpszFile=sPath+"\\双机切换软件.exe";
 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\\CurrentVersion\\Run",&RegKey);
  RegDeleteValue(RegKey,"KJ65N自动切换软件");
  RegCloseKey(RegKey);
  this->UpdateData(FALSE);
 }
 else
 {
  ::AfxMessageBox("没找到执行程序,自动运行失败");
  exit(0);
 }

  • vc实现开机自动运行的代码程序

CString sPath; 
 int nPos;
 HKEY RegKey;
 GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
 sPath.ReleaseBuffer();
 nPos=sPath.ReverseFind('\\');
 sPath=sPath.Left(nPos);
 CString lpszFile=sPath+"\\双机切换软件.exe";
 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\\CurrentVersion\\Run",&RegKey);
  RegSetValueEx(RegKey,"KJ65N自动切换软件",0,REG_SZ,(const unsigned char*)(LPCTSTR)fullName,fullName.GetLength());
  RegCloseKey(RegKey);
  this->UpdateData(FALSE);

  return TRUE;
 }
 else
 {
  ::AfxMessageBox("没找到执行程序,自动运行失败");
  exit(0);
  
  return FALSE;
 }
 
 return TRUE;

  • vc实现删除开机自动运行的代码程序

 CString sPath; 
 int nPos;
 HKEY RegKey;
 GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
 sPath.ReleaseBuffer();
 nPos=sPath.ReverseFind('\\');
 sPath=sPath.Left(nPos);
 CString lpszFile=sPath+"\\双机切换软件.exe";
 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\\CurrentVersion\\Run",&RegKey);
  RegDeleteValue(RegKey,"KJ65N自动切换软件");
  RegCloseKey(RegKey);
  this->UpdateData(FALSE);
 }
 else
 {
  ::AfxMessageBox("没找到执行程序,自动运行失败");
  exit(0);
 }



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理