首页 > 编程资源分享区 > C/C++源代码共享 > C++注册表操作大全(每贴更新10个)
2009
04-14

C++注册表操作大全(每贴更新10个)

1.打开键
LPCTSTR StrKey(%%1);
HKEY m_hKey=%%2;
if (ERROR_SUCCESS==::RegOpenKeyEx(m_hKey,StrKey,NULL,KEY_ALL_ACCESS,&m_hKey))
{
%%3
}


2.添加键
LPCTSTR StrKey(%%1);
HKEY hKey;
DWORD dwDisposition;
if (ERROR_SUCCESS==::RegCreateKeyEx(m_hKey, (LPCTSTR)StrKey,0,NULL,
REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisposition))
{
m_hKey=hKey;
if (dwDisposition==REG_CREATED_NEW_KEY)
{
%%2
}
else if (dwDisposition==REG_OPENED_EXISTING_KEY)
{
%%3
}
}


3.删除键
LPCTSTR StrKey(%%1);
HKEY SrcKey=m_hKey;
char KeyName[256];
int nRes=0;
if (OpenKey(SrcKey,StrKey))
{
nRes=FirstEnumKey(KeyName);
while (nRes)
{
DeleteKey(KeyName);
nRes=NextEnumKey(KeyName);
}
}
if (::RegDeleteKey(SrcKey,StrKey)==ERROR_SUCCESS)
{
%%2
}


