Solved

MFC using the Registry

Posted on 2009-04-14
7
1,971 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
Comment Utility
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
Comment Utility
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
Comment Utility
(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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now