How To Enable Automatic startup and startup of a program
When we write our own program in the window, we can also make our own program run automatically when the computer starts up. This is mainly to modify the Registry Information in windows, some information about all programs automatically started upon startup is stored in a folder in the registry. We only need to write our program information in a specific folder.
For the registry, you can press win + r on the keyboard and press regedit in the serial port to press enter to view the Registry (which can be understood as a big tree that records information in the system ), click the folder on the left and click in the following order: Software \ Microsoft \ Windows \ CurrentVersion \ Run. in this folder, information about the programs that are randomly started is stored. For example, when we use a computer optimization software for computer optimization, it will detect the items that need to be optimized, that is, some software will be started without being started, modify some information in this file.
First, we will explain the meanings of the functions used:
I
RegOpenKeyEx ()
Function Description: open a specified registry key.
Prototype
LONG RegOpenKeyEx ( HKEY hKey, // name of the primary key to be opened Lptstr lpSubKey, // name of the subkey to be opened DWORD ulOptions, // reserved, set to 0 REGSAM samDesired, // security access tag, that is, permission PHKEY phkResult // obtain the handle of the key to be opened. ) |
Parameters
Parameters:
HKey
The input parameter that identifies the handle of the Registry Key currently opened by RegCreateKeyEx or RegOpenKeyEx, or the following pre-defined handle
HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
LpSubKey
Input parameter, which points to a string used to save the name of the Registry to be opened. If this parameter is null or a pointer to an empty string, the function opens a key defined by the hKey. In this case, this function does not close the handle opened by the port wall.
UlOptions
Input parameter, reserved, set to 0
SamDesired
Input parameter. Indicates the permission to open the Registry. If the security descriptor of this parameter does not allow the current process to access the registry, the function returns a failure. In this program, we use the write parameter: KEY_WRITE (0x20006 ).
PhkResult
An output parameter pointing to a variable that is used to save the handle that opens the registry key. If the returned handle is no longer used, call RegCloseKey to close it.
Return Value:
ERROR_SUCCESS indicates that the function is successfully executed, and a non-zero value indicates that the function fails to be executed. To obtain the error description, call the FormatMessage function and input the FORMAT_MESSAGE_FROM_SYSTEM parameter.
II:
GetModuleFileName (NULL, pFileName, MAX_PATH );
The function prototype obtains the complete path of the file of the module loaded by the current process. The module must be loaded by the current process .)
1 2 3 4 5 |
DWORDGetModuleFileName ( HMODULE hModule, LPTSTR lpFilename, DWORD nSize ); |
HMODULE hModule: load the handle of a program instance. If this parameter is NULL, the function returns the full path of the current application.
LpFileName: the pointer to the memory block where you store the returned name. It is an output parameter.
DWORD nSize: maximum value loaded to the buffer lpFileName.
Note: If you want to obtain the full path of a running EXE or DLL, you can write the code in this way.
GetModuleFileNameEx (hProcess, hInst, lpFile, MAX_PATH); // pay attention to the next buffer.
III:
RegSetValueEx (): You can call the RegSetValueEx function when the registry key you want to set is not the default value, that is, the data and type of the name value, this function sets the data and type of the specified value under the registry key.
LONG RegSetValueEx (
HKEY hKey,
LPCTSTR lpValueName,
DWORD Reserved,
DWORD dwType,
Const byte * lpData,
DWORD cbData
);
HKey: the handle of an opened item.
LpValueName: pointer to a string that contains the name of the value to be set. If the value that owns the value does not exist in the specified registry key, this function adds it to this item.
Reserved: Reserved value, must be 0
DwType: Specifies the data type to be stored.
LpData: points to a buffer that contains the data to be stored for the specified value name.
CbData: Specifies the size of data pointed to by the lpData parameter, in bytes.
Run the following code to enable the program to start automatically.
Create a dialog box program, put a button on the dialog box, and write the following code in its message response function:
Void CSetAutoRunDlg: OnBnClickedButton1 () {// TODO, the long pointer type is const char * type LPCTSTR lpRun = "Software \ Microsoft \ Windows \ CurrentVersion \ Run"; // open the startup Item Key long lRet = RegOpenKeyEx (HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, & hKey); if (lRet = ERROR_SUCCESS) // open successfully {char pFileName [MAX_PATH] = {0 }; // define the storage path of the array // obtain the full path of the program. DWORD dwRet = GetModuleFileNam E (NULL, pFileName, MAX_PATH); // Add a sub-Key and set the value. // The following "test" is the application name (without suffix .exe) lRet = RegSetValueEx (hKey, "setsetorun", 0, REG_SZ, (BYTE *) pFileName, dwRet); // disable registry RegCloseKey (hKey); if (lRet! = ERROR_SUCCESS) {MessageBox ("system parameter error, startup cannot be completed");} else {MessageBox ("Boot started successfully ");} // isrun = 1 ;}}
After you click Run, the "started successfully" dialog box appears. After the computer is restarted, the software starts up (when the program is running, some anti-virus software may issue a Registry Modification warning, that is, our program is modifying the Registry)