1. New item
For example, create an item Yantai under HKEY_LOCAL_MACHINE/software/, and then create a new item bandsoft under Yantai.
Hkey hsoftkey;
Hkey maid;
Hkey hcompanykey;
// Open HKEY_LOCAL_MACHINE/software
If (regopenkeyex (HKEY_LOCAL_MACHINE, _ T ("software"), 0, key_write | key_read,
& Hsoftkey) = error_success)
{
DWORD dw;
// Create and open HKEY_LOCAL_MACHINE/software/Yantai
If (regcreatekeyex (hsoftkey, _ T ("Yantai"), 0, reg_none,
Reg_option_non_volatile, key_write | key_read, null,
& Maid, & DW) = error_success)
{
// Create and enable HKEY_LOCAL_MACHINE/software/Yantai/bandsoft
If (regcreatekeyex (maid, _ T ("bandsoft"), 0, reg_none,
Reg_option_non_volatile, key_write | key_read, null,
& Hcompanykey, & DW) = error_success)
{
Regclosekey (hsoftkey );
Regclosekey (maid );
Regclosekey (hcompanykey );
}
}
}
---------------------------------------------------------------------------
2. Create a new string value under the item
For example, create a new string value named "productname" REG_SZ "under" bandsoft "; Data: fd900 System
The code above is as follows:
Hkey hcompanykey;
// Open HKEY_LOCAL_MACHINE/software/Yantai/bandsoft
If (regopenkeyex (HKEY_LOCAL_MACHINE, _ T ("software // Yantai // bandsoft"), 0, key_write | key_read, & hcompanykey) = error_success)
{
Cstring STR = "fd900 system ";
// Add 1 as the final null character of the string/0 reserved space
Lpbyte lpdata = new byte [Str. getlength () + 1];
For (INT I = 0; I <Str. getlength (); I ++)
{
* (Lpdata + I) = Str. getat (I );
}
DWORD cbdata = Str. getlength () + 1;
// New Value
If (regsetvalueex (hcompanykey, _ T ("productname"), 0, REG_SZ, lpdata, cbdata )! = Error_success)
MessageBox ("error ");
Delete [] lpdata;
}
Else
MessageBox ("error ");
Regclosekey (hcompanykey );
------------------------------------------------------------------
3. Create a value of the REG_DWORD type under the item
For example, to create a value of the DWORD type under item bandsoft: Number, type REG_DWORD, data: 2
Hkey maid;
Hkey hcompanykey;
DWORD dw;
If (error_success! = Regopenkeyex (HKEY_LOCAL_MACHINE, _ T ("software // Yantai"), 0, key_write, & maid ))
MessageBox ("error ");
// Open HKEY_LOCAL_MACHINE/software/Yantai/bandsoft. If the bandsoft item does not exist, create
If (error_success! = Regcreatekeyex (maid, "bandsoft", 0, reg_none, reg_option_non_volatile, key_all_access, null, & hcompanykey, & DW ))
MessageBox ("error ");
// Name of the value of the DWORD type created under item bandsoft: Number type REG_DWORD, data: 2
DWORD dwvalue = 2;
Char szname [] = "Number ";
If (error_success! = Regsetvalueex (hcompanykey, szname, 0, REG_DWORD, (lpbyte) & dwvalue, sizeof (dwvalue )))
MessageBox ("error ");
Regclosekey (maid );
Regclosekey (hcompanykey );
You can see from the Registry that the name of the bandsoft item is: Number, type REG_DWORD, data: 0x00000002 (2)
Bytes -----------------------------------------------------------------------------------------
5. string value under the query item
For example, query the value of the string productname under bandsoft.
Hkey hcompanykey;
// Open HKEY_LOCAL_MACHINE/software/Yantai/bandsoft
If (regopenkeyex (HKEY_LOCAL_MACHINE, _ T ("software // Yantai // bandsoft"), 0,
Key_write | key_read, & hcompanykey) = error_success)
{
Lpbyte lpdata = new byte [80];
DWORD cbdata = 80;
DWORD dwtype = REG_SZ;
If (regqueryvalueex (hcompanykey, "productname", 0, & dwtype, lpdata, & cbdata)
= Error_success)
{
// Convert lpdata to cstring type
Cstring strvalue;
Strvalue = cstring (lpdata );
// Strvalue is the value you want to query
MessageBox (strvalue); // output fd900 System
}
Else
MessageBox ("error ");
Delete [] lpdata;
}
Else
MessageBox ("error ");
Regclosekey (hcompanykey );
Bytes -------------------------------------------------------------------------------------
6. All key values under the enumerated items
For example, enumerate all key values in HKEY_LOCAL_MACHINE/software/Microsoft/Windows/CurrentVersion/run under the startup Item,
And display it with the list box control:
Clistbox m_list;
Hkey hautorunkey;
DWORD dwindex = 0;
Long lresult;
If (regopenkeyex (HKEY_LOCAL_MACHINE, _ T ("software // Microsoft // windows // CurrentVersion // Run "),
0, key_read, & hautorunkey) = error_success)
{
Char szname [100];
DWORD cbname = 100;
Char szvalue [100];
DWORD cbvalue = 100;
Cstring strtemp;
Lresult = regenumvalue (hautorunkey, dwindex, szname, & cbname, 0, null, (lpbyte) szvalue, & cbvalue );
While (lresult = error_success & lresult! = Error_no_more_items)
{
Strtemp. Format ("Name: % s value: % s", szname, szvalue );
M_list.addstring (strtemp );
Dwindex = dwindex + 1;
// Be sure to assign a new value to the cbname and cbvalue. Because the value has changed, this error is not found until one day.
Cbname = 100;
Cbvalue = 100;
Lresult = regenumvalue (hautorunkey, dwindex, szname, & cbname, 0, null, (lpbyte) szvalue, & cbvalue );
}
}
Else
{
MessageBox ("error ");
}
Regclosekey (hautorunkey );
---------------------------------------------------------------------------------
7. All subitems under the enumerated items
For example, enumerate all the sub-items under HKEY_LOCAL_MACHINE/software and display them with the list box control.
Clistbox m_list
Hkey;
DWORD dwindex = 0;
Long lresult;
If (regopenkeyex (HKEY_LOCAL_MACHINE, _ T ("software "),
0, key_read, & hkey) = error_success)
{
Char szkeyname [100];
DWORD cbname = 100;
Lresult = regenumkeyex (hkey, dwindex, szkeyname, & cbname, 0, null );
While (lresult = error_success & lresult! = Error_more_data)
{
M_list.addstring (szkeyname );
Dwindex = dwindex + 1;
// Be sure to assign a value to the cbname again because the value has changed; otherwise, the next regeumkeyex error occurs.
Cbname = 100;
Lresult = regenumkeyex (hkey, dwindex, szkeyname, & cbname, 0, null );
}
}
Else
{
MessageBox ("error ");
}
Regclosekey (hkey );
-------------------------------------------------------------------------------
8. Delete the key value under the item
For example, delete the startup Item HKEY_LOCAL_MACHINE/software/Microsoft/Windows/CurrentVersion/run
Jfproc
Hkey hautorunkey;
// Open HKEY_LOCAL_MACHINE/software/Microsoft/Windows/CurrentVersion/run
If (error_success = regopenkeyex (HKEY_LOCAL_MACHINE, _ T ("software/Microsoft // windows // CurrentVersion // Run "),
0, key_write, & hautorunkey ))
{
Char szvalue [] = "jfproc ";
If (regdeletevalue (hautorunkey, szvalue )! = Error_success)
MessageBox ("error ");
}
Else
MessageBox ("error ");
Regclosekey (hautorunkey );
-----------------------------------------------------------------
9. delete an item (only applicable when there is no subitem under this item)
For example, deleting bandsoft under HKEY_LOCAL_MACHINE/software/Yantai/
Hkey;
// Open HKEY_LOCAL_MACHINE/software/Yantai
If (error_success = regopenkeyex (HKEY_LOCAL_MACHINE, _ T ("software // Yantai "),
0, key_write, & hkey ))
{
Char szkey [] = "bandsoft ";
If (Regdeletekey(Hkey, szkey )! = Error_success)
MessageBox ("error ");
}
Else
MessageBox ("error ");
Regclosekey (hkey );
------------------------------------------------------------------------
10. delete items (this option also applies if there are subitems)
Programming ideology:
Delete all the sub-items under this item, and then delete this item from the bottom up.
ExploitationRegdeletekeyThe returned value is not error_success when you delete an item (the item has a subitem,
Shdeleteemptykey should be used for items without a key value. I don't think it is necessary to useRegdeletekeyYou can,
Because each entry in the registry must contain a default name that is null, the data type is REG_SZ, and the value is also the key value of an empty string.
Function of the custom delete item
Bool deletekey (hkey, char * szsubkey)
{
If (error_success =Regdeletekey(Hkey, szsubkey ))
Return true;
// Open an entry
Hkey hsubkey;
// Not only the key_write permission is required, but also the key_read permission is required when the subitem is enumerated. I have found this error for a long time.
If (regopenkeyex (hkey, szsubkey, 0, key_write | key_read, & hsubkey )! = Error_success)
Return false;
// Delete a subitem
DWORD dwindex = 0;
Long lresult;
Char szname [100];
DWORD cbname = 100;
Lresult = regenumkeyex (hsubkey, dwindex, szname, & cbname, 0, null );
While (lresult = error_success & lresult! = Error_more_data)
{
// Recursively delete key
Deletekey (hsubkey, szname );
// Be sure to assign a value to the cbname again because the value has changed; otherwise, the next regeumkeyex error occurs.
Cbname = 100;
// Do not add dwindex = dwindex + 1,
// After the first sub-item is deleted, the second sub-item is changed to the first sub-item, always keeping dwindex = 0,
// This error was made by myself. After debugging, I found the answer for half a day.
Lresult = regenumkeyex (hsubkey, dwindex, szname, & cbname, 0, null );
}
Regclosekey (hsubkey );
// After the sub-item under the item is deleted, the item can be deleted now.
Regdeletekey(Hkey, szsubkey );
Return true;
}
Example: For the Yantai item in HKEY_LOCAL_MACHINE/software/Yantai/bandsoft,
The Yantai item has the Sub-item bandsoft. Therefore, the preceding custom deletekey function is used to delete the Yantai item. The Code is as follows:
Hkey;
// Open HKEY_LOCAL_MACHINE/software
If (error_success = regopenkeyex (HKEY_LOCAL_MACHINE, _ T ("software "),
0, key_write, & hkey ))
{
If (! Deletekey (hkey, "Yantai "))
MessageBox ("error ");
}
Else
MessageBox ("error ");
Regclosekey (hkey );