 |
Registry Access.
I dunno if it's just me being stupid (probably) but the first time I tried to write registry accessing stuff
I had all sorts of grief. So here's a nice little file that I add to all my Win32 projects that lets me access
the registry in a lovely fluffy pain-free way. Just download the zip and uncompress the single solitary C file
(dialog.c) that's in it. Add this to your project.
That's all very well, but how the hell do I use it. Well, first you'll need to set up a string table that
looks something like this. These examples are taken from the OSpy32 code, have a look there if you like.
These set up whereabouts in the registry the information will be saved ('HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\Oracle Spy 32-bit'
in this case), what type of entry to create (leave IDS_REG_KEY_CLASS as 'Directory') and the names of
the keys which will get created under this registry directory. Obviously you'll need to define the
IDs for these strings somewhere.
|   |
  |
 |
|   |
IDS_DESCRIPTION "Oracle Spy 32-bit"
IDS_INI_SECTION "SOFTWARE\\ORACLE\\"
IDS_REG_KEY_CLASS "Directory"
IDS_CONFIG_USER "User"
IDS_CONFIG_DBMS "Database"
IDS_CONFIG_PIPE "Pipe"
IDS_CONFIG_TIMER_SPEED "TimerSpeed"
|
  |
|   |
  |
  |
Then I usually create a single structure to hold all my configuration data, like this.
|   |
  |
 |
|   |
typedef struct tagCONFIG{
   TCHAR   user[62];
   TCHAR   database[31];
   TCHAR   pipe[31];
   UINT    timer_speed;
}CONFIG;
|
  |
|   |
  |
  |
Then I write a couple of functions to quickly and easily read and write this configuration information.
|   |
  |
 |
|   |
void GetConfig(CONFIG* lpconfig){
   ss_RegistrySetup(
     GetModuleHandle(NULL),
     IDS_INI_SECTION,
     IDS_DESCRIPTION,
     IDS_REG_KEY_CLASS
   );
   ss_GetRegistryString(
     IDS_CONFIG_USER,
     "scott/tiger\0",
     config->user,
     62
     );
   ss_GetRegistryString(
     IDS_CONFIG_DBMS,
     "new_york\0",
     config->database,
     31
   );
   ss_GetRegistryString(
     IDS_CONFIG_PIPE,
     "my_pipe\0",
     config->pipe,
     31
   );
   lpconfig->timer_speed=ss_GetRegistryInt(
     IDS_CONFIG_TIMER_SPEED,
     500
   );
   ss_RegistryShutdown();
}
...
void SetConfig(CONFIG* lpconfig){
   ss_RegistrySetup(
     GetModuleHandle(NULL),
     IDS_INI_SECTION,
     IDS_DESCRIPTION,
     IDS_REG_KEY_CLASS
   );
   ss_WriteRegistryString(
     IDS_CONFIG_USER,
     config->user
   );
   ss_WriteRegistryString(
     IDS_CONFIG_DBMS,
     config->database
   );
   ss_WriteRegistryString(
     IDS_CONFIG_PIPE,
     config->pipe
   );
   ss_WriteRegistryInt(
     IDS_CONFIG_TIMER_SPEED,
     lpconfig->timer_speed
   );
   ss_RegistryShutdown();
}
|
  |
|   |
  |
  |
You might also want to put these prototypes at the top of your main source file, or define them
in an include file or something, I was too lazy to do it for you. Sorry.
|   |
  |
 |
|   |
extern BOOL ss_RegistrySetup(HINSTANCE hinst,int section,int desc,int file);
extern int ss_GetRegistryInt(int name,int iDefault);
extern void ss_GetRegistryString(
   int name,
   LPTSTR lpDefault,
   LPTSTR lpDest,
   int bufSize
);
extern void ss_WriteRegistryInt(int name,int iVal);
extern void ss_WriteRegistryString(int name,LPTSTR lpString);
extern void ss_DeleteRegistryEntry(LPTSTR lpEntry);
extern void ss_RegistryShutdown(void);
|
  |
|   |
  |
  |
And there you so. Simple when you know how. Unless you're not me and it was
simple before.
|
  |