C++实现程序开机自启动


//写注册表,设置程序自启动
void setAutoStart()
{
    HKEY hKey;
    //找到系统的启动项
    LPCTSTR lpRun = (wchar_t*)QString("Software\\Microsoft\\Windows\\CurrentVersion\\Run").utf16();
    //打开启动项Key
    long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_WRITE, &hKey);
    QString appName = QApplication::applicationName();
    QString appPath = QApplication::applicationFilePath().replace("/", "\\");
    if (lRet == ERROR_SUCCESS)
    {
        DWORD dwRet = appPath.length() * 2;
        //添加一个子Key,并设置值
        lRet = RegSetValueEx(hKey, (WCHAR*)appName.utf16(), 0, REG_SZ,
                (BYTE*)appPath.utf16(), dwRet);
        //关闭注册表
        RegCloseKey(hKey);
        if (lRet != ERROR_SUCCESS)
        {
            qDebug("error");
        }
    }
}

你可能感兴趣的:(C++开发)