c++ method to make the program boot automatically

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

When we write our own program under window, we can also make our program run automatically when the computer is turned on. This is mainly to modify the registry information in windows. All the information of the program that is turned on and started automatically is placed in a folder in the registry. We just need to write our program information in a specific folder.

About the registry, we can press win+r on the keyboard at the same time, enter regedit into the running serial port, and you can see the registry (which can be understood as a big tree, recording the information in the system). We click the folder on the left, and the order is: Software\ Microsoft\ Windows\ CurrentVersion\ CurrentVersion\ Run. In this folder, we can store the information of randomly launched programs. For example, when we use a computer optimization software for computer optimization, it will detect the project that needs to be booted up and optimized, that is, some software is started without booting up, and what is modified is 1 of the information in this file.

First of all, let's explain the meaning of the functions used:

RegOpenKeyEx ()

Function description: opens a specified registry key
The prototype

LONG RegOpenKeyEx(
HKEY hKey, // name of the primary key to be opened
LPCTSTR lpSubKey, // name of the subkey to be opened
DWORD ulOptions, // reserved, set to 0
REGSAM samDesired, // security access flag, which is permission
PHKEY phkResult // gets the handle to the open key
)
Parameters
Parameters:
hKey
Enter a handle that identifies the registry key currently opened by RegCreateKeyEx or RegOpenKeyEx, or one of the following predefined handles
HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS


lpSubKey
Parameter to a string that holds the name of the registry to be opened. If the argument is null, or a pointer to an empty string, the function will open a key defined by hKey, in which case the function will not close the bulwark open handle.

ulOptions
Input, leave, set to 0

samDesired
The parameters. Identifies the right to open the registry and returns a failure if the security descriptor for this parameter does not allow the current process to access the registry. In this program we use the write parameter KEY_WRITE (0x20006).

phkResult
A pointer to a variable that holds the handle to the open registry key. If the returned handle is no longer used, RegCloseKey is called to close it.

The return value:
ERROR_SUCCESS indicates that the function executed successfully, and a non-zero value indicates that the function failed. To get the error description, call the FormatMessage function and pass in the FORMAT_MESSAGE_FROM_SYSTEM parameter.

2:
GetModuleFileName(NULL,pFileName, MAX_PATH);
The function prototype gets the full path to the file of the module loaded by the current process, which must be loaded by the current process.

DWORDGetModuleFileName(
HMODULE hModule,
LPTSTR lpFilename,
DWORD nSize
);
HMODULE hModule: handle to load an instance of a program. If the parameter is NULL, the function returns the full path of the current application.
lpFileName: is a pointer to the memory block where you store the returned name. It is an output parameter.
DWORD nSize: load into the maximum value of buffer lpFileName.

Note: if you want to get the full path of a running EXE or DLL, you can write the code like this
GetModuleFileNameEx (hProcess hInst, lpFile MAX_PATH); // just pay attention to the buffer.

3:

RegSetValueEx(): the RegSetValueEx function, which sets the data and type of the specified value under the registry key, can be called when the desired registry key is not the default value, that is, the data and type with the named value.

LONG RegSetValueEx(
HKEY hKey,
LPCTSTR lpValueName,
DWORD Reserved,
DWORD dwType,
CONST BYTE *lpData,
DWORD cbData
);
hKey: handle to an open item
lpValueName: pointer to a string containing the name of the value to be set. If the value with the value name does not exist in the specified registry key, this function adds it to the item.
Reserved: reserved value, must be forced to 0
dwType: specifies the data type to be stored
lpData: points to a buffer containing the data you want to store for the specified value name.
cbData: specifies the size, in bytes, of the data pointed to by the lpData parameter.

The following code is used to start the program automatically.
We set up a dialog program, put an button on the dialog, and then write the following code in its message response function:


void CSetAutoRunDlg::OnBnClickedButton1()
{
	// TODO:  Add control notification handler code here 
	
	HKEY hKey;  // Can be understood as a handle to manipulate the registry 
  
	// Find the startup entry for the system    , the long pointer type is const char *  type 
  LPCTSTR lpRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";  
  
  // Open startup Key  
  long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey);  
  
	if(lRet == ERROR_SUCCESS)  // Open the success 
  {  
    char pFileName[MAX_PATH] = {0};  // Define the array store path 
    // Get the full path of the program itself   
    DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PATH);  
    // add 1 child Key, And set the values  //  The following "test" Is the application name (without suffixes) .exe )  
    lRet = RegSetValueEx(hKey, "SetAutoRun", 0, REG_SZ, (BYTE *)pFileName, dwRet);  
    // Close the registry   
    RegCloseKey(hKey);  
    if(lRet != ERROR_SUCCESS)  
    {  
      MessageBox(" System parameter error , Unable to complete boot setup ");  
    }  
    else 
    { 
      MessageBox(" Boot on successfully ");  
    } 
    // isrun = 1; 
  }  
}

After clicking "run", the "startup success" dialog box will appear. After restarting the computer, the software will boot up and start (when running the program, some anti-virus software may issue a warning of registry modification, which is our program modifying the registry).


Related articles: