• Advertisement
Sign in to follow this  

C++ .NET and Windows Registry

This topic is 4614 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
*bump* Can I at least get a hint on how to do strings in C++\CLI code? The MSDN example doesnt even compile.

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
Quote:
void RetrieveKeys(RegistryKey^ rKey)
{
//Retrieve all subkeys for given key
System::String^ directory = rKey->GetValue("InstallPath")->ToString();
MessageBox::Show(directory);
}


Ok, so I have it getting the correct key information now, but how would I go about sending that string (directory) to a textbox? Keep in mind, im using C++\CLI code

Share this post


Link to post
Share on other sites
Anybody got a thought? Im really getting into this program, I dont wanna have to stop cause of lack of knowledge.

Share this post


Link to post
Share on other sites
Well, I set tWoWDir to public, and when going to access the text field, I do

Quote:

Form1::tWoWDir


And thats as far as I can go with intellisense. And I get an error when I try to do

Quote:

Form1::tWoWDir::Text = directory;

.\WarMiner.cpp(17) : error C3083: 'tWoWDir': the symbol to the left of a '::' must be a type
.\WarMiner.cpp(17) : error C2597: illegal reference to non-static member 'System::Windows::Forms::Form::Text'

or
Quote:

Form1::tWoWDir->Text = directory;

.\WarMiner.cpp(17) : error C2227: left of '->Text' must point to class/struct/union/generic type

Share this post


Link to post
Share on other sites
tWoWDir a an instance variable, in order to access it you must access it through an instance of Form1:

Form1^ f = new Form1();
f->tWowDir = "Rawr";


If you already have an instance created (I would assume as much, but hey, you never know) then use that instance instead of creating a new one.

Share this post


Link to post
Share on other sites
Ok, thank you SO much for that. I got alittle progress, and I will continue working on this as well, but here is the issue.

It compiled with this code

Quote:

void RetrieveKeys()
{
RegistryKey^ localMachine = Registry::LocalMachine;
RegistryKey^ software = localMachine->OpenSubKey("SOFTWARE\\Blizzard Entertainment\\World of Warcraft");
System::String^ directory = software->GetValue("InstallPath")->ToString();
fWarMiner^ mainForm = gcnew fWarMiner();
mainForm->tWoWDir->Text = directory;
}

int main(array<System::String ^> ^args)
{
// Grabbing registry value for World of Warcraft Directory
RetrieveKeys();

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

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


But, the actual text of tWoWDir was still blank (though I know im getting the value of the registry key, because I had a messagebox set to pop up with the value of directory in it. Now, is it a problem that im doing the line

Quote:
fWarMiner^ mainForm = gcnew fWarMiner();


then running

Quote:
Application::Run(gcnew fWarMiner());


Which seems to be making a new form

Share this post


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

  • Advertisement