C ++ RegQueryValueEx()获取DWORD但不起作用

时间:2012-02-23 03:21:14

标签: c++ winapi registry

我想得到一个注册表值,并且获得REG_SZ正在工作,但不是DWORD,我想也许我不知道如何将DWORD转换为多字节形式。我的代码如下,有人可以建议我的代码有什么问题,谢谢:

void GetRegistryValue(char* getValue, int maxSize)
{
    LPCTSTR keypath = TEXT("Software\\....");
    HKEY hKey;
    DWORD dwType=0, dwSize=maxSize;     
    LONG lResult; 

    lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keypath, 0, KEY_ALL_ACCESS, &hKey);

    if(lResult == ERROR_SUCCESS)
    {
        LPCTSTR server = TEXT("Server");
        TCHAR *wideValue = (TCHAR *) malloc(dwSize * sizeof(TCHAR));
        memset((void*)wideValue, 0x00, dwSize * sizeof(TCHAR));
        lResult = RegQueryValueEx(hKey, server, 0, &dwType, (LPBYTE)wideValue, &dwSize); 

        if (lResult == ERROR_SUCCESS)
        {
            if(dwType == REG_SZ)
            {
                wcstombs(getValue, wideValue, dwSize);  
            }
            else if(dwType == REG_DWORD)
            {                                       
                DWORD dwValue;
                lResult = RegQueryValueEx(hKey, server, 0, &dwType, (PBYTE)dwValue, &dwSize);
                if (lResult == ERROR_SUCCESS)
                {
                    TCHAR *lpszResult = (TCHAR *) malloc(dwSize * sizeof(TCHAR));
                    memset((void*)lpszResult, 0x00, dwSize * sizeof(TCHAR));
                    wsprintf(lpszResult, TEXT("%d"), dwValue);
                    wcstombs(getValue, lpszResult, dwSize);
                    free((void*) lpszResult);
                }
            }
            else
            {
                TCHAR *lpszResult = (TCHAR *) malloc(dwSize * sizeof(TCHAR));
                memset((void*)lpszResult, 0x00, dwSize * sizeof(TCHAR));
                wsprintf(lpszResult, TEXT("%s"), TEXT("error"));
                wcstombs(getValue, lpszResult, dwSize);
                free((void*) lpszResult);
            }
        }

        free((void*) wideValue);
    }
}



int _tmain(int argc, _TCHAR* argv[])
{
    char data[255];
    GetRegistryValue(data, 255);



    return 0;
}

0 个答案:

没有答案