1、本軟體自動啟動:
CString m_strfilepath;
char path[MAX_PATH]={0};
GetModuleFileName(NULL,path,MAX_PATH);
m_strfilepath=path;
CRegKey reg; //定義對象
reg.Create(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run");
reg.Create(reg.m_hKey,"new");
reg.SetValue(m_strfilepath,"Test.exe");
2、其他軟體自動啟動:
(1)添加自動啟動:
void CLimitUseCountDlg::OnStart()
{
// TODO: Add your control notification handler code here
CString m_strfilepath;
char path[MAX_PATH]={0};
char szValue[1024];
LPCTSTR vlauename="QQ.exe";
DWORD dCount=1024;
GetModuleFileName(NULL,path,MAX_PATH);
m_strfilepath=path;
CRegKey rk;
LPCTSTR lp="Software\\Microsoft\\Windows\\CurrentVersion\\Run\\QQ";
rk.Create(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run");
rk.Create(rk.m_hKey,"QQ");
rk.SetValue(m_strfilepath,"QQ.exe");
if(rk.Open(HKEY_LOCAL_MACHINE,lp)==ERROR_SUCCESS && rk.QueryValue(szValue,vlauename,&dCount)==ERROR_SUCCESS)//開啟註冊表
{
AfxMessageBox("開機自動啟動QQ.exe設定成功");
}
else
{
AfxMessageBox("開機自動啟動QQ.exe設定失敗!");
}
rk.Close();
}
(2)刪除自動啟動:
void CLimitUseCountDlg::OnDel()
{
CRegKey rk;
LPCTSTR subKey="QQ";
LPCTSTR KeyValue="QQ.exe";
LPCTSTR lp="Software\\Microsoft\\Windows\\CurrentVersion\\Run\\QQ\\";
// TODO: Add your control notification handler code here
//rk.DeleteSubKey(subKey);
//rk.RecurseDeleteKey(subKey);
if(rk.Open(HKEY_LOCAL_MACHINE,lp)==ERROR_SUCCESS && rk.DeleteValue(KeyValue)==ERROR_SUCCESS)//開啟註冊表
{
AfxMessageBox("開機自動啟動QQ.exe刪除成功");
}
else
{
AfxMessageBox("開機自動啟動QQ.exe刪除失敗");
}
rk.Close();
}