Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1622
  • Last Modified:

RegEnumValue and LPBYTE type

Hello, I'm trying to return a value from a registry key and am having issues with the LPBYTE datatype.

LPBYTE storeRegValue = NULL;
storeRegValue = new BYTE[1000];
retCode = RegEnumValue(hKey, i, str, &cchValue, 0, (LPDWORD)REG_SZ, (LPBYTE)storeRegValue, (LPDWORD)MAX_VALUE_NAME);

When I debug this, it gives me Bad Ptr errors even after initializing. The RegEnumValue is crashing the app with an "Cannot Read Memory Location Error". Any ideas? I've searched on the web and the code is declared and initialized in much the same way as I have done. Any help pointing out what I've missed would be great! Thanks in advance
0
MardukLitzer
Asked:
MardukLitzer
  • 2
  • 2
  • 2
  • +4
8 Solutions
 
ZoppoCommented:
Hi MardukLitzer,

the '(LPDWORD)REG_SZ' and '(LPDWORD)MAX_VALUE_NAME' wrong

Instead of '(LPDWORD)REG_SZ' you must either pass 'NULL' if you don't care about the value-type or a pointer to a DWORD which will recieve the type of the value

Instead of '(LPDWORD)MAX_VALUE_NAME' you must pass a pointer to a DWORD which is initialized with the size of the memory pointed to by 'storeRegValue' - after return this DWORD will contain the number of bytes copied to 'storeRegValue' ...

BTW, best would be to call this function twice, once to fine out how much bytes you need to allocate for the buffer and then a second time with the allocated buffer to be filled.

Hope that helps,

ZOPPO
0
 
Jaime OlivaresCommented:
There is at least one error, shoul be:

DWORD type = REG_SZ;
retCode = RegEnumValue(hKey, i, str, &cchValue, 0, &type, (LPBYTE)storeRegValue, (LPDWORD)MAX_VALUE_NAME);

0
 
jkrCommented:
The problem is the 'lpType' parameter, you need to pass the address of a DWORD there:


LPBYTE storeRegValue = NULL;
storeRegValue = new BYTE[1000];
DWORD dwType = REG_SZ; // <---------!
retCode = RegEnumValue(hKey, i, str, &cchValue, 0, &dwType,  // <---!
(LPBYTE)storeRegValue, (LPDWORD)MAX_VALUE_NAME);
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
itsmeandnobodyelseCommented:
To add to above comments:

The final statement is:

    DWORD dwType = REG_SZ;
    DWORD dwLen = 1000;

    retCode = RegEnumValue(hKey, i, str, &cchValue, 0, &dwType,  
       (LPBYTE)storeRegValue, &dwLen);

Regards, Alex
0
 
yuy2002Commented:
LPBYTE storeRegValue = new BYTE[1000];
DWORD dwType = REG_SZ;
DWORD dwLen = 1000;
retCode = RegEnumValue(hKey, i, str, &cchValue, 0, &dwType , (LPBYTE)storeRegValue, &dwLen );
0
 
ZoppoCommented:
BTW: Hardcoding 1000 is IMO not good - you should use RegQueryInfoKey to retrieve the length of the longest value-name and value-data, i.e.:

DWORD dwMaxNameLen = 0, dwMaxDataLen = 0;

if ( RegQueryInfoKey( hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dwMaxNameLen, &dwMaxDataLen, NULL, NULL ) != ERROR_SUCCESS )
{
 return;
}

TCHAR* str = new TCHAR[ dwMaxNameLen };
LPBYTE storeRegValue = new BYTE[ dwMaxDataLen ];
cchValue = dwMaxNameLen;
DWORD dwLen = dwMaxDataLen;

retCode = RegEnumValue(hKey, i, str, &cchValue, 0, &dwType , (LPBYTE)storeRegValue, &dwLen );

..

delete [] str;
delete [] storeRegValue;

regards,

ZOPPO

0
 
itsmeandnobodyelseCommented:
>>>> BTW: Hardcoding 1000 is IMO not good - you should use
>>>> RegQueryInfoKey to retrieve the length of the longest
>>>> value-name and value-data, i.e.:

If you know the max size or know a reasonable upper boundary you could/should do

    BYTE storeRegValue[4096] = { 0 };
    DWORD dwType = REG_SZ;
    DWORD dwLen = sizeof(storeRegValue);
    retCode = RegEnumValue(hKey, i, str, &cchValue, 0,
                        &dwType , storeRegValue, &dwLen );

The RegQueryInfoKey is only needed if you have to read arbitrary entries of unknown sizes.

Note, a cast to LPBYTE is not necessary if you have a BYTE* or a BYTE[].
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
Did you try casting it?

char *tmp = new char[1000];
if( NULL != tmp )
{
      RegQueryValueEx(hKey, name.data(), NULL, &dwtype, (LPBYTE)tmp, &dwsize);
      // do someting
      delete tmp;//finally delete
            
}

Best Regards,
DeepuAbrahamK
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
Well, simillar in RegEnumValue
0
 
MardukLitzerAuthor Commented:
Thanks all for your clarifications! I'm still trying to get used to the WINDOWS DATATYPES :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now