Windows自啓動技術-註冊表

Windows自啓動技術-註冊表

註冊表自啓動

Windows的Run和RunOnce註冊表項可以讓用戶登陸系統時自動啓動一些程序。

其中涉及到的註冊表項如下:

  • HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

  • HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce

  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

    在這四種項中添加的自啓動程序的規則不一樣:

  • HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run表示任何賬戶每一次登陸到Windows系統都會自動啓動在這個項下面註冊的程序

  • HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce表示任何賬戶下一次登陸到Windows系統會自動啓動在這個項下面註冊的程序,以後就不會自啓了

  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run表示當前賬戶每一次登陸到Windows系統都會自動啓動在這個項下面註冊的程序

  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce表示當前賬戶下一次登陸到Windows系統會自動啓動在這個項下面註冊的程序,以後就不會自啓了
    一般來說
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run項下注冊自啓動項,不使用HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run註冊表原因是因爲修改該註冊表的值需要管理員權限。

API 功能
RegCreateKey 創建一個KEY,並返回相應的HKEY
RegOpenKey 打開註冊表,得到一個HKEY,用來作爲下面這些函數的第一個參數
RegOpenKeyEx 同RegOpenKey類似,一般很少用,增加了一個訪問控制類型參數
RegSetValue 設置一個HKEY的默認值
RegSetValueEx 設置一個HKEY除默認值以外其它的值
RegQueryValue 獲取一個HKEY的默認值
RegQueryValueEx 獲取一個HKEY除默認值以外其它的值
RegDeleteKey 刪除一個KEY,此KEY不能包含子KEY
SHDeleteKey 刪除一個KEY以及所有子KEY
RegDeleteValue 刪除KEY裏面的值
RegCloseKey 關閉註冊表

編碼實現

// auto_exection_reg.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//

#include <iostream>
#include <Windows.h>


int main()
{
	HKEY   hKey;
	//const char *filename = "C:\\Users\\Public\\hello.exe";
	BYTE value[256] = "C:\\Users\\Public\\hello.exe";
	
	if (ERROR_SUCCESS != ::RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey))
	{
		MessageBox(0, "error", "error", MB_OK);
	}
	
	/*if (ERROR_SUCCESS != RegSetValueEx(hKey, L"hello", 0, REG_SZ, (const byte*)filename, (1 + ::strlen(filename))))
	{
		MessageBox(0, L"error", L"error", MB_OK);
	}*/
	if (ERROR_SUCCESS != RegSetValueEx(hKey, "hello", 0, REG_SZ, value, 256))
	{
		MessageBox(0, "error", "error", MB_OK);
	}

	RegCloseKey(hKey);
	MessageBox(0, "OK", "RegSetValueEx", MB_OK);
//	else
//	{
//		MessageBox(0, L"RegSetValueEx", L"error", MB_OK);
//	}
//
//
//}
//	else
//	{
//	MessageBox(0, L"error", L"error", MB_OK);
//	return false;
//	}

	
	system("pause");
	return 0;
}

//LSTATUS RegOpenKeyExA(
//	[in]           HKEY   hKey,
//	[in, optional] LPCSTR lpSubKey,
//	[in]           DWORD  ulOptions,
//	[in]           REGSAM samDesired,
//	[out]          PHKEY  phkResult
//);

重啓系統

在實際中可能會遇到註冊表重定向問題,HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run寫入值後會發現值會被重定向到HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Run

在RegOpenKeyEx的時候可以將函數設置爲KEY_WOW64_64KEY

LONG lReturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\***", 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hKey);

完整代碼

#include "stdafx.h"
#include "AutoRun_Reg.h"


void ShowError(char *lpszText)
{
	char szErr[MAX_PATH] = { 0 };
	::wsprintf(szErr, "%s Error!\nError Code Is:%d\n", lpszText, ::GetLastError());
#ifdef _DEBUG
	::MessageBox(NULL, szErr, "ERROR", MB_OK | MB_ICONERROR);
#endif
}


BOOL Reg_CurrentUser(char *lpszFileName, char *lpszValueName)
{
	// 默認權限
	HKEY hKey;
	// 打開註冊表鍵
	if (ERROR_SUCCESS != ::RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey))
	{
		ShowError("RegOpenKeyEx");
		return FALSE;
	}
	// 修改註冊表值,實現開機自啓
	if (ERROR_SUCCESS != ::RegSetValueEx(hKey, lpszValueName, 0, REG_SZ, (BYTE *)lpszFileName, (1 + ::lstrlen(lpszFileName))))
	{
		::RegCloseKey(hKey);
		ShowError("RegSetValueEx");
		return FALSE;
	}
	// 關閉註冊表鍵
	::RegCloseKey(hKey);

	return TRUE;
}


BOOL Reg_LocalMachine(char *lpszFileName, char *lpszValueName)
{
	// 管理員權限
	HKEY hKey;
	// 打開註冊表鍵
	if (ERROR_SUCCESS != ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey))
	{
		ShowError("RegOpenKeyEx");
		return FALSE;
	}
	// 修改註冊表值,實現開機自啓
	if (ERROR_SUCCESS != ::RegSetValueEx(hKey, lpszValueName, 0, REG_SZ, (BYTE *)lpszFileName, (1 + ::lstrlen(lpszFileName))))
	{
		::RegCloseKey(hKey);
		ShowError("RegSetValueEx");
		return FALSE;
	}
	// 關閉註冊表鍵
	::RegCloseKey(hKey);

	return TRUE;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章