Solved

MFC using the Registry

Posted on 2009-04-14
7
2,001 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 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
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 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

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!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

707 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