Jump to content
  • Advertisement
Sign in to follow this  
PyroBlizzard

C++ .NET and Windows Registry

This topic is 4886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anybody think they can tell me how to do it? I need it to read a registry key from the HKEY_LOCAL_MACHINE set on startup of the program. This is also a GUI based program, so I need it to execute probably before even the Application::EnableVisualstyles() code is executed.

Share this post


Link to post
Share on other sites
Advertisement
Lo,

I searched on google, and there's two classes of importance to you - Registry and RegistryKey, both in the Microsoft.Win32 namespace. I'd search on MSDN for these, it'll be able to give much more detailed documentation than I ever could.

CloudNine

Share this post


Link to post
Share on other sites
See the documentation for the Microsoft.Win32.Registry and Microsoft.Win32.RegistryKey classes. Its all pretty straight forward. It would look something like (in C# anyway):

RegistryKey key = Registry.LocalMachine.OpenKey("KeyPath1\\KeyPath2\\KeyPath3");
string value = key.GetValue(""); \\get the key's default value
key.Close();

Share this post


Link to post
Share on other sites
When doing a quick test to see if it works, I always get errors. Here is my current code.

Quote:
// WarMiner.cpp : main project file.

#include "stdafx.h"
#include "WarMiner.h"

using namespace WarMiner;
using namespace System;
using namespace Microsoft::Win32;

[STAThreadAttribute]

void RetrieveKeys(RegistryKey* rKey)
{
//Retrieve all subkeys for given key
String* names[] = rKey->GetSubKeyNames();
MessageBox::Show("Success");
}

int main(array<System::String ^> ^args)
{
// Grabbing registry value for Directory
RegistryKey* localMachine = Registry::LocalMachine;
RetriveKeys(localMachine);

// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualstyles();

// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}


And here is the error log
Quote:

------ Build started: Project: WarMiner, Configuration: Debug Win32 ------
Compiling...
WarMiner.cpp
.\WarMiner.cpp(12) : error C3699: '*' : cannot use this indirection on type 'Microsoft::Win32::RegistryKey'
compiler replacing '*' with '^' to continue parsing
.\WarMiner.cpp(15) : error C3699: '*' : cannot use this indirection on type 'System::String'
compiler replacing '*' with '^' to continue parsing
.\WarMiner.cpp(15) : error C2728: 'System::String ^' : a native array cannot contain this managed type
Did you mean 'array<System::String ^>'?
.\WarMiner.cpp(15) : error C2440: 'initializing' : cannot convert from 'cli::array<Type,dimension> ^' to 'System::String ^[]'
with
[
Type=System::String ^,
dimension=1
]
.\WarMiner.cpp(22) : error C3699: '*' : cannot use this indirection on type 'Microsoft::Win32::RegistryKey'
compiler replacing '*' with '^' to continue parsing
.\WarMiner.cpp(23) : error C3861: 'RetriveKeys': identifier not found
Build log was saved at "file://c:\Documents and Settings\Eric Musgrove\My Documents\Visual Studio 2005\Projects\WarMiner\WarMiner\Debug\BuildLog.htm"
WarMiner - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
search MSDN for the compiler error codes, like C3699
hint: it has something to do with * and line number 12 ;)

Share this post


Link to post
Share on other sites
Alright, I got that, but i've run into another error now, on line 15, with my string

Quote:
System::String^ names[] = rKey->GetSubKeyNames();


Quote:

------ Build started: Project: WarMiner, Configuration: Debug Win32 ------
Compiling...
WarMiner.cpp
.\WarMiner.cpp(15) : error C2728: 'System::String ^' : a native array cannot contain this managed type
Did you mean 'array<System::String ^>'?
.\WarMiner.cpp(15) : error C2440: 'initializing' : cannot convert from 'cli::array<Type,dimension> ^' to 'System::String ^[]'
with
[
Type=System::String ^,
dimension=1
]
Build log was saved at "file://c:\Documents and Settings\Eric Musgrove\My Documents\Visual Studio 2005\Projects\WarMiner\WarMiner\Debug\BuildLog.htm"
WarMiner - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


[Edited by - PyroBlizzard on June 1, 2005 9:56:02 PM]

Share this post


Link to post
Share on other sites
I'd suggest reading up on C++/CLI as it's syntax is different to the original managed C++ (which will be used in MSDN examples).

That error is caused because you're declaring an unmanaged array, a managed array will be done something like:


array<System::String^>^ names = rKey->GetSubKeyNames( );

Share this post


Link to post
Share on other sites
Aight, thnx. Anywhere you can recommend that I can start reading up on CLI syntax? Since im still somewhat new to C++, it would be best to start my learning over with the new syntax, instead of the old.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!