roger's picture

Things I learnt this week: RegQueryValueEx

RegQueryValueEx, when passed lpData = NULL, will set *lpcbData to the length required in bytes, even if it's already set to something. If lpData != NULL, and *lpcbData is too short, RegQueryValueEx will return ERROR_MORE_DATA.

This means that you probably ought to call it in a loop, like this:

   DWORD cbData = 0;
   BYTE *pData = (BYTE *)malloc(cbData);
   do
   {
      result = RegQueryValueEx(hKey, pszValueName, NULL, NULL, pData, &cbData);
      if (result == ERROR_SUCCESS)
         break;

      free(pData);
      pData = (BYTE *)malloc(cbData);
   } while (result == ERROR_MORE_DATA);

   if (result == ERROR_SUCCESS)
   {
      // Do something with pData
   }

   free(pData);

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <br> <code> <dd> <dl> <dt> <hr> <h1> <h2> <h3> <i> <img> <li> <ol> <p> <pre> <table> <td> <th> <tr> <tt> <u> <ul>
  • Images can be added to this post.

More information about formatting options