For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > RegQueryValueEx(...)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author RegQueryValueEx(...)
David Watkins

2005-11-21, 4:00 am

I am writing an NT service (Win32 Console App) to act as a scheduler. It will launch another application at a determined time. I am storing the name of the app and some parameters on when it is to be launched in the registry.

I can retrieve registry entries that are stored as a DWORD with no problem. The problem is when I try to read a string REG_SZ. The following code just hangs when running as a service.

Note that if I run the same exact code below in an MFC app or a Win32 Console App not as a service it retrieves the string with no problem. The non-service console app and the service console app have the exact same project settings and include files.


BOOL GetRegyString(LPCTSTR pszKeyNm, LPCTSTR pszvalNm, LPTSTR lpbyDataBuf, int nBufSize)
{
DWORD dwValType = REG_SZ;
BOOL bSuccess = TRUE;
HKEY hKey;

if (OpenRegyKey(pszKeyNm, &hKey))
{
strcpy(lpbyDataBuf,_T("Failed To Retrieve String From Registry"));

if (RegQueryValueEx(hKey,
pszvalNm,
NULL,
&dwValType,
(LPBYTE) lpbyDataBuf,
(LPDWORD) &nBufSize) == ERROR_SUCCESS)

{
RegCloseKey(hKey);
return bSuccess;
}

RegCloseKey(hKey);
}
return !bSuccess;
}

The Win32 Console App service code will run, but fail to get the string, if I declare and set the following:

DWORD dwBufSize;
dwBufSize = sizeof(nBufsize);

but will still hang with the following


DWORD dwBufSize;
dwBufSize = nBufsize;

Not sure what is going on here.

Thanks,

Dave


Vincent Fatica

2005-11-21, 4:00 am

On Mon, 21 Nov 2005 04:43:00 GMT, "David Watkins" <DaveW@worldnet.att.net>
wrote:

>BOOL GetRegyString(LPCTSTR pszKeyNm, LPCTSTR pszvalNm, LPTSTR lpbyDataBuf, int nBufSize)


You don't say how you are calling this function ...

> if (OpenRegyKey(pszKeyNm, &hKey)


.... or what OpenRegyKey does.

>DWORD dwBufSize;
>dwBufSize = sizeof(nBufsize);


That doesn't seem right; sizeof(nBufSize) is 4 (it's an int).

More info needed.
--
- Vince
David Watkins

2005-11-21, 4:00 am

The calling code:

#define MAX_REGY_STRING 255

TCHAR szStrVal[MAX_REGY_STRING];

if (!GetRegyString(cstrRegyPath, cstrRegyVal, szStrVal, MAX_REGY_STRING))
{
...
}


For what this is worth here is the OpenRegyKey(...) function


BOOL OpenRegyKey(LPCTSTR pszKeyNm, HKEY *pHKey)
{
BOOL bSuccess = TRUE;

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
pszKeyNm,
0,
KEY_ALL_ACCESS,
pHKey) != ERROR_SUCCESS)
{
return !bSuccess;
}
return bSuccess;
}


"Vincent Fatica" <abuse@localhost.com> wrote in message
news:4381543f$1@vefatica.net...
> On Mon, 21 Nov 2005 04:43:00 GMT, "David Watkins" <DaveW@worldnet.att.net>
> wrote:
>
>
> You don't say how you are calling this function ...
>
>
> ... or what OpenRegyKey does.
>
>
> That doesn't seem right; sizeof(nBufSize) is 4 (it's an int).
>
> More info needed.
> --
> - Vince



David Watkins

2005-11-21, 4:00 am

What is strange is that the RegQueryValueEx() will hang if the buffer size
parameter is large enough. If the buffer size parameter is too small then
the function returns with ERROR_MORE_DATA but does not hang.


"Vincent Fatica" <abuse@localhost.com> wrote in message
news:4381543f$1@vefatica.net...
> On Mon, 21 Nov 2005 04:43:00 GMT, "David Watkins" <DaveW@worldnet.att.net>
> wrote:
>
>
> You don't say how you are calling this function ...
>
>
> ... or what OpenRegyKey does.
>
>
> That doesn't seem right; sizeof(nBufSize) is 4 (it's an int).
>
> More info needed.
> --
> - Vince



Frank Hickman [MVP]

2005-11-21, 4:00 am

"David Watkins" <DaveW@worldnet.att.net> wrote in message
news:lEcgf.76399$qk4.42308@bgtnsc05-news.ops.worldnet.att.net...
> The calling code:
>
> #define MAX_REGY_STRING 255
>
> TCHAR szStrVal[MAX_REGY_STRING];
>
> if (!GetRegyString(cstrRegyPath, cstrRegyVal, szStrVal, MAX_REGY_STRING))
> {
> ...
> }
>


<snip>

