?
Solved

MFC using the Registry

Posted on 2009-04-14
7
Medium Priority
?
2,051 Views
Last Modified: 2013-11-20
I have tried several methods from CodeGuru, CodeProject, etc. and nothing works:  Problem: My "purchased Installer" for my programs places the User Info in the registry under HKEY_LOCAL_MACHINE. MFC in CWinApp only uses HKEY_CURRENT_USER.  How do I get CWinApp to change to HKEY_LOCAL_MACHINE?  I get a error when I try to use the following code in MFC:
HKEY hKey;
RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("MSS Inc\\Notifyktop"),
             0, KEY_QUERY_VALUE, &hKey);
unsigned char buffer[_MAX_PATH];
unsigned long datatype;
unsigned long bufferlength = sizeof(buffer);

RegQueryValueEx(hKey, "Name", NULL, &datatype, buffer, &bufferlength);
HKEY hKey;
unsigned long dwDisp;

RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("MSS Inc\\Notify"),
               0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
               NULL, &hKey, &dwDisp);

HKEY hKey;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("MSS Inc\\Notify"),
             0, KEY_QUERY_VALUE, &hKey);
unsigned char buffer[_MAX_PATH];
unsigned long datatype;
unsigned long bufferlength = sizeof(buffer);
 
RegQueryValueEx(hKey, "Name", NULL, &datatype, buffer, &bufferlength);
HKEY hKey;
unsigned long dwDisp;
 
RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("MSS Inc\\Notify"),
               0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
               NULL, &hKey, &dwDisp);

Open in new window

0
Comment
Question by:LarryMillsSr
7 Comments
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 300 total points
ID: 24137443
0
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 300 total points
ID: 24137941
There is an MFC wrapper class - CRegKey, have a look at the functionality that exposes.

ps. Vista doesn't like applications using anything but the HKEY_CURRENT_USER for actions other than reading of values.  What error do you get?
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 300 total points
ID: 24139591
(Wished I had known about CRegKey)

If CRegKey doesn't work out, there is always the API functions.

The main ones to understand would be RegCreateKeyEx, RegOpenKeyEx, RegGetValue and RegSetValueEx.

In concept, you open a registry key (starting with HKEY_CURRENT_USER (or use the function RegOpenCurrentUser).  To access sub keys, you first have to open them (get a handle to the subkey) with RegOpenKeyEx supplying the handle to the parent key and the name of the sub-key.  Once you've drilled down to the desired key, you read/write the value with RegGetValue/ResSetValueEx.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Accepted Solution

by:
DanRollins earned 600 total points
ID: 24143344
Each of thes registry function returns an error value.  For instance, use:
   LONG nResult= RegOpenKeyEx( HKEY_LOCAL_MACHINE, ... etc.
If nResult is non-zero, then it is an error code.  Please report the error codes that you are receiving.
0
 

Expert Comment

by:LarryAMillsSr
ID: 24144276
I tried "RegOpenKeyEx" and it returned INVALID_FUNCTION_ERROR. so naturally I don't know whether any of the other functions were okay or not.  I'm going to try the CRegister class to see if that works out.  I'll let all of you know.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 24144453
A result of 2 is  ERROR_FILE_NOT_FOUND meaning that you have misspelled or otherwise incorrectly entered the name of the subkey.  The way to solve this is as I described in your other question:  http:Q_24320847.html#a24144448
0
 

Author Closing Comment

by:LarryMillsSr
ID: 31569858
HooKooDooKu was DEAD-ON the error solution good job.
alb66 gave the correctly needed class and
AndyAinscow gave much needed info for future use for Vista.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

830 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