Read registry entry string with C code

Reading a non-fixed-length string from registry with the help of C? Here's how.

After DLL calling from C1 the next nice thing was to read out a string from the registry.

To be honest I'm now sure we need all the headers included, but unfortunately I don't have time to test them all. The main point is that we need to dinamically adjust the size of the buffer to read out the exact length we need. The returned value behaves the same as it were a string.

#include <windows.h>
#include <winbase.h>
#include <windef.h>
#include <stdio.h>
#include <string.h>

PPERF_DATA_BLOCK readStringFromRegistry( void* keyname, void* valuename  ) {
  HKEY hkeyPtr;
  DWORD keystatus;
  DWORD buffersize = 1;
  DWORD cbData = buffersize;
  PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( buffersize );

  keystatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT(keyname), 0,KEY_READ, &hkeyPtr);
  if (ERROR_SUCCESS == keystatus)
    keystatus = RegQueryValueEx( hkeyPtr, TEXT(valuename),NULL,NULL,(LPBYTE) PerfData,&cbData );

    while( keystatus == ERROR_MORE_DATA )
      buffersize += 1;
      PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, buffersize );
      cbData = buffersize;
      keystatus = RegQueryValueEx( hkeyPtr,TEXT(keyname),NULL,NULL,(LPBYTE) PerfData,&cbData );

  return PerfData;