Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / MFC
Article

CHKLM v1.1 - Registry Access

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
3 Mar 2000 90.1K   945   18   13
A Freeware MFC class to write to HKEY_LOCAL_MACHINE.
  • Download source files - 11 Kb
  • Introduction

    Welcome to CHKLM v1.0, a simple MFC class to allow you to write values to the HKEY_LOCAL_MACHINE registry key. The functions provided with MFC only allow values to be written to HKEY_CURRENT_USER.

    Full source code is provided for those interested. A VC 5 workspace file is included which builds a simple demo console application which calls the class methods.


    Features
    History
    API
    Contacting the Author


    Features

    • Simple and clean C++ interface based upon the MFC functions.
    • The classes are fully Unicode compliant and include Unicode built options in the workspace file.
    • Full documentation is provided in the form of a HTML file.


    History

    V1.0 (27th June 1998)
    • Initial public release.

    V1.01 (14 July 1999)

    • Now ships with a VC 5 workspace instead of VC 6.
    • Tidied up the documentation file.
    • Unicode Build configurations now actually compile and work <g>.

    V1.1 (3 October 1999)

    • Addition of GetProfileStringArray() and WriteProfileStringArray() methods.


    API

    The API consists of the public methods of the CHKLM class. They consist of:

    CHKLM::SetRegistryKey
    CHKLM::WriteProfileString
    CHKLM::GetProfileString
    CHKLM::WriteProfileInt
    CHKLM::GetProfileInt
    CHKLM::GetProfileBinary
    CHKLM::WriteProfileBinary
    CHKLM::GetProfileStringArray
    CHKLM::WriteProfileStringArray


    CHKLM::SetRegistryKey

    void SetRegistryKey(LPCTSTR lpszRegistryKey);

    Parameters:

    • lpszRegistryKey -- Pointer to a string containing the name of the key.
    • nIDRegistryKey -- ID/index of a key in the registry.

    Remarks:
    This function sets m_sRegistryKey, which is then used by the member functions of CHKLM. The registry key is usually the name of a company. It is stored in a key of the following form:

    HKEY_LOCAL_MACHINE\Software\<company
           name>\<application name>\<section name>\<value name>
    .


    CHKLM::GetProfileInt

    UINT GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);

    Return Value:
    The integer value of the string that follows the specified entry if the function is successful. The return value is the value of the nDefault parameter if the function does not find the entry. The return value is 0 if the value that corresponds to the specified entry is not an integer.

    When you retrieve a signed integer, you should cast the value into an int.

    Parameters:

    • lpszSection -- Points to a null-terminated string that specifies the section containing the entry.
    • lpszEntry -- Points to a null-terminated string that contains the entry whose value is to be retrieved.
    • nDefault -- Specifies the default value to return if the framework cannot find the entry. This value can be an unsigned value in the range 0 through 65,535 or a signed value in the range -32,768 through 32,767.

    Remarks:
    Call this member function to retrieve the value of an integer from an entry within a specified section of the application's HKLM part of the registry.

    This member function is not case sensitive, so the strings in the lpszSection and lpszEntry parameters may differ in case.


    CHKLM::WriteProfileInt

    BOOL WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);

    Return Value:
    Nonzero if successful; otherwise 0.

    Parameters:

    • lpszSection -- Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
    • lpszEntry -- Points to a null-terminated string that contains the entry whose value is to be written. If the entry does not exist in the specified section, it is created.
    • nValue -- Contains the value to be written.

    Remarks:
    Call this member function to write the specified value into the specified section of the application's HKLM part of the registry.


    CHKLM::GetProfileString

    CString GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL);

    Return Value:
    The return value is the string from the application's HKLM part of the registry or lpszDefault if the string cannot be found. The maximum string length supported by the framework is _MAX_PATH. If lpszDefault is NULL, the return value is an empty string.

    Parameters:

    • lpszSection -- Points to a null-terminated string that specifies the section containing the entry.
    • lpszEntry -- Points to a null-terminated string that contains the entry whose string is to be retrieved. This value must not be NULL.
    • lpszDefault -- Points to the default string value for the given entry if the entry cannot be found in the initialization file.

    Remarks:
    Call this member function to retrieve the string associated with an entry within the specified section in the application's HKLM part of the registry.


    CHKLM::WriteProfileString

    BOOL WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue);

    Return Value:
    Nonzero if successful; otherwise 0.

    Parameters:

    • lpszSection -- Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
    • lpszEntry -- Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created.
    • lpszValue -- Points to the string to be written. If this parameter is NULL, the entry specified by the lpszEntry parameter is deleted.

    Remarks:
    Call this member function to write the specified string into the specified section of the application's HKLM part of the registry.


    CHKLM::GetProfileBinary

    BOOL GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPBYTE* ppData, UINT* pBytes);

    Return Value:
    Nonzero if successful; otherwise 0.

    Parameters:

    • lpszSection -- Points to a null-terminated string that specifies the section containing the entry.
    • lpszEntry -- Points to a null-terminated string that contains the entry whose binary entry is to be retrieved. This value must not be NULL.
    • ppData -- Upon successful return this will contain the binary entry.
    • pBytes -- Upon successful return this will contain the size of the binary entry.

    Remarks:
    Call this member function to retrieve a binary entry associated with an entry within the specified section in the application's HKLM part of the registry. The user is responsible for deleting the ppData memory.


    CHKLM::WriteProfileBinary

    BOOL WriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes);

    Return Value:
    Nonzero if successful; otherwise 0.

    Parameters:

    • lpszSection -- Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
    • lpszEntry -- Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created.
    • pData -- Points to the binary data to be written.
    • nBytes -- The size of the binary data pointed to by pData.

    Remarks:
    Call this member function to write the specified binary value into the specified section of the application's HKLM part of the registry.


    CHKLM::GetProfileStringArray

    BOOL GetProfileStringArray(LPCTSTR lpszSection, LPCTSTR lpszEntry, CStringArray& array);

    Return Value:
    Nonzero if successful; otherwise 0.

    Parameters:

    • lpszSection -- Points to a null-terminated string that specifies the section containing the entry.
    • lpszEntry -- Points to a null-terminated string that contains the entry whose string array is to be retrieved. This value must not be NULL.
    • array -- Upon successful return this will contain the CStringArray.

    Remarks:
    Call this member function to retrieve a string array associated with an entry within the specified section in the registry. The value is stored as a MULTI_SZ string in the registry.


    CHKLM::WriteProfileStringArray

    BOOL WriteProfileStringArray(LPCTSTR lpszSection, LPCTSTR lpszEntry, const CStringArray& array);

    Return Value:
    Nonzero if successful; otherwise FALSE.

    Parameters:

    • lpszSection -- Points to a null-terminated string that specifies the section containing the entry. If the section does not exist, it is created. The name of the section is case independent; the string may be any combination of uppercase and lowercase letters.
    • lpszEntry -- Points to a null-terminated string that contains the entry into which the value is to be written. If the entry does not exist in the specified section, it is created.
    • array -- The CStringArray to be written.

    Remarks:
    Call this member function to write the specified string array into the registry. The value will be stored as a MULTI_SZ string in the registry.



    Contacting the Author

    PJ Naughter
    Email: pjn@indigo.ie
    Web: http://www.naughter.com
    3 October 1999


    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralMasked Value Data Pin
    sbuonocore19-Jul-05 3:25
    sbuonocore19-Jul-05 3:25 
    GeneralThank you Pin
    Varun Shoor18-Aug-03 19:16
    Varun Shoor18-Aug-03 19:16 
    GeneralChange App name Pin
    13-Jan-03 6:22
    suss13-Jan-03 6:22 
    GeneralRe: Change App name Pin
    pjnaughter13-Jan-03 8:45
    pjnaughter13-Jan-03 8:45 
    GeneralGetProfileFloat and WriteProfileFloat Pin
    Codin' Carlos20-Jan-02 13:42
    Codin' Carlos20-Jan-02 13:42 
    GeneralCannot read ReadOnly keys Pin
    Hans-Georg Ulrich5-Aug-01 23:59
    Hans-Georg Ulrich5-Aug-01 23:59 
    GeneralRe: Cannot read ReadOnly keys Pin
    pjnaughter13-Jan-03 8:46
    pjnaughter13-Jan-03 8:46 
    GeneralSimple class... Pin
    Jason Troitsky (was Hattingh)21-Nov-00 8:16
    Jason Troitsky (was Hattingh)21-Nov-00 8:16 
    GeneralRe: Simple class... Pin
    D.D. de Kerf28-Nov-00 21:11
    D.D. de Kerf28-Nov-00 21:11 
    Generalthis class sucks BIGTIME Pin
    Magnus13-Jul-00 23:25
    Magnus13-Jul-00 23:25 
    GeneralRe: this class sucks BIGTIME Pin
    pjnaughter14-Jul-00 4:17
    pjnaughter14-Jul-00 4:17 
    GeneralCRegKey Pin
    rampas6-Jun-00 23:45
    rampas6-Jun-00 23:45 
    GeneralRe: CRegKey Pin
    pjnaughter14-Jul-00 4:20
    pjnaughter14-Jul-00 4:20 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.