VC to operate the registry

Source: Internet
Author: User

Recently, you need to manipulate the registry in the program you write. Summarize some experience, and make a demo for later use, now take it out and share with you ...

For ease of use, some operations are written as functions to facilitate invocation, as shown in the following code:

I. Definition

HKEY hkey;   
Char content[256];      The contents of the queried registry key value   
DWORD DWTYPE=REG_SZ;        Define read data type   
DWORD dwlength=256;   
struct Hkey__*rootkey;      Registry primary Key Name   
TCHAR *subkey;          To open the address of the registry key   
TCHAR *keyname;         Want to set the name of the item   
TCHAR *valuename;       Want to set the name of the value   
lpbyte setcontent_s;        String type   
int setcontent_d[256];      DWORD type   
BYTE setcontent_b[256];     Binary type   

int showcontent (struct Hkey__*rerootkey,tchar *resubkey,tchar *revaluename);   
int setvalue_s (struct Hkey__*rerootkey,tchar *resubkey,tchar *revaluename,lpbyte resetcontent_s);   
int setvalue_d (struct Hkey__*rerootkey,tchar *resubkey,tchar *revaluename,int resetcontent_d[256]);   
int Setvalue_b (struct Hkey__*rerootkey,tchar *resubkey,tchar *revaluename,byte resetcontent_b[256]);   
int DeleteKey (struct Hkey__*rerootkey,tchar *resubkey,tchar *rekeyname);   
int DeleteValue (struct Hkey__*rerootkey,tchar *resubkey,tchar *revaluename);

Second, view functions

Showcontent (struct Hkey__*rerootkey,tchar *resubkey,tchar *revaluename)   
{   
    int i=0;    Operation Result: 0==succeed   
    if (RegOpenKeyEx (Rerootkey,resubkey,0,key_read,&hkey) ==error_success)   
    {   
        if ( RegQueryValueEx (Hkey,revaluename,null,&dwtype, (unsigned char *) content,&dwlength)!=error_success)   
        {   
            AfxMessageBox ("Error: Unable to inquire about registry information");   
            I=1;   
        }   
        RegCloseKey (hkey);   
    }   
    else
    {   
        AfxMessageBox ("Error: Unable to open related hkey");   
        I=1;   
    }   
    return i;   
}

Three, set string value function

setvalue_s (struct Hkey__*rerootkey,tchar *resubkey,tchar *revaluename,lpbyte resetcontent_s)   
{   
    int i=0;    Operation Result: 0==succeed   
    //int strlength;   
    Strlength=cstring (setcontent_s). GetLength ();   

    if (RegOpenKeyEx (Rerootkey,resubkey,0,key_write,&hkey) ==error_success)   
    {   
        if (RegSetValueEx HKey, Revaluename,null,reg_sz,resetcontent_s,cstring (setcontent_s). GetLength ())!=error_success)   
        {   
            AfxMessageBox ("Error: Unable to set relevant registry information");   
            I=1;   
        }   
        RegCloseKey (hkey);   
    }   
    else
    {   
        AfxMessageBox ("Error: Unable to inquire about registry information");   
        I=1;   
    }   
    return i;   
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.