Solved

RegQueryValueEx trouble...

Posted on 1998-08-25
10
208 Views
Last Modified: 2010-04-10
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!
0
Comment
Question by:webrage
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 1171163
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?
0
 

Author Comment

by:webrage
ID: 1171164
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!

0
 

Author Comment

by:webrage
ID: 1171165
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");
            }
0
 

Author Comment

by:webrage
ID: 1171166
ok sorry i hope u can read it anyway
0
 
LVL 1

Expert Comment

by:Andy_Keys
ID: 1171167
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

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 1171168
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.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1171169
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
0
 

Author Comment

by:webrage
ID: 1171170
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...
0
 
LVL 1

Accepted Solution

by:
Andy_Keys earned 40 total points
ID: 1171171
Gladdly!
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1171172
OK webrage

Well done Andy
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

759 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

21 Experts available now in Live!

Get 1:1 Help Now