[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Registry API functions

Posted on 1999-11-30
5
Medium Priority
?
435 Views
Last Modified: 2012-05-04
I want to write a simple console application program that will read a registry key.  I want to use the RegOpenKeyEx, RegQueryKeyEx, and RegCloseKey registry API functions.  I am having trouble figuring out how to get started (what files to include and such).  I started a new console application project (in VC++ 6.0) and chose to create a simple "Hello World" program.  From there, I tried to include <winreg.h>, but when I tried to compile it, the compiler reported finding an error in winreg.h (something like it didn't find a semicolon where it thought there should be one).  I don't expect errors to be in standard header files that come with VC++, so I am assuming that I am doing something wrong.  Can someone explain the right way to include header files that will enable me to use registry functions.  Simple sample code would be great.
0
Comment
Question by:tdiamond
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2245705
Next is my code for make it
int Test(void)
{
  int bStop = 0;
      // skip registry key opening sequence for clarity
  const INT nSIZE = 4096; //maximum(real 8)
  const DWORD dwLen = 8;
  const char *lpszPathName = "SOFTWARE\\COMPANY\\MYCOMPANY";
  const char *pszValueName = "Param01";

  HKEY hKey,hBaseKey = HKEY_LOCAL_MACHINE;
  DWORD dwType;
  DWORD dwData  = nSIZE;
  char* bData   = (char *)::calloc(nSIZE, sizeof(char));
  struct date dDate;
  LONG retValue;

                        //open key
  retValue = ::RegOpenKeyEx(hBaseKey, lpszPathName,
                   NULL, KEY_ALL_ACCESS,&hKey);
  if (retValue != ERROR_SUCCESS)//no such : 1-st time
    {                           //create key
       uint32 disposition;
       retValue = ::RegCreateKeyEx(hBaseKey, lpszPathName, 0, ""/*class*/,
                REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,
                0/*SecurityAttributes*/, &hKey,
                &disposition);
       if (retValue == ERROR_SUCCESS)//must be!
       {
          //you code for form bData
                        //save data
         ::RegSetValueEx(hKey, pszValueName, 0, REG_BINARY,
                    (LPBYTE)bData, dwLen);
         ::RegCloseKey(hKey); //close key
       }
    }
  else            //exist, read data
    {
      retValue = ::RegQueryValueEx(hKey, pszValueName,
                    NULL, &dwType,(LPBYTE)bData, &dwData);

       if (retValue == ERROR_SUCCESS && REG_BINARY == dwType)
       {
         if (Empty(bData))      //no data?
           {
             //you code for form bData
                  // Save
            ::RegSetValueEx(hKey, pszValueName, 0, REG_BINARY,
                    (LPBYTE)bData, dwLen);
           }
         else  //test data
           {
           }

       }
      ::RegCloseKey(hKey);            //close key
    }
  if (bData)
    free (bData);
  return bStop;
}
Alex
0
 
LVL 23

Expert Comment

by:chensu
ID: 2245709
#include <windows.h>

instead of

#include <winreg.h>
0
 

Author Comment

by:tdiamond
ID: 2247030
AlexVirochovsky, thanks for the code, although you forgot to tell me what header files to include so your code would work on my machine.  You can thank Chensu for his comment.

Thanks Chensu...
0
 

Author Comment

by:tdiamond
ID: 2247047
AlexVirochovsky - I made a mistake and rejected your answer by mistake.  Answer it again and I will grade it.

Sorry again...
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 800 total points
ID: 2247466
Ok, thanks.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

612 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