What does this function, GetRegyString, look like?

--
============
Frank Hickman
Microsoft MVP
NobleSoft, Inc.
============
Replace the _nosp@m_ with @ to reply.


Vincent Fatica

2005-11-21, 4:00 am

On Mon, 21 Nov 2005 05:28:22 GMT, "David Watkins" <DaveW@worldnet.att.net>
wrote:

>What is strange is that the RegQueryValueEx() will hang if the buffer size
>parameter is large enough. If the buffer size parameter is too small then
>the function returns with ERROR_MORE_DATA but does not hang.


RegQueryValueEx() has never failed to return for me. I would have guessed
that was impossible.
--
- Vince
Frank Hickman [MVP]

2005-11-21, 4:00 am

"Frank Hickman [MVP]" <fhickman3_NOSP@M_noblesoft.com> wrote in message
news:u9B1g%23l7FHA.4012@TK2MSFTNGP14.phx.gbl...
> "David Watkins" <DaveW@worldnet.att.net> wrote in message
> news:lEcgf.76399$qk4.42308@bgtnsc05-news.ops.worldnet.att.net...
>
> <snip>
>
> What does this function, GetRegyString, look like?
>


Doh! Nevermind I see it now...

It appears that your passing the address of a stack variable as an out
variable. Try rewriting it like this...

BOOL GetRegyString(LPCTSTR pszKeyNm, LPCTSTR pszvalNm, LPTSTR lpbyDataBuf,
LPDWORD lpBufSize)
{
// You should really put code here to make sure your parameters are
valid before using them...
ASSERT( pszKeyNm != NULL );
ASSERT( pszvalNm != NULL );
ASSERT( lpbyDataBuf != NULL );
ASSERT( lpBufSize != NULL );

DWORD dwValType= REG_SZ;
DWORD nBufLen= *lpBufSize;
BOOL bSuccess = TRUE;
HKEY hKey;

if (OpenRegyKey(pszKeyNm, &hKey))
{
strcpy(lpbyDataBuf,_T("Failed To Retrieve String From Registry"));

if (RegQueryValueEx(hKey,
pszvalNm,
NULL,
&dwValType,
(LPBYTE) lpbyDataBuf,
&nBufSize) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
*lpBufSize= nBufSize;
return bSuccess;
}

RegCloseKey(hKey);
}
return !bSuccess;
}

This may or may not be the cause for the lockup.

--
============
Frank Hickman
Microsoft MVP
NobleSoft, Inc.
============
Replace the _nosp@m_ with @ to reply.


David Watkins

2005-11-22, 4:01 am

Dumb Mistake!

Actually the program was not dying in the RegQueryValueEx(). It was
actually hanging on exit of the GetRegyString() when I was passing the
retrieved string with a bogus pointer. I finally got the debugger working
on the NTservice.

Dave



"Frank Hickman [MVP]" <fhickman3_NOSP@M_noblesoft.com> wrote in message
news:ucmfZIm7FHA.3660@TK2MSFTNGP09.phx.gbl...
> "Frank Hickman [MVP]" <fhickman3_NOSP@M_noblesoft.com> wrote in message
> news:u9B1g%23l7FHA.4012@TK2MSFTNGP14.phx.gbl...
>
> Doh! Nevermind I see it now...
>
> It appears that your passing the address of a stack variable as an out
> variable. Try rewriting it like this...
>
> BOOL GetRegyString(LPCTSTR pszKeyNm, LPCTSTR pszvalNm, LPTSTR lpbyDataBuf,
> LPDWORD lpBufSize)
> {
> // You should really put code here to make sure your parameters are
> valid before using them...
> ASSERT( pszKeyNm != NULL );
> ASSERT( pszvalNm != NULL );
> ASSERT( lpbyDataBuf != NULL );
> ASSERT( lpBufSize != NULL );
>
> DWORD dwValType= REG_SZ;
> DWORD nBufLen= *lpBufSize;
> BOOL bSuccess = TRUE;
> HKEY hKey;
>
> if (OpenRegyKey(pszKeyNm, &hKey))
> {
> strcpy(lpbyDataBuf,_T("Failed To Retrieve String From Registry"));
>
> if (RegQueryValueEx(hKey,
> pszvalNm,
> NULL,
> &dwValType,
> (LPBYTE) lpbyDataBuf,
> &nBufSize) == ERROR_SUCCESS)
> {
> RegCloseKey(hKey);
> *lpBufSize= nBufSize;
> return bSuccess;
> }
>
> RegCloseKey(hKey);
> }
> return !bSuccess;
> }
>
> This may or may not be the cause for the lockup.
>
> --
> ============
> Frank Hickman
> Microsoft MVP
> NobleSoft, Inc.
> ============
> Replace the _nosp@m_ with @ to reply.
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com