Overview
Features
Download
Documentation
Community
Add-Ons & Services

[Win] modify registry key with binary data

General discussion regarding the development of POCO for contributors.

[Win] modify registry key with binary data

Postby nicolas » 07 Dec 2010, 19:11

Hello,

It could be interesting if WinRegistryKey class was able to set/get value with binary data, it is sometime necessary to modify system flags in windows registry.
In the same logic than other accessors in this class, here is the code for a binary value access:

Code: Select all
typedef unsigned char Byte;
typedef std::vector<Byte> Binary;


Code: Select all
void WinRegistryKey::setBinary(const std::string& name, const Binary& data)
   {
      open();
      #if defined(POCO_WIN32_UTF8)
         std::wstring uname;
         Poco::UnicodeConverter::toUTF16(name, uname);
         if (RegSetValueExW(_hKey, uname.c_str(), 0, REG_BINARY, &data.front(), data.size()) != ERROR_SUCCESS)
            handleSetError(name);
      #else
         if (RegSetValueEx(_hKey, name.c_str(), 0, REG_BINARY, &data.front(), data.size()) != ERROR_SUCCESS)
            handleSetError(name);
      #endif
   }


Code: Select all
void BinaryRegistryKey::getBinary(const std::string& name, Binary& data) const
   {
      data.clear();
      open();
      DWORD type;
      DWORD size;
      #if defined(POCO_WIN32_UTF8)
         std::wstring uname;
         Poco::UnicodeConverter::toUTF16(name, uname);
         if (RegQueryValueExW(_hKey, uname.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || type != REG_BINARY)
            throw NotFoundException(key(name));
         if (size > 0)
         {
            data.resize(size);
            RegQueryValueExW(_hKey, uname.c_str(), NULL, NULL, &data.front(), &size);
         }
      #else
         if (RegQueryValueEx(_hKey, name.c_str(), NULL, &type, NULL, &size) != ERROR_SUCCESS || type != REG_BINARY)
            throw NotFoundException(key(name));
         if (size > 0)
         {
            data.resize(size);
            RegQueryValueEx(_hKey, name.c_str(), NULL, NULL, &data.front(), &size);
         }
      #endif
   }


After it could be nice to have data value as return value with getBinary() function, but std::vector is not COW...
I let you think about that ;)
nicolas
 
Posts: 5
Joined: 07 Dec 2010, 18:38

Return to Contributors

Who is online

Users browsing this forum: No registered users and 1 guest