Our general procedure for the operation of the Registry is to open, read, and close, then first take a look at some of the most important functions of the introduction bar, the other more functions are to be accessed by the MSDN.
First of all, turn on and off function, before reading and writing the registry, you must first open the target's subkey, that is, to obtain an operation of the handle, and open this function is the most commonly used is RegCreateKeyEx, if there is no this key exists, then automatically create this subkey. LONG RegCreateKeyEx (hkey hkey,//parent key handle LPCTSTR Lpsubkey,//subkey name DWORD Reserve D,//typically 0 LPTSTR lpclass,//typically 0 DWORD dwoptions,//Option REG when creating subkeys SAM samdesired,//Open mode lpsecurity_attributes lpsecurityattributes,//Specify Inheritance Phkey Phkresult,
The handle to return the subkey Lpdword Lpdwdisposition//Open or create subkey information);
If the function call succeeds, it returns ERROR_SUCCESS which means that success returns 0 and the failure returns a value other than 0.
The samdesired parameter has multiple values, and the last lpdwdisposition parameter is used to return information with two values: Reg_created_new_key the specified key does not exist, then create. Reg_opened_existing_key the specified key exists, then open.
In addition, there are similar functions as follows: LONG RegOpenKeyEx (hkey hkey,//To open the handle of the primary key LPCTSTR Lpsubkey,//To open the subkey DWORD uloptions,//Reserved
Regsam samdesired,//safe Access Parameters Phkey Phkresult//Open key handle address); When compared to open functions, closing is more tolerantEasy, its function prototype is as follows: LONG RegCloseKey (hkey hkey); so just pass in a handle to close. Then, we look at the correlation function of Read and write, the function prototype reads as follows: LONG RegQueryValueEx (hkey hkey,///subkey handle LPCTSTR Lpvaluename,//Key value name Lpdword L
Preserved,//is 0 Lpdword lptype,//Return key value data type Lpbyte lpdata,//Return key value data Lpdword Lpcbdata//buffer size);
Returns ERROR_SUCCESS after the function call succeeds.
For example, using Regedit to find the presence of Key_local_machine Guesttools we can use the above API to get its values and properties.
int Getguesttoolsfilepath (char * filename, char * path) {LONG lret;
HKEY hkey;
DWORD Dwbuflen = rge_path_length;
Char Regpath[rge_path_length] = {0};
Char Toolspath[rge_path_length] = {0};
if (NULL = = path) {printf ("The path in Getguesttoolsfilepath is Error");
return-1; } if (Is64bitsystem ()) {if Regopenkeyexa (HKEY_LOCAL_MACHINE, Software\\wow6432node\\guest TOOLS), 0,key_query_
Value,&hkey)!= error_success) {printf ("# #Open the Reg get WhiteList Path error##");
return-1; }else{if (Regopenkeyexa (HKEY_LOCAL_MACHINE, "Software\\guest TOOLS", 0,key_query_value,&hkey)!=
CESS) {printf ("# #Open the Reg get WhiteList Path error##");
return-1;
} Lret = Regqueryvalueexa (hkey, "InstallPath", Null,null, (LPBYTE) regpath,&dwbuflen); if (Lret!= error_success) {//log_debug ("Failed to Get the Reg value!! ");
printf ("# #Failed to get" the Reg Value from:%s and Error:%lu, ToolsPath, GetLastError ());
return-1;
} regclosekey (HKEY); if (' i ' = = Regpath[0] | |
Rge_path_length <= strlen (Regpath)) {printf ("The Regpath is error!!");
return-1;
} strcpy (Path,regpath); if (NULL!= filename && (ifle_path_length-rge_path_length) > strlen (filename)) {STRC
at (Path,filename);
printf ("# #Get IP Config File path Success path:%s #", path);
return 0; }