开发者

RegQueryValueExW only brings back one value from registry

开发者 https://www.devze.com 2023-01-08 08:59 出处:网络
I am querying the registry on Windows CE.I want to pull back the DhcpDNS value from the TcpIp area of the registry, which works.

I am querying the registry on Windows CE. I want to pull back the DhcpDNS value from the TcpIp area of the registry, which works.

What happens though, however, is if there is two values - displayed as "x.x.x.x" "x.x.x.x" in my CE registry editor - then it only brings back one of them. I am sure this is a silly mistake but I am unsure why it is happening.

Here is the code I am using

std::string ISAPIConfig::GetTcpIpRegSetting(const std::wstring &regEntryName)
{
    HKEY hKey = 0;
    HKEY root = HKEY_LOCAL_MACHINE;
    LONG retVal = 0;

    wchar_t buffer[3000];
    DWORD bufferSize = 0;
    DW开发者_C百科ORD dataType = 0;

    std::string dataString = "";

    //Open IP regkey
    retVal = RegOpenKeyExW(root, L"Comm\\PCI\\FETCE6B1\\Parms\\TcpIp", 0, 0, &hKey);

    //Pull out info we need
    memset(buffer, 0, sizeof(buffer));
    bufferSize = sizeof(buffer);
    retVal = RegQueryValueExW(hKey, regEntryName.c_str(), 0, &dataType, reinterpret_cast<LPBYTE>(buffer), &bufferSize);
    Unicode::UnicodeToAnsi(buffer, dataString);

    return dataString;
}

void UnicodeToAnsi(const std::wstring &wideString, std::string &ansiString){
    std::wostringstream converter;
    std::ostringstream converted;
    std::wstring::const_iterator loop;

    for(loop = wideString.begin(); loop != wideString.end(); ++loop){
        converted << converter.narrow((*loop));
    }

    ansiString = converted.str();
}


The value is a multi_sz, which is in the format:

{data}\0{data}\0\0

I don't know what the Unicode::UnicodeToAnsi does, but it's likely just looking for that first null terminator and stopping there. You have to parse past single nulls until you hit the double-null.

EDIT

You have to update your code - very likely your interfaces. Right now you're trying to returns a string for a multi_sz which, by definition, means multiple strings. you probably want to returns a string[] (though I'd probably opt to use a couple output parameters - one that's an array pointer and the other that is a element count).

You then need to loop through the data that came back from the RegQuery call, something maybe like this (off the top of my head, not tested or compiled):

TCHAR *p = buffer;

if(bufferSize > 0)
{
  do
  {
      Unicode::UnicodeToAnsi(p, dataString); 
      // do something with dataString - store it in an array or whatever
      p+= _tcslen(p);
    }   while((p-buffer) < bufferSize)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消