CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2008
    Posts
    48

    Converting to C++ from C#

    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.
    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.            
    }
    Recursive Function. Iterates the passed Key to find out necessary values.
    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)
                {
                    ;
                }
            }
    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.

    Code:
    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.

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Converting to C++ from C#

    RegEnumKeyEx enumerates the subkeys of the specified open registry key.
    Note that you can find all registry functions in MSDN Library.
    Also, you can find examples, including how to enumerate registry subkeys.
    See MSDN: Using the Registry
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured