VC程序开机时自动运行

作者在 2010-04-17 14:18:56 发布以下内容

//开机自动运行
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);

默认分类 | 阅读 1220 次
文章评论,共0条
游客请输入验证码
浏览44552次
文章分类