//開機啟動 int CMainWnd::CreateRun() { //添加以下代碼 HKEY hKey; char pFileName[MAX_PATH] = {0}; //得到程序自身的全路徑 DWORD dwRet = GetModuleFileNameW(NULL, (LPWCH)pFileName, MAX_PATH); //找到系統的啟動項 LPCTSTR lpRun = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); //打開啟動項Key long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey); if(lRet== ERROR_SUCCESS) { //添加注冊 RegSetValueEx(hKey, _T("UilibDemo"), 0,REG_SZ,(const BYTE*)(LPCSTR)pFileName, MAX_PATH); RegCloseKey(hKey); } return 0; } //取消開機啟動 int CMainWnd::DeleteRun() { //添加以下代碼 HKEY hKey; char pFileName[MAX_PATH] = {0}; //得到程序自身的全路徑 DWORD dwRet = GetModuleFileNameW(NULL, (LPWCH)pFileName, MAX_PATH); //找到系統的啟動項 LPCTSTR lpRun = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); //打開啟動項Key long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey); if(lRet== ERROR_SUCCESS) { //刪除注冊 RegDeleteValue(hKey,_T("UilibDemo")); RegCloseKey(hKey); } return 0; }
可通過命令:regedit 查看注冊表中的數據