首页 > 编程语言 > 详细

C++ 开机自动启动

时间:2019-08-19 11:51:34      阅读:144      评论:0      收藏:0      [点我收藏+]

针对不需要管理员权限的程序

转载:https://blog.csdn.net/u010601662/article/details/72851812/

// 程序开机自动启动
void autostart()
{
    HKEY hKey;
    string strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

    //1、找到系统的启动项  
    if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) ///打开启动项       
    {
        //2、得到本程序自身的全路径
        TCHAR strExeFullDir[MAX_PATH];
        GetModuleFileName(NULL, strExeFullDir, MAX_PATH);

        //3、判断注册表项是否已经存在
        TCHAR strDir[MAX_PATH] = {};
        DWORD nLength = MAX_PATH;
        long result = RegGetValue(hKey, nullptr, "GISRestart", RRF_RT_REG_SZ, 0, strDir, &nLength);

        //4、已经存在
        if (result != ERROR_SUCCESS || _tcscmp(strExeFullDir, strDir) != 0)
        {
            //5、添加一个子Key,并设置值,"GISRestart"是应用程序名字(不加后缀.exe) 
            RegSetValueEx(hKey, "GISRestart", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1)*sizeof(TCHAR));

            //6、关闭注册表
            RegCloseKey(hKey);
        }
    }
    else
    {
        QMessageBox::warning(0, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("\n系统参数错误,不能随系统启动n"));
    }
}

// 取消开机自动启动
void cancelAutoStart()
{
    HKEY hKey;
    string strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

    //1、找到系统的启动项  
    if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
    {
        //2、删除值
        RegDeleteValue(hKey, "GISRestart");

        //3、关闭注册表
        RegCloseKey(hKey);
    }
}

 

C++ 开机自动启动

原文:https://www.cnblogs.com/Toya/p/11375747.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!