Windows to set the boot mode (manual /C++ code form)

This section describes how to start related software in Windows

  • First of all, I will explain how to realize the principle of the relevant software under Windows. Because Windows itself has a registry mechanism, the so-called registry can be understood as the database of Windows. inwin+rThe inputregeditTo view

  • As shown in the figure, the registry is basically filled withkey-valueSave in the form of.
  • Then Windows when the system is opened, it will be the default to read the database (that is, the registry) related configuration information from the start of relevant software, so malicious rogue software can also modify the registry information to attack the system.
  • Therefore, if you set the boot automatically, in fact, just modify the corresponding value in the registry.
  • The change location is stored in the registry at:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunAdd the corresponding string value by right clicking on it.
  • The string that requires the software path to be started needs to be quoted.

C++ code to set the startup software

  • Once you understand how this works, all the C++ code starts to do is write to the registry
  • Here’s the code
bool WriteToRegedit(HKEY hKey_,std::wstring path_,std::wstring key_,std::wstring value_)
{
	HKEY hkey;
	::RegCreateKeyExW(hKey_, 
		path_.c_str(), 
		0.NULL, 
		REG_OPTION_NON_VOLATILE, 
		KEY_WRITE, 
		NULL, 
		&hkey, 
		NULL);
	//
	::RegSetValueExW(hkey, 
		key_.c_str(), 
		0, 
		REG_SZ, 
		reinterpret_cast<LPBYTE>(const_cast<wchar_t*>(value_.c_str())),
		sizeof(std::wstring::value_type) * (value_.size() + 1));
	::RegCloseKey(hkey);
	return true;
}

// Set the boot method
// @param:key_: Boot item name
// @param:value_: Path of the startup file
// return: false: the setting fails true: the setting succeeds
bool SetBootStartUp(std::wstring key_,std::wstring path_ )
{
	return WriteToRegedit(HKEY_CURRENT_USER,L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",key_,path_);
}
Copy the code

The code is relatively simple, that is, to write a string to the location of the corresponding registry, use method:

	std::wstring key_ = L"TestDemo";
	std::wstring path = L"\"SSSS\"";
	SetBootStartUp(key_,path);
Copy the code
  • I hope it’s helpful