Microsoft KB Archive/816162

= How to write your own registry accessing functions by using the base class libraries in Visual C++ .NET or in Visual C++ 2005 =

Article ID: 816162

Article Last Modified on 5/21/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * 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

 * SUMMARY
 * Using the Registry Class and the RegistryKey Class
 * Create a Visual C++ .NET or Visual C++ 2005 Application
 * REFERENCES



SUMMARY
This 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 top

Using the Registry Class and the RegistryKey Class
In 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.

back to the top

Create a Visual C++ .NET or Visual C++ 2005 Application
Create 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&quot;Value:{0} for {1} is successfully written.&quot;, Value, ValueName); }   catch (Exception *e) {       Console::WriteLine(S&quot;Error occurs in WriteRegistry&quot;, 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&quot;The registry key does not exist&quot;); }

//Get the value. Value = Key->GetValue(ValueName);

Console::Write(S&quot;Value:{0} for {1} is successfully retrieved.&quot;, Value, ValueName); }   catch( Exception *e) {       Console::Write(S&quot;Error occurs in ReadRegistry&quot;, 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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li>  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&quot;Software\\MySoftware&quot;, S&quot;Count&quot;, obj);

Object * Value; ReadRegistry(Registry::CurrentUser, S&quot;Software\\MySoftware&quot;, S&quot;Count&quot;, Value);

Console::ReadLine; } </li> Press F5 to run the application.</li></ol>

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 top

<div class="references_section">