Advertisement

Editing the Windows Registry within a C/C++ application?

Started by September 06, 2000 12:39 AM
4 comments, last by Spike 24 years, 3 months ago
In what file is the windows 98 registry stored? ( RegEdit must get it from somewhere :-) ) Anyhow, my game needs to read and write information to the registry. The wearabouts of the data file will be a great start, but if there were any Functions that specifically dealt with handling the registry, it would be great help to hear about them. ( I do know how to program in C/C++ and use the win32 api ) Regards ~Spike You can contact me here
~SpikeYou can contact me at luke_howard@dingoblue.net.au
i think the file is system.dat, but check out the msdn for documentation on registry functions.. it would be a lot easier
Advertisement
Take a look at the set of Profile commands... e.g. GetProfileXXXXX, SetProfileXXXXX, in MSDN help.
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
You definitely don''t want to go mucking about with any file that contains the registry data. The ''correct'' (API-approved) way of handling the registry is through the Win32 API functions RegCreateKeyEx(), RegQueryValueEx(), and RegSetValueEx(). Here''s some relevant code from the screensaver I''m presently working on:

    // Registry.cpp -- a few routines for dealing with the registry,// loading and storing the user parameters to it.#include <windows.h>#include <regstr.h>#include "extern.h"#define SET_REGVAL(key,value,valtype) \  RegSetValueEx(key, #value, 0, valtype, (BYTE *)&value, sizeof(value))#define GET_REGVAL(key,value,deflt) \  Temp = sizeof(value); \  RetVal = RegQueryValueEx(key, #value, 0, NULL, (BYTE *) &value, &Temp); \  if (RetVal != ERROR_SUCCESS) { \    value = deflt; \  }#define SAVER_REGISTRY_PATH "Software\\SixPaw\\Mosaic"void StoreParameters(HKEY);void GetUserParameters(void) {  HKEY MosaicKey;  DWORD KeyExistence;  DWORD Temp;  LONG RetVal;  DWORD StringType = REG_SZ;  // Open up the registry path for the options for this screensaver  RetVal = RegCreateKeyEx(HKEY_CURRENT_USER, SAVER_REGISTRY_PATH, 0, "",                          REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,                          &MosaicKey, &KeyExistence);  if (RetVal != ERROR_SUCCESS) {    // Error here!  }  if (KeyExistence == REG_CREATED_NEW_KEY) {    // The options weren''t found; time to create them.    WindowWidth=640;    WindowHeight=480;    WindowBits=32;    ImageFileName[0] = ''\0'';    StoreParameters(MosaicKey);  } else {    GET_REGVAL(MosaicKey, WindowWidth, 640);    GET_REGVAL(MosaicKey, WindowHeight, 480);    GET_REGVAL(MosaicKey, WindowBits, 32);    Temp = 512;    RetVal = RegQueryValueEx(MosaicKey, "ImageFileName", 0, &StringType,                             (BYTE *) ImageFileName, &Temp);    if (RetVal != ERROR_SUCCESS) {      ImageFileName[0] = ''\0'';    }  }}void StoreParameters(HKEY RegKey){  SET_REGVAL(RegKey, WindowWidth, REG_DWORD);  SET_REGVAL(RegKey, WindowHeight, REG_DWORD);  SET_REGVAL(RegKey, WindowBits, REG_DWORD);  RegSetValueEx(RegKey, "ImageFileName", 0, REG_SZ, (BYTE *) ImageFileName,                strlen(ImageFileName)+1);}    


Note that things get (a little) more complicated when you want to retrieve or store strings, but for basic data types the macros I defined work very effectively.

Hope this helps!
Thankx for all the help.

It does seem very logical to use API approved functions to edit the registry. It would make the risk of messing up the OS much much less.

Shatteri: thankx, I got something simple going by refering to parts of your code, it was a great help!

~Spike
** Have I gone blind, or has the world gone black? **


Edited by - Spike on September 8, 2000 1:37:25 AM
~SpikeYou can contact me at luke_howard@dingoblue.net.au
here is a tutorial i wrote for reading/writing to/from the registry...
http://workspot.net/~kondor/tutors/windows/registry.html

This topic is closed to new replies.

Advertisement