4.枚举第一个键
BOOL CRegEdit::FirstEnumKey(char* Value)
{
DWORD dwSize=255;
m_EnumLoop=0;
if (ERROR_SUCCESS==::RegEnumKeyEx(m_hKey,m_EnumLoop,Value,&dwSize,NULL,NULL,NULL,NULL))
{
%%2
}


5.枚举下一个键
BOOL CRegEdit::NextEnumKey(char* Value)
{
DWORD dwSize=255;
m_EnumLoop++;
if (ERROR_SUCCESS==::RegEnumKeyEx(m_hKey,m_EnumLoop,Value,&dwSize,NULL,NULL,NULL,NULL))
{
%%2
}


6.获取DWORD值
LPCTSTR StrChildKey(%%1);
DWORD %%1;
DWORD dwSize=255,dwType=REG_DWORD;
if (ERROR_SUCCESS==::RegQueryValueEx(m_hKey,StrChildKey,0,&dwType,(BYTE *)(&%%2),&dwSize))
{
%%2
}


7.获取二进制值
LPCTSTR StrChildKey(%%1);
DWORD %%1;
DWORD dwSize=255,dwType=REG_BINARY;
if (ERROR_SUCCESS==::RegQueryValueEx(m_hKey,StrChildKey,0,&dwType,(BYTE *)(&%%2),&dwSize))
{
%%2
}


8.读取字符串值
LPCTSTR StrChildKey(%%1);
CString %%2;
DWORD dwSize=255,dwType=REG_SZ;
char String[256];
if (ERROR_SUCCESS==::RegQueryValueEx(m_hKey,StrChildKey,0,&dwType,(BYTE *)String,&dwSize))
%%2=CString(String);


9.写入字符串值
LPCTSTR StrChildKey(%%1);LPCTSTR Value;CString %%2;
if (ERROR_SUCCESS==::RegSetValueEx( m_hKey,(LPCTSTR)StrChildKey,0,REG_SZ,(BYTE *)(LPCSTR)Value,strlen(Value)+1) )
%%2=CString(Value);


10.写入二进制值
LPCTSTR StrChildKey(%%1);const char* %%2;
if (ERROR_SUCCESS==::RegSetValueEx( m_hKey,(LPCTSTR)StrChildKey,0,REG_BINARY,(BYTE *)%%2,strlen(Value)) )
{
%%3
}


 


C++注册表操作大全(每贴更新10个)》有 2 条评论

  1. hias_asia 说:

     

    11.写入DWORD值
    LPCTSTR StrChildKey(%%1);DWORD %%2;
    if (ERROR_SUCCESS==::RegSetValueEx( m_hKey,(LPCTSTR)StrChildKey,0,REG_DWORD,(BYTE *)&%%2,sizeof(Value)) )
    {
    %%3
    }

    12.设置DWORD值
    BOOL CRegEdit::SetDwordValue(HKEY Root, LPCTSTR StrKey, LPCTSTR StrChildKey, DWORD Value)
    m_hKey=m_RootKey=Root;
    if (CreateKey(StrKey))
    {
    if (WriteDword(StrChildKey,Value))
    {
    %%2
    }

    13.删除值
    if (ERROR_SUCCESS==RegDeleteValue(m_hKey,%%1))
    {
    %%2
    }

    14.关闭键
    if (m_hKey!=NULL)
    ::RegCloseKey(m_hKey);

    15.列出一个键下所有的子键
    LPTSTR subKey;
    DWORD numSubKey=0;
    DWORD i;
    DWORD sizeSubKey;
    TCHARsubKey[MAX_KEY_LENGTH];
    RegQueryInfoKey(m_hKey,NULL,NULL,NULL,&numSubKey,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
    if (numSubKey)
    {
    for (i=0; i<numSubKey; i++)
    {
    subKey[0]=’\0′;
    sizeSubKey=MAX_KEY_LENGTH;
    RegEnumKeyEx(m_hKey,i,subKey,&sizeSubKey,NULL,NULL,NULL,NULL);
    CString %%1=CString(subKey);
    }
    }

    16.列出一个键下所有的值
    //#define MAX_KEY_LENGTH 255
    //#define MAX_VALUE_NAME 16383
     TCHAR achKey[MAX_KEY_LENGTH];
     DWORD cbName;
     TCHAR achClass[MAX_PATH] = TEXT(“”);
     DWORD cchClassName = MAX_PATH;
     DWORD cSubKeys=0;
     DWORD cbMaxSubKey;
     DWORD cchMaxClass;
     DWORD cValues;
     DWORD cchMaxValue;
     DWORD cbMaxValueData;
     DWORD cbSecurityDescriptor;
     FILETIME ftLastWriteTime;
     DWORD i, retCode;
     TCHAR  achValue[MAX_VALUE_NAME];
     DWORD cchValue = MAX_VALUE_NAME;
     retCode = RegQueryInfoKey

    (m_hKey,achClass,&cchClassName,NULL,&cSubKeys,&cbMaxSubKey,&cchMaxClass,&cValues,&cchMaxValue,&cbMaxValueData,&cbSecurityDesc

    riptor,&ftLastWriteTime);
     if (cValues)
     {
      for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
      {
    cchValue = MAX_VALUE_NAME;
    achValue[0] = ‘\0′;
    retCode = RegEnumValue(m_hKey, i,achValue,&cchValue,NULL,NULL,NULL,NULL);
    if (retCode == ERROR_SUCCESS ){
     CString %%1=CString(achValue);
     %%2
    }
    }
    }

    17.列出一个键下所有的子键和值
    //#define MAX_KEY_LENGTH 255
    //#define MAX_VALUE_NAME 16383
     TCHAR achKey[MAX_KEY_LENGTH];
     DWORD cbName;
     TCHAR achClass[MAX_PATH] = TEXT(“”);
     DWORD cchClassName = MAX_PATH;
     DWORD cSubKeys=0;
     DWORD cbMaxSubKey;
     DWORD cchMaxClass;
     DWORD cValues;
     DWORD cchMaxValue;
     DWORD cbMaxValueData;
     DWORD cbSecurityDescriptor;
     FILETIME ftLastWriteTime;
     DWORD i, retCode;
     TCHAR  achValue[MAX_VALUE_NAME];
     DWORD cchValue = MAX_VALUE_NAME;
     retCode = RegQueryInfoKey

    (m_hKey,achClass,&cchClassName,NULL,&cSubKeys,&cbMaxSubKey,&cchMaxClass,&cValues,&cchMaxValue,&cbMaxValueData,&cbSecurityDesc

    riptor,&ftLastWriteTime);
     if (cSubKeys)
     {
      for (i=0; i<cSubKeys; i++)
      {
    cbName = MAX_KEY_LENGTH;
    retCode = RegEnumKeyEx(m_hKey,i,achKey,&cbName,NULL,NULL,NULL,&ftLastWriteTime);
    if (retCode == ERROR_SUCCESS) {
     CString %%1=CString(achKey);
     %%2
     }
     }
     }
     if (cValues)
     {
      for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
      {
    cchValue = MAX_VALUE_NAME;
    achValue[0] = ‘\0′;
    retCode = RegEnumValue(m_hKey, i,achValue,&cchValue,NULL,NULL,NULL,NULL);
    if (retCode == ERROR_SUCCESS ){
     CString %%3=CString(achValue);
     %%4
    }
    }
    }

    18.枚举所有的子键和值
    //#include <vector>
    using std::vector;
    struct RegStore{
    HKEY hKey;
    TCHAR rootKey[MAX_KEY_LENGTH];
    CString path;
    };
    vector<struct RegStore> cache;
    CString m_content,%%1;
    bool findFlag;
    DWORD i,sizeSubKey,numSubKey=0;
    struct RegStore rs;
    TCHAR subKey[MAX_KEY_LENGTH];
    rs.hKey=hKey;
    strcpy(rs.rootKey,rootKey);
    rs.path=path;
    cache.push_back(rs);
    for(int iCt=0;iCt<cache.size();iCt++)
    {
    rs=cache.at(iCt);
    RegOpenKeyEx(rs.hKey,rs.rootKey,0,KEY_READ,&hKey);
    if (RegQueryValueEx(hKey,m_content,NULL,NULL,NULL,NULL)==ERROR_SUCCESS)
    {
    findFlag=1;
    %%1=rs.path;
    %%2
    }
    else
    {
    RegQueryInfoKey(hKey,NULL,NULL,NULL,&numSubKey,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
    if (numSubKey)
    {
    for (i=0; i<numSubKey; i++)
    {
    subKey[0]=’\0′;
    sizeSubKey=MAX_KEY_LENGTH;
    RegEnumKeyEx(hKey,i,subKey,&sizeSubKey,NULL,NULL,NULL,NULL);
    rs.hKey=hKey;
    strcpy(rs.rootKey,rootKey);
    rs.path=path+subKey+”\\”;
    cache.push_back(rs);
    }
    }
    }
    RegCloseKey(hKey);
    }

  2. hiroki 说:

    是不是 很多人 都用MFC啊!!!

留下一个回复