C++ .NET and Windows Registry

Started by
14 comments, last by PyroBlizzard 18 years, 10 months ago
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.
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?
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
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();

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 ==========
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?
*bump* Anybody got thoughts on my errors?
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?
search MSDN for the compiler error codes, like C3699
hint: it has something to do with * and line number 12 ;)
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
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]
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?
*bump* Can I at least get a hint on how to do strings in C++\CLI code? The MSDN example doesnt even compile.
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?
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( );
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.
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?

This topic is closed to new replies.

Advertisement