Click here to Skip to main content
14,641,852 members
Rate this:
Please Sign up or sign in to vote.
See more:
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.
Posted
Comments
Philippe Mori 22-Oct-11 16:49pm
   
Well the recommanded way is to do it at installation time as you already run with elevated priviledges. If you want to do it at run time, then you would typically have to start an helper application with elevated priviledges to do it or require the user to start the application as an administrator the first time the application is run (that last suggestion would be recommanded only if your user are typically "power" users).
Rate this:
Please Sign up or sign in to vote.

Solution 1

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%.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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; 
} 
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100