Jump to content

win32 registration key commands


Josh
 Share

Recommended Posts

bool GetRegKey(const WString& name, WString& value)
    {
        auto sarr = name.Split("\\");
        if (sarr.size() <= 1) return false;

        WString file = sarr[sarr.size() - 1];
        WString dir = name.Left(name.size() - 1 - file.size());

        HKEY root;
        if (sarr[0] == "HKEY_CLASSES_ROOT")
        {
            root = HKEY_LOCAL_MACHINE;
        }
        else if (sarr[0] == "HKEY_CURRENT_CONFIG")
        {
            root = HKEY_CURRENT_CONFIG;
        }
        else if (sarr[0] == "HKEY_CURRENT_USER")
        {
            root = HKEY_CURRENT_USER;
        }
        else if (sarr[0] == "HKEY_LOCAL_MACHINE")
        {
            root = HKEY_LOCAL_MACHINE;
        }
        else if (sarr[0] == "HKEY_USERS")
        {
            root = HKEY_USERS;
        }
        else
        {
            return false;
        }

        dir = dir.Right(dir.GetSize() - sarr[0].GetSize() - 1);

        HKEY hKey = NULL;
        auto res = RegOpenKeyExW(root, dir.c_str(), 0, KEY_READ, &hKey);
        if (res != ERROR_SUCCESS) return false;

        WCHAR szBuffer[2048];
        DWORD dwBufferSize = sizeof(szBuffer);

        DWORD type = REG_LINK;
        res = RegQueryValueExW(hKey, file.c_str(), 0, &type, (LPBYTE)szBuffer, &dwBufferSize);
        RegCloseKey(hKey);

        if (res != ERROR_SUCCESS) return false;

        value = wstring(szBuffer);

        return true;
    }

    bool GetRegKey(const WString& name, uint32_t& value)
    {
        auto sarr = name.Split("\\");
        if (sarr.size() <= 1) return false;

        WString file = sarr[sarr.size() - 1];
        WString dir = name.Left(name.size() - 1 - file.size());

        HKEY root;
        if (sarr[0] == "HKEY_CLASSES_ROOT")
        {
            root = HKEY_LOCAL_MACHINE;
        }
        else if (sarr[0] == "HKEY_CURRENT_CONFIG")
        {
            root = HKEY_CURRENT_CONFIG;
        }
        else if (sarr[0] == "HKEY_CURRENT_USER")
        {
            root = HKEY_CURRENT_USER;
        }
        else if (sarr[0] == "HKEY_LOCAL_MACHINE")
        {
            root = HKEY_LOCAL_MACHINE;
        }
        else if (sarr[0] == "HKEY_USERS")
        {
            root = HKEY_USERS;
        }
        else
        {
            return false;
        }

        dir = dir.Right(dir.GetSize() - sarr[0].GetSize() - 1);

        HKEY hKey = NULL;
        auto res = RegOpenKeyExW(root, dir.c_str(), 0, KEY_READ, &hKey);
        if (res != ERROR_SUCCESS) return false;

        DWORD result;
        DWORD dwBufferSize = sizeof(result);

        DWORD type = REG_DWORD;
        res = RegQueryValueExW(hKey, file.c_str(), 0, &type, (LPBYTE)&result, &dwBufferSize);
        RegCloseKey(hKey);

        if (res != ERROR_SUCCESS) return false;

        value = result;

        return true;
    }

    bool GetRegKey(const WString& name, uint64_t& value)
    {
        auto sarr = name.Split("\\");
        if (sarr.size() <= 1) return false;

        WString file = sarr[sarr.size() - 1];
        WString dir = name.Left(name.size() - 1 - file.size());

        HKEY root;
        if (sarr[0] == "HKEY_CLASSES_ROOT")
        {
            root = HKEY_LOCAL_MACHINE;
        }
        else if (sarr[0] == "HKEY_CURRENT_CONFIG")
        {
            root = HKEY_CURRENT_CONFIG;
        }
        else if (sarr[0] == "HKEY_CURRENT_USER")
        {
            root = HKEY_CURRENT_USER;
        }
        else if (sarr[0] == "HKEY_LOCAL_MACHINE")
        {
            root = HKEY_LOCAL_MACHINE;
        }
        else if (sarr[0] == "HKEY_USERS")
        {
            root = HKEY_USERS;
        }
        else
        {
            return false;
        }

        dir = dir.Right(dir.GetSize() - sarr[0].GetSize() - 1);

        HKEY hKey = NULL;
        auto res = RegOpenKeyExW(root, dir.c_str(), 0, KEY_READ, &hKey);
        if (res != ERROR_SUCCESS) return false;

        uint64_t result;
        DWORD dwBufferSize = sizeof(result);

        DWORD type = REG_QWORD;
        res = RegQueryValueExW(hKey, file.c_str(), 0, &type, (LPBYTE)&result, &dwBufferSize);
        RegCloseKey(hKey);

        if (res != ERROR_SUCCESS) return false;

        value = result;

        return true;
    }

 

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...