Solved

RegEnumValue and LPBYTE type

Posted on 2007-11-19
10
1,539 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 30

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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