Solved

RegEnumValue and LPBYTE type

Posted on 2007-11-19
10
1,544 Views
Last Modified: 2013-12-04
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
Comment
Question by:MardukLitzer
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 225 total points
ID: 20312721
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
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
ID: 20312728
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 20312731
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 75 total points
ID: 20313387
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
 
LVL 4

Assisted Solution

by:yuy2002
yuy2002 earned 25 total points
ID: 20318112
LPBYTE storeRegValue = new BYTE[1000];
DWORD dwType = REG_SZ;
DWORD dwLen = 1000;
retCode = RegEnumValue(hKey, i, str, &cchValue, 0, &dwType , (LPBYTE)storeRegValue, &dwLen );
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 225 total points
ID: 20318554
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 75 total points
ID: 20318634
>>>> 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
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 25 total points
ID: 20319078
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
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20319082
Well, simillar in RegEnumValue
0
 
LVL 2

Author Comment

by:MardukLitzer
ID: 20319128
Thanks all for your clarifications! I'm still trying to get used to the WINDOWS DATATYPES :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now