Link to home
Start Free TrialLog in
Avatar of webrage
webrage

asked on

RegQueryValueEx trouble...

I have created a service giving it administration rigths but still it wont read registry keys..
the key: HKEY_LOCAL_MACHINE/software/blaha/hejhej/ ,
where i read two strings and two dwords!
this mess only occurs when the program is running as a service.. btw i putted audit when a reg query failure occcurs, but it doesnt "audit" anything!
Avatar of jhance
jhance

The fact is that RegQueryValueEx does indeed work even when running as a service.  Why don't you post some of your code so that it can be examined?
Avatar of webrage

ASKER

strcpy(szKey, "software\\emcat\\emcat gateway service\\");
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
             szKey,
               NULL,
                 KEY_QUERY_VALUE,
                 &hkey) == ERROR_SUCCESS) {
      RegQueryValueEx(hkey,                                                    "dbconnstr",
                  NULL,
                  NULL,
                  (unsigned char *)&CONNSTR,
                  &dwsize);
      RegQueryValueEx(hkey,
                  "logname",
                  NULL,
                  NULL,
                  (unsigned char *)&logname,
                  &dwsize);
      RegQueryValueEx(hkey,
                  "dbupdate",
                  NULL,
                  NULL,
                  (unsigned char *)&dbUpdate,
                  &dwsize);
      RegQueryValueEx(hkey,
                  "dirupdate",
                  NULL,
                  NULL,
                  (unsigned char *)&dirUpdate,
                  &dwsize);
      RegCloseKey(hkey);
      } else { //it doesnt do this
        strcpy(buf,"Error reading: ");
        strcat(buf,szKey);
        strcat(buf," key in registry!");
        AddToMessageLog(TEXT(buf));        
        printf("\nError open key");
        }

but dbconnstr and the other ones have the value of ""!
and this work running as not a service! strange eh!

Avatar of webrage

ASKER

checking if the code looks better this way..

      
strcpy(szKey, "software\\emcat\\emcat gateway service\\");
            if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                     szKey,
                                     NULL,
                                     KEY_QUERY_VALUE,
                                     &hkey) == ERROR_SUCCESS) {
                  RegQueryValueEx(hkey,
                                          "dbconnstr",
                                          NULL,
                                          NULL,
                                          (unsigned char *)&CONNSTR,
                                          &dwsize);
                  RegQueryValueEx(hkey,
                                          "logname",
                                          NULL,
                                          NULL,
                                          (unsigned char *)&logname,
                                          &dwsize);
                  RegQueryValueEx(hkey,
                                          "dbupdate",
                                          NULL,
                                          NULL,
                                          (unsigned char *)&dbUpdate,
                                          &dwsize);
                  RegQueryValueEx(hkey,
                                          "dirupdate",
                                          NULL,
                                          NULL,
                                          (unsigned char *)&dirUpdate,
                                          &dwsize);
                  
                  RegCloseKey(hkey);
            } else {
                  strcpy(buf,"Error reading: ");
                  strcat(buf,szKey);
                  strcat(buf," key in registry!");
                  AddToMessageLog(TEXT(buf));
                  printf("\nError open key");
            }
Avatar of webrage

ASKER

ok sorry i hope u can read it anyway
I've had problems before becuase I was not setting the MaxSize of the input buffer before calling the RegQueryValueEx()
ie. dwSize = BufferSize
ReqQuery...
then before the next read reset the value of dwsize ready for the next read

Also check if the buffer is large enough to hold the data, if the size of the keydata is bigger than dwSize, RegQueryValueEx returns an error.
You can let RegQueryValueEx set the dwSize parameter to the keydata's size by calling RegQueryValueEx with the buffer parameter set to NULL.
The clue is the code works when the program is running normally, but fails when the program runs as a service.

This indicates it's a security problem...

The 4th parameter of RegOpenKeyEx is the security mask based on the minimum access you need.

Additionally make sure you service has rights to access the information needed
Avatar of webrage

ASKER

oh sorry but i have solved the problem already..
and it was Andy Keys who helped me! so Andy if
he is listning, send an answer so i can give ya points...
ASKER CERTIFIED SOLUTION
Avatar of Andy_Keys
Andy_Keys

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK webrage

Well done Andy