开机启动代码MFC实现

BOOL CAutoRunDlg::OnInitDialog()里添加

HKEY h_key = HKEY_LOCAL_MACHINE;
CString c_substr = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY h_result;
CString subkey ,str;
subkey = "MySoftMyWay";
unsigned long i,j;
CButton* m_button = (CButton *)GetDlgItem(IDC_CHECK1);
if (RegOpenKey(h_key,c_substr,&h_result)==ERROR_SUCCESS)
if (RegQueryValueEx(h_result,subkey,NULL,&j,(unsigned char *)str.GetBuffer(str.GetLength()),&i)==ERROR_SUCCESS)
{
m_button->SetCheck(1);
} 
else
{
m_button->SetCheck(0);
}
RegCloseKey(h_result);
OnOk按钮
void CAutoRunDlg::OnOK() 
{
// TODO: Add extra validation here
CButton* m_button =(CButton *) GetDlgItem(IDC_CHECK1);
HKEY h_key = HKEY_LOCAL_MACHINE;
HKEY h_result;
CString m_SubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
RegOpenKey(h_key,m_SubKey,&h_result);
CString m_Appname('0',MAX_PATH) ;

if (m_button->GetCheck()) //设置自动运行
{
GetModuleFileName(NULL,(char*)m_Appname.GetBuffer(m_Appname.GetLength()),MAX_PATH);
if (RegSetValueEx(h_result,"MySoftMyWay",0,REG_SZ,(unsigned char*)m_Appname.GetBuffer(m_Appname.GetLength()),m_Appname.GetLength())==ERROR_SUCCESS)
{
MessageBox("设置成功.","提示");
}
else
MessageBox("设置失败.","提示");
}
else //取消自动运行
{
if (RegDeleteValue(h_result,"MySoftMyWay")==ERROR_SUCCESS)
MessageBox("设置成功.","提示");
else
MessageBox("设置失败.","提示");
}
RegCloseKey(h_result);
}

转帖: http://hi.baidu.com/blueapple_c/blog/item/a26a52dc0b432cd38c102904.html

=============================================================================

#define   KEY_AUTO_RUN	 _T( "Software\\Microsoft\\Windows\\CurrentVersion\\Run ") 
#define   KEY_PROGRAM	 _T( "BQQ ") 
                  HKEY   RegEntry; 
int   open   =   RegOpenKeyEx(HKEY_LOCAL_MACHINE,   KEY_AUTO_RUN,   0,   KEY_SET_VALUE,   &RegEntry); 
if((p=((CButton*)GetDlgItem(IDC_AUTORUN))-> GetCheck())==1) 
{ 
autoRun=   1; 
TCHAR   fileName[MAX_BUFFER_SIZE+1]; 
GetModuleFileName(GetModuleHandle(AfxGetApp()-> m_pszAppName),   fileName,   MAX_BUFFER_SIZE   ); 
int   setval   =   RegSetValueEx(RegEntry,   KEY_PROGRAM,   0,   REG_SZ,   (LPBYTE)fileName,   strlen(fileName))   ; 
} 
else	//   delete   key 
RegDeleteValue(RegEntry,   KEY_PROGRAM); 
RegCloseKey(RegEntry);

转帖: http://topic.csdn.net/t/20020427/16/682149.html

==========================================================================

CString   str; //添加注册表路径 
//WCHAR*     CurrentPath=(WCHAR*)malloc(sizeof(char)*100);//程序当前路径
WCHAR*     CurrentPath= new WCHAR[sizeof(WCHAR)*100];//程序当前路径
HKEY   hRegKey;   //注册key
BOOL   bResult;   //打开注册表返回值
str=_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");//注册表   
GetModuleFileName(NULL,CurrentPath,MAX_PATH); //获得运用程序路径
if(bResult=RegOpenKey(HKEY_LOCAL_MACHINE,   str,   &hRegKey)   !=   ERROR_SUCCESS)//打开注册表     
{   
   //free(CurrentPath); //打开不成功就释放内存并返回
   delete[] CurrentPath;
   return;   
}   
if(bResult=::RegSetValueEx(hRegKey,_T("StarBySelf"),0,REG_SZ,   
   (const   unsigned   char   *)CurrentPath,   
   MAX_PATH)!=   ERROR_SUCCESS)   //打开成功写信息到注册表
{   //写入失败
   RegCloseKey(hRegKey);   //关闭注册表key
//   free(CurrentPath);   //释放内存资源
   delete[] CurrentPath;
   return;   //返回
}   
//成功后执行释放内存
RegCloseKey(hRegKey);   
// free(CurrentPath);
delete[] CurrentPath;

转帖: http://hi.baidu.com/fox_message/blog/item/ee8ec7d1e5ae6285a1ec9c07.html

你可能感兴趣的:(windows,Microsoft,validation,delete,mfc,Path)