Create a dialog box-based project in Visual C ++ 6.0 or 5.0, and set two command buttons: "query user information" and "Modify user information ", used to query and modify the user name and company name in the registry. It must be noted that the user information is located in the location of // HKEY_LOCAL_MACHINE // software // Microsoft // windows // CurrentVersion // In the system registry, the key value registeredowner and registeredorganization indicate the user name and company name respectively.
// Read the Registry
Void cregistryview: onbutton1 ()
{
// Todo: add your control notification handler code here
Hkey;
Lpctstr Path = "software // Microsoft // MS setup (ACME) // User Info ";
Long ret =: regopenkeyex (HKEY_CURRENT_USER, path, 0, key_read, & hkey );
If (Ret! = Error_success) // If the hkey cannot be opened, terminate the execution of the program
{
MessageBox ("error: the related hkey cannot be opened during query! ");
Return;
}
Unsigned char TMP [256] = "/0 ";
DWORD type = REG_SZ;
DWORD size = 80;
Ret =: regqueryvalueex (hkey, "defname", null, & type, TMP, & size );
If (Ret! = Error_success)
{
MessageBox ("error: Unable to query information about the registry! ");
Return;
}
Cstring tmpstring = cstring (TMP );
Afxmessagebox (tmpstring );
: Regclosekey (hkey );
}
// Write to the Registry
Void cregistryview: onbutton2 ()
{
// Todo: add your control notification handler code here
Unsigned char TMP [256];
Sprintf (char *) TMP, "% s", "hello ");
Cstring tmpstring = TMP;
Hkey;
DWORD type = REG_SZ;
DWORD size = tmpstring. getlength () + 1;
Lpctstr Path = "software // Microsoft // MS setup (ACME) // User Info ";
Long ret =: regopenkeyex (HKEY_CURRENT_USER, path, 0, key_write, & hkey );
If (Ret! = Error_success)
{
MessageBox ("error: failed to open the relevant hkey! ");
Return;
}
Ret =: regsetvalueex (hkey, "defname", null, type, TMP, size );
If (Ret! = Error_success)
{
MessageBox ("error: Unable to modify registry information! ");
Return;
}
: Regclosekey (hkey );
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/sinomac/archive/2007/02/23/1512967.aspx