首页 > 代码库 > 设置Qt程序在Windows开机后自动运行
设置Qt程序在Windows开机后自动运行
(转自:http://blog.csdn.net/weiren2006/article/details/7443362)
让软件在系统启动时自动运行,可通过修改注册表实现。
在\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run中添加需启动的程序路径,代码如下:
voidautoStart(boolflag)
{
//写入注册表,开机自启动
HKEYhKey;
//找到系统的启动项
LPCTSTRlpRun=(wchar_t*)QString("Software\\Microsoft\\Windows\\CurrentVersion\\Run").utf16();
//打开启动项Key
longlRet=RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpRun,0,KEY_WRITE,&hKey);
if(lRet==ERROR_SUCCESS)
{
DWORDdwRet=QApplication::applicationFilePath().length()*2;
//添加一个子Key,并设置值
if(flag==true)
{
lRet=RegSetValueEx(hKey,(WCHAR*)QString("test").utf16(),0,REG_SZ,
(BYTE*)QApplication::applicationFilePath().utf16(),dwRet);
}
else
{
lRet=RegSetValueEx(hKey,(WCHAR*)QString("test").utf16(),0,REG_SZ,
(BYTE*)"",dwRet);
}
//关闭注册表
RegCloseKey(hKey);
if(lRet!=ERROR_SUCCESS)
{
qDebug("error");
}
}
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。