c++ write registry mode allows the program to boot automatically

  • 2020-05-30 20:47:44
  • OfStack

Open registry: win+R , the input regedit Click ok

You will see 5 root directories, the boot - up information is written under HKEY_LOCAL_MACHINE, the specific directory is SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run
How do I write the startup information to the registry? It's actually quite simple, step 1, open the registry, step 2, write the registry, step 3, close the handle to the key

To open the registry, use the RegOpenKeyEx function


    
LONGRegOpenKeyEx(
  HKEYhKey, //  The name of the primary key that needs to be opened 
  LPCTSTRlpSubKey, //  The name of the child key that needs to be opened 
  DWORDulOptions, //  Reserve, set as 0
  REGSAM samDesired, //  Security access tags, or permissions 
  PHKEYphkResult //  Gets the handle to the key to be opened 
)

The first parameter, we're going to pass in
HKEY_LOCAL_MACHINE

The second parameter, we're going to pass in
SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run

The third parameter, we're going to pass in
0

The fourth parameter is a little bit naughty. Many instances pass KEY_ALL_ACCESS, but in 64-bit operating systems, it is better to pass KEY_ALL_ACCESS|, KEY_WOW64_64KEY. Otherwise, after writing the registry, the written registration information will not be displayed

For the fifth parameter, pass in a reference to HKEY

Write the registry using RegSetValueEx
function


LONG RegSetValueEx(
  HKEY hKey,// RegOpenKeyEx The end of the function passed in 1 Is the handle to the open key 
  LPCTSTR lpValueName, //  Key the name of the 
  DWORD Reserved, //  The incoming NULL
  DWORD dwType, //  The incoming REG_SZ
  CONST BYTE *lpData, //  The full address of the program to start 
  DWORD cbData// lpData The size of the 
);

Paste the code, win8 system vs2012


void test()
{
	
	LPCTSTR lpSubKey = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; 
	
	HKEY hKey;
	REGSAM flag = KEY_WOW64_64KEY; 
	DWORD dwDisposition = REG_OPENED_EXISTING_KEY; 
	LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS|flag,&hKey);
	//LONG lRet = ::RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, NULL, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition); 
	if ( ERROR_SUCCESS != lRet) 
  { 
    return; 
  } 
	
	TCHAR *pchrName = L"D:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQ.exe";
	lRet = ::RegSetValueEx(hKey, TEXT("TEST"), NULL, REG_SZ, (LPBYTE)pchrName, wcslen(pchrName)*sizeof(TCHAR)+1); // Set the registry key  
  if ( ERROR_SUCCESS != lRet) 
  { 
    return; 
  } 
 
  ::RegCloseKey(hKey); // with RegCreateKeyEx Matching to write  
	
};

One more point to note is that under the win8 system, you need to obtain administrator privileges to properly run the above code, otherwise you cannot open the registry

The specific methods are as follows:

VS2012, at the project properties - configuration properties - linker - manifest file -UAC execution level

Where is set to requireAdministrator (/level='requireAdministrator')

Rerun VS2012 as an administrator


Related articles: