The Wayback Machine - https://web.archive.org/web/20080617203054/http://www.codeguru.com:80/cpp/cpp/cpp_managed/nfc/article.php/c8859/

CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
Home Improvement
Dental Insurance
Computer Deals
Promotional Items
Hurricane Shutters
Find Software
Calling Cards
Compare Prices
Condos For Sale
Server Racks
Memory Upgrades
Laptop Batteries
Laptops
Televisions


RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> C++ >> Managed C++ >> .NET Framework Classes

Download: IBM Data Studio v1.1. Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

Managed C++: Read and Write Registry Keys and Values
Rating:

Tom Archer - MSFT (view profile)
December 20, 2004

Programmatically accessing the Windows Registry has always been a bit of a convoluted task. For this reason, a myriad C++ classes have been created over the years to enable easier and more intuitive access to the Registry from Windows applications. Thankfully, the task of reading and writing Registry keys and values is monumentally easier with the .NET framework.


(continued)



Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.

This article illustrates the following common Registry tasks (Figure 1 shows the mixed-mode MFC/.NET demo application supplied with this article):


(Full Size Image)

Figure 1: The attached demo illustrates the most common tasks that involve programmatically accessing and modifying the Windows Registry.

Note: To use the following code snippets, you must include the namespace Microsoft::Win32 in your code and, if you're writing a mixed-mode application, undefine MessageBox:
using namespace Microsoft::Win32;
#undef MessageBox

Enumerating a Registry Key's Subkeys

To understand how to enumerate a Registry key, consider the basic example of enumerating the Software key in the HKEY_CURRENT_USER (HKCU) hive. The following are the basic steps for accomplishing that task:

  1. Instantiate a RegistryKey object using the static Registry::CurrentUser value. Note that you can access all of the standard root keys (hives) in this manner. For example, you can access the HKEY_CLASSES_ROOT via Registry::ClassesRoot, the HKEY_LOCAL_MACHINE via the Registry::LocalMachine, and so on.
  2. Instantiate a RegistryKey object representing the Registry key you want to work with (the Software key, in this case) via a call to the root key's RegistryKey::GetSubKey method.
  3. Open the Registry key via a call to the RegistryKey::Open method.
  4. Retrieve the subkeys by calling the RegistryKey::GetSubKeyNames method. This method returns an array of subkey names (in the form of String objects).
  5. Iterate through the returned array, performing your own application-specific logic.
  6. Close any open RegistryKey objects via a call to the respective Close methods.
RegistryKey* currentUser;
RegistryKey* softwareKey;

try
{
  // Specify the HKEY_CURRENT_USER hive
  currentUser = Registry::CurrentUser;

  // Open the Software key
  softwareKey = currentUser->OpenSubKey(S"Software");

  // Request all subkeys from the Software key
  String* subkeys[] = softwareKey->GetSubKeyNames();

  // Enumerate the subkeys
  for (int i = 0; i < subkeys->Length; i++)
  {
    // Each subkey is now represented by subkeys[i]
  }
}
catch(Exception* ex)
{
  MessageBox::Show(ex->Message);
}
__finally
{
  if (softwareKey) softwareKey->Close();
  if (currentUser) currentUser->Close();
}

Enumerating a Registry Key's Values

The process for enumerating a Registry key's value is almost identical to enumerating keys with the following differences:

  • To obtain the array of Registry key value names, call the RegistryKey::GetValueNames method.
  • Each entry in the returned array is a String representing the value name. To obtain the actual value, you then pass that name to the RegistryKey::GetValue method:
Note: The RegistryKey::GetValue method returns a base Object that you then cast to the appropriate type. Therefore, you need to know exactly what type you are expecting because there is no programmatic way of determining the value's type as defined in the Registry (string, numeric, or binary value).
RegistryKey* currentUser;
RegistryKey* softwareKey;

