VC:注册表实现软件自动启动(CRegKey 、GetModuleFileName)
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();
}