注册表(HKLM)和编码 [英] Registry (HKLM) and coding

查看:210
本文介绍了注册表(HKLM)和编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为开发人员,我将使用C ++从注册表中获取信息以进行评估或试用.我想写到本地机器"区域,以便此信息对于给定计算机上的所有用户都是相同的.但是,如何以编程方式设置一个对所有用户均为读写的注册表项?在这种情况下,我无法选择在安装时进行设置.

如果无法做到这一点,我将不得不求助于HKCU,并且每个用户都会有一个新的试用版.

我们以前为用户提供评估许可证的软件程序是免费写入HKLM注册表的,甚至在IT锁定注册表访问HKLM的公司中也可以使用.

任何线索或建议将不胜感激.

解决方案

写给HKLM的操作仅限于提升的过程.
因此,您将不得不使用诸如安装之类的提升过程来进行设置.
设置密钥时,请提供适当的安全描述符,以便所有用户/进程都可以对其进行访问/修改.

另一个选择是使用文件系统并将其存储在%PROGRAMDATA%下.在dll上使用regserver32.但是请相信,这可以通过常规的c ++程序运行.希望对您有帮助!

HRESULT SetHKLMRegistryKeyAndValue(PCWSTR pszSubKey, PCWSTR pszValueName,  
    PCWSTR pszData) 
{ 
    HRESULT hr; 
    HKEY hKey = NULL; 
 
    // Creates the specified registry key. If the key already exists, the  
    // function opens it.  
    hr = HRESULT_FROM_WIN32(RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0,  
        NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL)); 
 
    if (SUCCEEDED(hr)) 
    { 
        if (pszData != NULL) 
        { 
            // Set the specified value of the key. 
            DWORD cbData = lstrlen(pszData) * sizeof(*pszData); 
            hr = HRESULT_FROM_WIN32(RegSetValueEx(hKey, pszValueName, 0,  
                REG_SZ, reinterpret_cast<const BYTE *>(pszData), cbData)); 
        } 
 
        RegCloseKey(hKey); 
    } 
 
    return hr; 
} 


As a developer, I will be using C++ to get information from the Registry for Evaluation or Trial purposes. I would like to write to the LOCAL MACHINE area so that this information will be the same for all users on a given computer. However, how do I programatically set up a Registry Key that is Read-Write for all users? In this case, I do not have the option of setting this up at installation time.

If this is not possible, I will have to resort to HKCU and each user will have a fresh trial.

A software program we previously to provide our users with evaluation licenses wrote freely to the HKLM registry, and it even worked at companies where the IT locked down registry access to the HKLM.

Any clues or suggestions will be greatly appreciated.

解决方案

Writing to HKLM is restricted to elevated processes only.
So you will have to set it up using an elevated process like an installation.
When setting up the key, provide the appropriate security descriptors so that it can be accessed/modifies by all users/processes.

Another option would be to use the file system and store it under %PROGRAMDATA%.


I have used the following piece of code for registering HKCR (this required admin privileges, not sure about HKLM) using regserver32 on a dll. But believe this can work from a regular c++ program. Hope it helps!

HRESULT SetHKLMRegistryKeyAndValue(PCWSTR pszSubKey, PCWSTR pszValueName,  
    PCWSTR pszData) 
{ 
    HRESULT hr; 
    HKEY hKey = NULL; 
 
    // Creates the specified registry key. If the key already exists, the  
    // function opens it.  
    hr = HRESULT_FROM_WIN32(RegCreateKeyEx(HKEY_LOCAL_MACHINE, pszSubKey, 0,  
        NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL)); 
 
    if (SUCCEEDED(hr)) 
    { 
        if (pszData != NULL) 
        { 
            // Set the specified value of the key. 
            DWORD cbData = lstrlen(pszData) * sizeof(*pszData); 
            hr = HRESULT_FROM_WIN32(RegSetValueEx(hKey, pszValueName, 0,  
                REG_SZ, reinterpret_cast<const BYTE *>(pszData), cbData)); 
        } 
 
        RegCloseKey(hKey); 
    } 
 
    return hr; 
} 


这篇关于注册表(HKLM)和编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