简体   繁体   中英

How to extract hive and registry name from registry key

I have to extract hive from registry key and registry name so that I can open the key in regopen

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Firefox

I have to extract HKEY_LOCAL_MACHINE and Software\Mozilla\Firefox

Is there any API to extract them as wstring ?

wstring keyname = L"HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Firefox";

If I understand you correctly, you just want to split the string "HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\Firefox" into substrings "HKEY_LOCAL_MACHINE" and "SOFTWARE\Mozilla\Firefox" . There are no Win32 API functions for that exact purpose, but you can use the find() and substr() methods of C++'s std::wstring class, eg:

#include <string>

std::wstring keyname = ...; // "HKEY_LOCAL_MACHINE\\SOFTWARE\\Mozilla\\Firefox"

std::wstring::size_type pos = keyname.find(L'\\');
if (pos != std::wstring::npos)
{
    std::wstring root = keyname.substr(0, pos);
    std::wstring key = keyname.substr(pos+1);
    // use root and key as needed...
}

Alternative, you can use std::wistringstream and std::getline() :

#include <string>
#include <sstream>

std::wstring keyname = ...; // "HKEY_LOCAL_MACHINE\\SOFTWARE\\Mozilla\\Firefox"
std::wstring root, key;

std::wistringstream iss(keyname);
if (std::getline(iss, root, L'\\') && std::getline(iss, key))
{
    // use root and key as needed...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM