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.

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.            
}

Recursive Function. Iterates the passed Key to find out necessary values.

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)
            {
                ;
            }
        }

The above code works fine, but when I tried to convert it to C++, I'm pretty lost.
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.

RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Enum\\Bluetooth", 0, KEY_READ, &hKey)

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++?

As I am only beginning this thing a code sample with some explanations would be great.

probably this helps:
http://msdn.microsoft.com/en-us/library/ms724235(v=VS.85).aspx
You should however very sparingly use recursion as a means of looping.
Recursion can always be replaced by iteration which is much lighter on resources and also much easier to debug.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.