CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: RegQueryValueEx

  1. #1
    Join Date
    May 2009
    Posts
    14

    RegQueryValueEx

    Okay, for some reason when I call RegQueryValueEx, my buffer pads every other byte with a space. So the output is something like "1 0 0 1 u s w e s t" and so on... I've tried a few different methods but can't seem to nail this one...

    On top of that, when I cout the data, it only prints the first byte...

    Code:
    int main ()
    {
    
    	HKEY hKey;					
    	DWORD cType;
    	char lpData[1024] = {0};
    	DWORD buffersize = sizeof(lpData);
    
    	if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Blizzard Entertainment\\Warcraft III"),NULL,KEY_READ,&hKey) == ERROR_SUCCESS)
    	{
    		cout << "Opened successfully";
    	}else{
    		cout << "Failed to open reg key: " << GetLastError() << "\n";
    	}
    	
    	RegQueryValueEx(hKey,TEXT("Battle.net Gateways"),NULL,&cType,(LPBYTE) lpData,&buffersize);
    	
    	cout << "Registry Key Open: mem key location=" << hKey << "\n\n";
    	cout << "Data: " << lpData;
    
    	RegCloseKey (hKey);
    	
    	system("Pause");
    
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: RegQueryValueEx

    Quote Originally Posted by Feered View Post
    Okay, for some reason when I call RegQueryValueEx, my buffer pads every other byte with a space. So the output is something like "1 0 0 1 u s w e s t" and so on...
    Is it a UNICODE string?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2009
    Posts
    14

    Re: RegQueryValueEx

    I fixed the space padding by going to the compilers settings and turning UNICODE to Not Set. However, now when I cout the buffer, it's only printing the first word.


    --edit
    I'm sorry, just realized it's not a space padding. It's a null padding after each word.
    Last edited by Feered; June 17th, 2010 at 07:26 AM.

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: RegQueryValueEx

    Your code contains few mistakes.
    Take a look at next example and guess which are they.

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <iostream>
    
    #ifdef UNICODE
    #define t_cout wcout
    #else
    #define t_cout cout
    #endif
    
    int _tmain()
    {
       HKEY hKey = NULL;               
       DWORD cType = REG_NONE;
       TCHAR lpData[1024] = {0};
       DWORD size = sizeof(lpData);
    
       LONG lRet = RegOpenKeyEx(HKEY_CURRENT_USER, 
                               _T("Software\\Blizzard Entertainment\\Warcraft III"), 
                               NULL, KEY_QUERY_VALUE, &hKey);
       if(ERROR_SUCCESS != lRet)
       {
          std::t_cout << _T("RegOpenKeyEx failed. Error: ") << lRet << std::endl;;
          return lRet;
       }
    
       lRet = RegQueryValueEx(hKey, _T("Battle.net Gateways"), NULL, &cType, (LPBYTE)lpData, &size);
       RegCloseKey(hKey);
    
       if(ERROR_SUCCESS != lRet)
       {
          std::t_cout << _T("RegQueryValueEx failed. Error: ") << lRet << std::endl;;
          return lRet;
       }
    
       if(REG_SZ != cType)
       {
          std::t_cout << _T("Value has not expected type REG_SZ") << std::endl;
          return E_FAIL;
       }
    
       std::t_cout << "Data: " << lpData << std::endl;
       system("Pause");
       return ERROR_SUCCESS;
    }
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Jun 2010
    Posts
    115

    Re: RegQueryValueEx

    Hello, ovidiucucu

    I'd like to get the Type of the data, (REG_SZ,REG_DWORD etc),

    Thank you

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: RegQueryValueEx

    RegQueryValueEx has a parameter:
    lpType
    [out] Pointer to a variable that receives a code indicating the type of data stored in the specified value. For a list of the possible type codes, see Registry Value Types. The lpType parameter can be NULL if the type code is not required.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured