Solved

MFC using the Registry

Posted on 2009-04-14
7
1,984 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 100 total points
ID: 24137443
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 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 100 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 49

Accepted Solution

by:
DanRollins earned 200 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 50
Windows Drag & Drop Location 2 100
Rewrite URL With Encoded Characters 6 78
fix34  challenge 9 121
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

810 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