Windows 注册表操作

经常操作注册表,然后得到一份操作注册表函数实现。这里备份下。

#ifndef _REGEDIT_H
#define _REGEDIT_H

int RegRead_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,TCHAR *Content, DWORD nLength);
int RegRead_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE *Content, DWORD nLength);
int RegSetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S);
int RegSetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S);
int RegSetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256]);
int RegDeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName);
int RegDeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName);


#endif
#include "StdAfx.h"
#include "regedit.h"

int RegRead_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,TCHAR *Content, DWORD nLength)
{
    HKEY hKey;
    int i=0;    //操作结果:0==succeed
    DWORD dwType = REG_SZ;
    DWORD dwLength = nLength;
    if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_READ,&hKey)==ERROR_SUCCESS)
    {
        if(RegQueryValueEx(hKey,ReValueName,NULL,&dwType,(LPBYTE)Content,&dwLength)!=ERROR_SUCCESS)
        {
            i=1;
        }
        RegCloseKey(hKey);
    }
    else
    {
        i=1;
    }
    return i;
}

int RegRead_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE *Content, DWORD nLength)
{
    HKEY hKey;
    int i=0;    //操作结果:0==succeed
    DWORD dwType = REG_DWORD;
    DWORD dwLength = nLength;
    if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_READ,&hKey)==ERROR_SUCCESS)
    {
        if(RegQueryValueEx(hKey,ReValueName,NULL,&dwType,(LPBYTE)Content,&dwLength)!=ERROR_SUCCESS)
        {
            i=1;
        }
        RegCloseKey(hKey);
    }
    else
    {
        i=1;
    }
    return i;
}

int RegSetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S)
{
    int i=0;    //操作结果:0==succeed
    HKEY hKey;
    if(RegCreateKeyEx(ReRootKey,ReSubKey,0,NULL,NULL,KEY_ALL_ACCESS,NULL,&hKey,NULL)==ERROR_SUCCESS)
    {
        int nLen = _tcslen((TCHAR*)ReSetContent_S);
        if(RegSetValueEx(hKey,ReValueName,NULL,REG_SZ,ReSetContent_S,nLen*sizeof(TCHAR)+1)!=ERROR_SUCCESS)
        {
            i=1;
        }
        RegCloseKey(hKey);
    }
    else
    {
        i=1;
    }
    return i;
}

int RegSetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_D)
{
    int i=0;    //操作结果:0==succeed
    HKEY hKey;
    if(RegCreateKeyEx(ReRootKey,ReSubKey,0,NULL,NULL,KEY_ALL_ACCESS,NULL,&hKey,NULL)==ERROR_SUCCESS)
    {
        if(RegSetValueEx(hKey,ReValueName,NULL,REG_DWORD,(LPBYTE)ReSetContent_D,sizeof(DWORD))!=ERROR_SUCCESS)
        {
            i=1;
        }
        RegCloseKey(hKey);
    }
    else
    {
        i=1;
    }
    return i;
}

int RegSetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256])
{
    int i=0;    //操作结果:0==succeed
    HKEY hKey;
    if(RegCreateKeyEx(ReRootKey,ReSubKey,0,NULL,NULL,KEY_ALL_ACCESS,NULL,&hKey,NULL)==ERROR_SUCCESS)
    {
        if(RegSetValueEx(hKey,ReValueName,NULL,REG_BINARY,(LPBYTE)ReSetContent_B,sizeof(DWORD))!=ERROR_SUCCESS)
        {
            i=1;
        }
        RegCloseKey(hKey);
    }
    else
    {
        i=1;
    }
    return i;
}

int RegDeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName)
{
    int i=0;    //操作结果:0==succeed
    HKEY hKey;
    if((RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey))==ERROR_SUCCESS)
    {
        if((RegDeleteKey(hKey,ReKeyName))!=ERROR_SUCCESS)
        {
            i=1;
        }
        RegCloseKey(hKey);
    }
    else
    {
        i=1;
    }
    return i;
}

int RegDeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName)
{
    int i=0;    //操作结果:0==succeed
    HKEY hKey;
    if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
    {
        if(RegDeleteValue(hKey,ReValueName)!=ERROR_SUCCESS)
        {
            i=1;
        }
        RegCloseKey(hKey);
    }
    else
    {
        i=1;
    }
    return i;
}
posted on 2014-09-25 17:50  好好单调  阅读(247)  评论(0编辑  收藏  举报