天天看點

關于用VS實作開機自啟動功能(win7/winXp)

void OnBnClickedCheckAutorun(bool _bAutoStart)

{

    char cFilePath[255] = {0};     

    HMODULE hFilePath = GetModuleHandle(NULL); 

    //得到目前執行檔案的全路徑

    GetModuleFileName(hFilePath, cFilePath, sizeof(cFilePath));

    if (_bAutoStart) 

    {

        //設定開機啟動

        //打開系統資料庫

        HKEY hKey;  

        iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,

        "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey );

        if (iRet != ERROR_SUCCESS)

        {

          return ;

        }

        //寫入系統資料庫 

        iRet = RegSetValueEx(hKey, "檔案名", 0, REG_SZ, (BYTE*)(LPCSTR)cFilePath, sizeof       (cFilePath));

        //關閉系統資料庫   

       RegCloseKey(hKey);                

   }

       else

       {

           //取消設定開機啟動

           //打開系統資料庫

           HKEY hKey;  

           iRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,

          "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey );

          if (iRet != ERROR_SUCCESS)

          {

              return ;

          }

           //删除系統資料庫中内容 

           iRet = RegDeleteValue(hKey, "檔案名");

           if (iRet != ERROR_SUCCESS)

           {

               return ;

           }

            //關閉系統資料庫

            RegCloseKey(hKey);      

        } 

}

本文轉自 我不會抽煙 51CTO部落格,原文連結:http://blog.51cto.com/zhouhongyu1989/1179627,如需轉載請自行聯系原作者