转换为 C++来自 C#
我几乎所有的编程都是使用 C# 完成的,对于 C++ 来说我还是个新手。但是现在我必须转换为 C++,并且发现它有点困难。例如,我使用 C# 编写了一个非常简单的程序来获取注册表项,然后使用递归函数迭代注册表项以查找特定项,然后获取我想要的值。没问题,我可以使用 C# 在 10 分钟内编写该程序。这是代码。
我的主要职能。它获取蓝牙注册表项,然后调用递归函数。
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.
}
递归函数。迭代传递的 Key 以找出必要的值。
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)
{
;
}
}
上面的代码工作正常,但当我尝试将其转换为 C++ 时,我很迷失。 注意:我正在使用 Win32 控制台 C++ 程序。
我发现我可以执行如下操作来获取蓝牙注册表项。
RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Enum\\Bluetooth", 0, KEY_READ, &hKey)
但在那之后,我对递归函数非常迷失。特别是,当我不知道子项名称时,如何获取所传递的注册表项的可用子项?或者简而言之,C++ 中的RegistryKey.GetSubKeyNames() 的等效行为是什么?
由于我才刚刚开始这件事,因此带有一些解释的代码示例会很棒。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过循环调用
RegEnumKeyEx
获取所有子键,直到返回ERROR_NO_MORE_ITEMS
。以完全相同的方式,您可以通过循环调用
RegEnumValue
获取所有值,直到返回ERROR_NO_MORE_ITEMS
。You get all subkeys by calling
RegEnumKeyEx
in a loop until it returnsERROR_NO_MORE_ITEMS
.In the exact same way, you get all values by calling
RegEnumValue
in a loop until it returnsERROR_NO_MORE_ITEMS
.我假设您想从 .NET 过渡到本机 C++ 编程。 (即没有 CLI 和 .NET 框架,如果您启用了托管 C++ 编译,您仍然可以使用它们)。
如果您在 C# 领域花费了大量时间,您可能非常习惯大量非常方便的类,几乎可以完成所有可以想象到的事情,您所要做的就是点击“.”。并让 Intellisense 列出方法。好吧...您可以忘记所有这些便利:)
C++ 中没有这样的(至少不完整的)框架,所以您经常不得不求助于 Win32 API。 MSDN 库是您的朋友。如果您想精通 C++,请学习如何阅读它并学习如何查找内容(不仅仅是通过名称,还要了解不同类别的位置)。在这种情况下,如果您搜索找到的函数,您将找到一整套在注册表上运行的函数。所以现在,查看 MSDN 库中同一类别的其他方法,您可以找到 RegEnumKeyEx。 (提示:确保将 MSDN 库 UI 切换到经典视图,这样可以更轻松地在主题之间导航。我不知道 MS 对他们的“新”外观和感觉有何想法)
当您开始使用 Win32 API 时,您将意识到这是多么痛苦,尤其是来自 C#。但您不必直接使用它(或至少不必每次都使用)。您可以使用其他库,例如 ATL 为您提供了 CRegKey 类,这使得注册表的使用更加简单。如果找不到该类,请执行 C++ 最擅长的操作,编写自己的类。不断地直接使用 Windows 函数将使您的代码变得非常非常长并且难以阅读。
当您进入 C++ 时,另外两个值得了解的库是 STL(必须)和 Boost(强烈应该)。 Boost 特别具有大量操作系统抽象,因此您不必每次都直接访问 Windows DLL。
I'm assuming you want to transition from .NET to native C++ programming. (i.e. no CLI and no .NET framework, which you could still use if you enabled managed C++ compilation).
If you spent a ton of time in C# land, you are probably very used to a ton of very convenient classes for just about everything imaginable and all you have to do is hit "." and let the Intellisense list the methods. Well.... you can forget all those conveniences :)
There is no such (at least not complete) framework in C++ so often you have to turn to Win32 API. MSDN Library is YOUR FRIEND. If you want to get good at C++, learn how to read it and learn how to look things up (not just by name, but learn where different categories are). In this case, if you search for the function you found, you will find a whole set of functions that work on registry. So now, looking at other methods in the same category in MSDN library, you can find RegEnumKeyEx. (hint: make sure to switch MSDN library UI to classical view, that makes it much easier to navigate between topics. I don't know what MS was thinking with their "new" look and feel)
As you start using Win32 API you will realize what a pain it is, especially coming from C#. But you don't have to use it directly (or at least not every time). You can use other libraries, for example ATL provides you with CRegKey class which makes working with registry much simpler. If you can't find the class, do what C++ does best, write your own class. Constantly working directly with windows functions will make your code very, very long and a pain to read.
Another 2 libraries worth knowing as you get into C++ are STL (a must) and Boost (strong should). Boost especially has a lot of OS abstraction so you don't have to go directly to windows DLLs every time.
如果您搜索有关 RegOpenKeyEx 的 MSDN 帮助,然后向上查找内容,您将找到所有相关方法:注册表方法
您可能想要使用 RegEnumKeyEx 枚举子项。
If you search for the MSDN help on RegOpenKeyEx and then go up in the contents you'll find all the related methods: Registry methods
You probably want to use RegEnumKeyEx to enumerate subkeys.