实现软件自动启动代码
生活随笔
收集整理的這篇文章主要介紹了
实现软件自动启动代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
自動啟動代碼: void CServerApp::SetAutoRun() { HKEY hKey; char* szRegpath = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; long lRet; // 打開注冊表 lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegpath, 0, KEY_ALL_ACCESS, &hKey); if (lRet != ERROR_SUCCESS) { AfxMessageBox("打開自動啟動注冊表失敗!"); RegCloseKey(hKey); return; } // 得到當前文件的路徑 char szFilePath[MAX_PATH]; DWORD dwRet; dwRet = GetModuleFileName(NULL, szFilePath, MAX_PATH); if (dwRet == 0) { AfxMessageBox("得到當前文件路徑失敗!"); return; } // 將當前路徑增加到該注冊表中 lRet = RegSetValueEx(hKey, "AttendServer", 0, REG_SZ, (CONST BYTE*)&szFilePath, strlen(szFilePath) + 1); if (lRet != ERROR_SUCCESS) { AfxMessageBox("寫入自動啟動注冊表失敗!"); RegCloseKey(hKey); return; } RegCloseKey(hKey); } 取消自動運行代碼: void CServerApp::CancelAutoRun() { HKEY hKey; char* szRegpath = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; long lRet; // 打開注冊表 lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegpath, 0, KEY_ALL_ACCESS, &hKey); if (lRet != ERROR_SUCCESS) { AfxMessageBox("打開自動啟動注冊表失敗!"); RegCloseKey(hKey); return; } lRet = RegDeleteValue(hKey, "AttendServer"); if (lRet != ERROR_SUCCESS) { AfxMessageBox("刪除該軟件在自動啟動注冊表設置失敗!"); RegCloseKey(hKey); return; } RegCloseKey(hKey); }
總結
以上是生活随笔為你收集整理的实现软件自动启动代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CLR 4.0 有哪些新东西? -- 类
- 下一篇: linux用户和组帐户管理