Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Writing to the registry

Posted on 1999-07-28
3
Medium Priority
?
318 Views
Last Modified: 2013-11-20
How do a I write something to the registry directly under HKEY_CURRENT_USER

Currently when I use SetRegistryKey it places the key under \Software\

HKEY_CURRENT_USER\Software\"it goes here"

HKEY_CURRENT_USER\"I want it here!"

Thanks,
-Eric
0
Comment
Question by:nielsew
[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
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
nil_dib earned 200 total points
ID: 1311753
this code creates a subkey under  HKEY_LOCAL_MACHINE\Software\MyApplication
and writes and reads a dword into/from that key.

ACL acl;
DWORD dwDisposition;
SECURITY_ATTRIBUTES     SecurityAttributes;
SECURITY_DESCRIPTOR     SecurityDescriptor;
HKEY        m_hKey;

// set security descripter
InitializeSecurityDescriptor(&SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&SecurityDescriptor, TRUE, NULL, FALSE);
SecurityAttributes.nLength = sizeof(SecurityAttributes);
SecurityAttributes.lpSecurityDescriptor = &SecurityDescriptor;
SecurityAttributes.bInheritHandle = FALSE;

// create a new key
if(RegCreateKeyEx ( hKey, "Software\\MyApplication", 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, &SecurityAttributes, &m_hKey, &dwDisposition) != ERROR_SUCCESS))
       return false;
// open the new key
if(RegOpenKey(hKey, "Software\\MyApplication", &m_hKey) != ERROR_SUCCESS))
       return false;

// write a DWORD value
DWORD dw = 120;
if(RegSetValueEx(m_hKey, "DWORD", NULL, REG_DWORD,
(LPBYTE)&dw, sizeof(dw)) != ERROR_SUCCESS)
       return false;

// read the DWORD value
DWORD dw = 0;
DWORD dwValue;
DWORD dwType = 0;
LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)"DWORD", NULL, REG_DWORD, NULL, &dw);
if (lRes == ERROR_SUCCESS)
{
    lRes = RegQueryValueEx(m_hKey, (LPTSTR)"DWORD", NULL, REG_DWORD, (LPBYTE)&dwValue, &dw);
    // dwValue = 120
}

nil_dib
0
 

Author Comment

by:nielsew
ID: 1311754
Thanks!
0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1311755
forgot two things ...
replace hKey
with HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER (in your case )....

replace the last lines with:

DWORD dwType = REG_DWORD;
LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)"DWORD", NULL, &dwType, NULL, &d6w);
if (lRes == ERROR_SUCCESS)
{
lRes = RegQueryValueEx(m_hKey, (LPTSTR)"DWORD", NULL, &dwType, (LPBYTE)&dwValue, &d6w);
...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

671 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