?
Solved

MFC using the Registry

Posted on 2009-04-14
7
Medium Priority
?
2,013 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 19

Assisted Solution

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

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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

770 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