注册表遇到的坑

注册表是个非常牛逼的东西。驱动与用户程序通信都用的这个玩意。但是呢有个地方要注意就是你的健值太大api给的大小往往不靠谱。要自己手动加大。

IResult = RegQueryInfoKey(IKey, NULL, NULL, 0, NULL, NULL, NULL, &numValues,
        &maxLenValueName, &maxLenValueData, NULL, NULL);

注册表遇到的坑
还有不要太小气直接搞个一万都行。值其实不大但是tmd设置1024都不行。
注册表遇到的坑
注册表遇到的坑

std::map <std::string, std::string> Fun_Enum_Process_By_Regedit()
{
    std::map <std::string, std::string> _Map;
    HKEY     hKey;                  
    long     IResult;   
    DWORD    numValues, maxLenValueName, maxLenValueData, lpType, lenValue;
    DWORD    lenData;                                                  
    HKEY     RootKey = HKEY_LOCAL_MACHINE;      
    LPCTSTR  lpSubKey = L"SYSTEM\\CurrentControlSet\\Services\\haidragon";
    HKEY     IKey = 0;
    wchar_t  *ValueName = new   wchar_t[1024];                          
    BYTE  *lpData = new  BYTE[1024];
    ZeroMemory(ValueName, sizeof(ValueName));
    ZeroMemory(lpData,    sizeof(lpData));
    IResult = RegOpenKeyEx(RootKey, lpSubKey, 0, KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE | KEY_READ, &IKey);
    if (ERROR_SUCCESS != IResult)
    { 
        goto end;
    }
    IResult = RegQueryInfoKey(IKey, NULL, NULL, 0, NULL, NULL, NULL, &numValues,
        &maxLenValueName, &maxLenValueData, NULL, NULL);
    if (ERROR_SUCCESS != IResult)
    { 
        goto end;
    }
    for (int i = 0; ERROR_SUCCESS == IResult, i < (int)numValues; i++)
    {
        lenValue = 1024;
        lenData = 4096; 
        IResult = RegEnumValueW(IKey, i, ValueName, &lenValue, 0, &lpType, lpData, &lenData); 
        //lpData是俩字节的
        _Map[wstring2string(std::wstring(ValueName))] = wstring2string(std::wstring((wchar_t*)lpData));
        if (ERROR_SUCCESS == RegDeleteValue(IKey, ValueName)) {
            wprintf(L" 删除键值%s 成功!!!!\n", ValueName);
        }
        ZeroMemory(ValueName, sizeof(ValueName));
        ZeroMemory(lpData, sizeof(lpData));
    } 
end: 
    delete  ValueName;
    delete   lpData;
    return _Map;
}

猜你喜欢

转载自blog.51cto.com/haidragon/2345419