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
Solved

RegEnumValue and LPBYTE type

Posted on 2007-11-19
10
1,549 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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 …
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.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

792 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