How to write your own registry accessing functions by using the base class libraries in Visual C++ .NET or in Visual C++ 2005 (816162)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
- Microsoft .NET Framework 1.1
- Microsoft .NET Framework 1.0
Important This article contains information about modifying the registry.
Before you modify the registry, make sure to back it up and make sure that you
understand how to restore the registry if a problem occurs. For information
about how to back up, restore, and edit the registry, click the following
article number to view the article in the Microsoft Knowledge Base: 256986 Description of the Microsoft Windows Registry For a Microsoft Visual Basic .NET version of this
article, see
316151. For a Microsoft Visual C# .NET version of this
article, see
816163. This article refers to the following
Microsoft .NET Framework Class Library namespaces:
- Microsoft::Win32
- System::Diagnostics
IN THIS TASK SUMMARYThis article describes how to use Microsoft Visual C++. NET or Microsoft Visual C++ 2005 to
access the registry. To do this, write your own registry accessing
functions by using the Base Class Libraries. back to the topUsing the Registry Class and the RegistryKey ClassIn the Microsoft .NET Framework, the Registry class and the RegistryKey class provide control to the registry. The definitions of these classes exist in
the Microsoft.Win32 namespace and in the mscorlib.dll assembly. You must use the namespace and the assembly before
you use these classes. To access the registry without any constraints,
use the Registry class and the RegistryKey class in the Microsoft.Win32 namespace. The Registry class has seven field members
that give you access to a particular area of the registry. It is similar to
opening a key in the registry. All these members return a pointer to
the RegistryKey class.
ClassesRoot | The Windows Registry base key
HKEY_CLASSES_ROOT. | CurrentConfig | The Windows Registry base key
HKEY_CURRENT_CONFIG. | CurrentUser | The Windows Registry base key
HKEY_CURRENT_USER. | DynData | The Windows Registry base key
HKEY_DYN_DATA. | LocalMachine | The Windows Registry base key
HKEY_LOCAL_MACHINE. | PerformanceData | The Windows Registry base key
HKEY_PERFORMANCE_DATA. | Users | The Windows Registry base key HKEY_USERS. | back to the
topCreate a Visual C++ .NET or Visual C++ 2005
ApplicationCreate a
console application project to read and to write to the registry by using the Registry class and
the RegistryKey class. To do this, follow these steps: - Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- On the File menu, point to New, and then click Project.
- In Microsoft Visual Studio .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Application under Templates.
In Microsoft Visual Studio .NET 2003, click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates.
In Visual Studio 2005, click Visual C++ under Project Types, and then click CLR Console Application under Templates. - In the Name text box, type
Registry, and then click OK.
- Open the Registry.cpp file, and then add the following lines of code just below the #include
directives:
using namespace System;
using namespace System::Diagnostics;
using namespace Microsoft::Win32;
- Add the following code under the using statements just
above the _tmain() function.
Note In Visual Studio 2005, the function is the main() function.void WriteRegistry(RegistryKey __gc* ParentKey , String __gc* SubKey , String __gc* ValueName , Object *Value )
{
RegistryKey __gc* Key;
try
{
//Open the registry key.
Key = ParentKey->OpenSubKey(SubKey,true);
if (!Key) //if the key does not exist.
{
//Create the Subkey
Key = ParentKey->CreateSubKey(SubKey);
}
//Set the value.
Key->SetValue(ValueName, Value);
Console::WriteLine(S"Value:{0} for {1} is successfully written.", Value, ValueName);
}
catch (Exception *e)
{
Console::WriteLine(S"Error occurs in WriteRegistry", e->Message);
}
}
void ReadRegistry( RegistryKey __gc* ParentKey , String __gc* SubKey, String __gc* ValueName , Object *Value)
{
RegistryKey __gc* Key;
try
{
//Open the registry key.
Key = ParentKey->OpenSubKey(SubKey, true);
if (!Key) //if the key does not exist
{
throw new Exception(S"The registry key does not exist");
}
//Get the value.
Value = Key->GetValue(ValueName);
Console::Write(S"Value:{0} for {1} is successfully retrieved.", Value, ValueName);
}
catch( Exception *e)
{
Console::Write(S"Error occurs in ReadRegistry", e->Message );
}
} Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample.
To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
- Replace the int _tmain()
function that is generated by Visual C++ .NET with the following code.
Note In Visual Studio 2005, the function is the main() function.int _tmain(int argc, _TCHAR* argv[])
{
Int32 i = 123;
System::Object* obj = __box(i);
WriteRegistry(Registry::CurrentUser, S"Software\\MySoftware", S"Count", obj);
Object * Value;
ReadRegistry(Registry::CurrentUser, S"Software\\MySoftware", S"Count", Value);
Console::ReadLine();
}
- Press F5 to run the application.
Note This procedure creates a registry key that is named MySoftware under the HKEY_CURRENT_USER\Software subkey, and creates a DWORD value that is named Count under the MySoftware key. Count has a value of 123. back to the topREFERENCESFor additional information about the classes and the
namespaces that are used in this article, visit the following Microsoft Web
sites: back to the
top
Modification Type: | Major | Last Reviewed: | 1/5/2006 |
---|
Keywords: | kbRegistry kbHOWTOmaster KB816162 kbAudDeveloper kbAudITPRO |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|