I have done pretty much all my programming using C# and very much a newbie to C++. However now I have to convert to C++ and is finding it a bit difficult. For example, I wrote a pretty simple program using C# to acquire a RegistryKey, then using a recursive function I iterate through my registry key to find a specific key and then get the values I want. No problem, I can write that program in 10 minutes using C#. Here is the code.
My primary function. It gets Bluetooth Registry Key and then call the recursive function.
Recursive Function. Iterates the passed Key to find out necessary values.Code:private static void CheckOpenComPorts() { RegistryKey blueToothPorts = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\Bluetooth"); List<string> foundPorts = new List<string>(); AddFoundPortsToList(blueToothPorts, ref foundPorts); //Rest of the program; not relevant here. }
The above code works fine, but when I tried to convert it to C++, I'm pretty lost.Code:private static void AddFoundPortsToList(RegistryKey regKey, ref List<string> ports) { try { string[] subKeys = regKey.GetSubKeyNames(); if (subKeys != null) { foreach (string subKey in subKeys) { AddFoundPortsToList(regKey.OpenSubKey(subKey), ref ports); } } if (regKey.Name.EndsWith("Device Parameters")) { string str = System.Convert.ToString(regKey.GetValue("PortName")); if (String.IsNullOrEmpty(str) == false) { ports.Add(str); } } } catch (System.Security.SecurityException ex) { ; } }
Note : I'm using a Win32 Console C++ Program.
I figured out that I can do something like the following to get the Bluetooth Registry Key.
But after that, I'm pretty lost about the recursive function. Specially, how do I get the available subkeys of the passed registry key when I do NOT know the subkey names?. Or in short, what is the equivalent behavior of RegistryKey.GetSubKeyNames() in C++?Code:RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Enum\\Bluetooth", 0, KEY_READ, &hKey)
As I am only beginning this thing a code sample with some explanations would be great.


Reply With Quote
Ovidiu Cucu

Bookmarks