起動C++実現

2778 ワード

//         
void autostart()
{
    HKEY hKey;
    QString strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

    //1、          
    if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.toStdString().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"
)); } } // void SGISRestartDlg::cancelAutoStart() { HKEY hKey; QString strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; //1、 if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.toStdString().c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) { //2、 RegDeleteValue(hKey, "GISRestart"); //3、 RegCloseKey(hKey); } }