目錄
?
一、前言
二、代碼
三、使用步驟
1.編譯生成exe文件、
2.以管理員身份運行代碼?
3.打開注冊表,驗證結果
一、前言
????????在Windows操作系統中,將exe文件的路徑添加到注冊表下,主要用于實現程序的開機自動運行功能。
? ? ? ? 注冊表路徑為:
Software\\Microsoft\\Windows\\CurrentVersion\\Run
二、代碼
?????????實現的代碼如下。代碼為一個函數,可以直接復制粘貼到運行程序中。注意添加頭文件。
#include <windows.h>
#include <io.h>
void AddToSystem()
{HKEY hKEY;char CurrentPath[MAX_PATH] = { 0 };char SysPath[MAX_PATH] = { 0 };long ret = 0;LPSTR FileNewName;LPSTR FileCurrentName;DWORD type = REG_SZ;DWORD size = MAX_PATH;LPCTSTR Rgspath = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; // regedit win + RGetSystemDirectory(SysPath, size);GetModuleFileName(NULL, CurrentPath, size);//Copy FileFileCurrentName = CurrentPath;FileNewName = lstrcat(SysPath,"\\Steal.exe"); // 文件名可以自己更改struct _finddata_t Steal;printf("retl = %d\n", ret);if (_findfirst(FileNewName, &Steal) != -1)return;//已經安裝!printf("ret2 = %d\n", ret); int ihow = MessageBox(0, "該程序只允許用于合法的用途!\n繼續運行該程序將使這臺機器處于被監控的狀態!\n如果您不想這樣,請按“取消”按鑰退?出。\n按下“是”按鈕該程序將被復 + \制到您的機器上,并隨系統啟動自動運行。\n按下“否”按鈕,程序只運行一次,不會在您的系統內留下任何東西。?","警告", MB_YESNOCANCEL | MB_ICONWARNING | MB_TOPMOST);if (ihow == IDCANCEL)exit(0);if (ihow == IDNO)return;//只運行一次//復制文件ret = CopyFile(FileCurrentName, FileNewName, TRUE);if (!ret){return;}//加入注冊表printf("ret = %d\n", ret);ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Rgspath, 0, KEY_WRITE, &hKEY);if (ret != ERROR_SUCCESS){RegCloseKey(hKEY);return;}//Set Keyret = RegSetValueEx(hKEY, "Steal", NULL, type, (const unsigned char*)FileNewName, size);if (ret != ERROR_SUCCESS){RegCloseKey(hKEY);return;}RegCloseKey(hKEY);
}
三、使用步驟
1.編譯生成exe文件、
? ? ? ? 將代碼復制粘貼到程序中,點擊生成。
?????????生成結果如圖:
2.以管理員身份運行代碼?
? ? ? ? 找到 debug 文件下的exe文件,右鍵管理員身份啟動。
3.打開注冊表,驗證結果
? ? ? ? win鍵 + R,輸入regedit,找到這個路徑下:Software\\Microsoft\\Windows\\CurrentVersion\\Run
,發現有exe文件生成,證明成功了!