Solved

RegEnumValue and LPBYTE type

Posted on 2007-11-19
10
1,555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Acrinis True image 2 118
Memory going from 12gb to 64gb or 96gb. worth it? 15 221
How to downgrade visual studio 2015 enterprise to professional 6 120
What are MicroServices? 4 65
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and 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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

738 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