try
{
  // Specify the HKEY_CURRENT_USER hive
  currentUser = Registry::CurrentUser;

  // Open the "Shell Folders" subkey
  softwareKey = currentUser->OpenSubKey(
    S"Software\\Microsoft\\Windows\\CurrentVersion\\"
    S"Explorer\\Shell Folders");

  // Request all values from the "Shell Folders" key
  String* valueNames[] = softwareKey->GetValueNames();

  // Enumerate the values
  for (int i = 0; i < valueNames->Length; i++)
  {
    // Each value name is now represented by valueNames[i]
    // The actual value is returned via the subkey's GetValue
    // method like this:
    String* value = static_cast<String*>(softwareKey->
                                         GetValue(valueNames[i]));
  }
}
catch(Exception* ex)
{
  MessageBox::Show(ex->Message);
}
__finally
{
  if (softwareKey) softwareKey->Close();
  if (currentUser) currentUser->Close();
}

Creating a New Registry Key and Value

To create a new Registry key, you simply need to call the RegistryKey::CreateSubKey method. This method returns a RegistryKey object representing the newly created key. You then can create that key's values via calls to the SetValue method. The following code snippet creates a new key under the HKCU/Software key called "My Product" and then adds values for Description and Version:

Note: The CreateSubKey method will not throw an exception if it fails due to the subkey already existing. Instead, the method simply returns a value of null.
RegistryKey* currentUser;
RegistryKey* softwareKey;

try
{
  RegistryKey* currentUser = Registry::CurrentUser;
  RegistryKey* softwareKey = currentUser->
                             CreateSubKey(S"Software\\My Product");

  softwareKey->SetValue(S"Description", S"Description of my product");
  softwareKey->SetValue(S"Version", S"1.42");
}
catch(Exception* ex)
{
  MessageBox::Show(ex->Message);
}
__finally
{
  if (softwareKey) softwareKey->Close();
  if (currentUser) currentUser->Close();
}

Modifying an Existing Registry Key and Value

This task is almost identical to creating values, with the only difference being that because the key already exists, you need to open the key (via RegistryKey::OpenSubKey) before calling the SetValue methods (The OpenSubKey method will return null if the key does not exist.):

RegistryKey* currentUser;
RegistryKey* softwareKey;

try
{
  RegistryKey* currentUser = Registry::CurrentUser;
  RegistryKey* softwareKey = currentUser->
                             OpenSubKey(S"Software\\My Product",
                                        true);

  if (softwareKey)
  {
    softwareKey->SetValue(S"Description", S"Description of my
                          product");
    softwareKey->SetValue(S"Version", S"1.42");
  }
}
catch(Exception* ex)
{
  MessageBox::Show(ex->Message);
}
__finally
{
  if (softwareKey) softwareKey->Close();
  if (currentUser) currentUser->Close();
}

About the Author
I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers. Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.

Downloads

  • RegistryOps.zip - Download source code - 58 Kb

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed

    Whitepaper: PHP and Service Component Architecture
    Intel Go Parallel Portal: Translating Multicore Power into Application Performance
    IBM developerWorks Article: Building SOA Solutions with the Service Component Architecture
    IBM developerWorks Article: Service Component Architecture Client and Implementation Model for Java
    Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.


  • RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

    (You must be signed in to rank an article. Not a member? Click here to register)

    Latest Comments:
    #using - jov0708 (02/08/2008)
    how can we do in VC++6.0? - Jakkani ShivaPrasad (06/26/2005)

    View All Comments
    Add a Comment:
    Title:
    Comment:
    Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



    (You must be signed in to comment on an article. Not a member? Click here to register)


    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
    Intel Go Parallel Article: Getting Started with TBB on Windows
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Avaya Article: How to Feed Data into the Avaya Event Processor
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    IBM Article: Developing a Software Policy for Your Organization
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    Intel Go Parallel Article: Intel Threading Tools and OpenMP
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Win a Lenovo ThinkPad X300 Notebook in the Intel Resource Center Scavenger Hunt
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Enterprise Search--Do You Know What's Out There?
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES