Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

NT 4.0 WKS registry programming, key create

How can I create a registry key and assign a value? The only thing I succeeded was to create a
subkey (i.e. a folder, but the folder is empty). Attached are code fragments that I have used in
WATCOM C 10.6. I have played with the security mask, but no success. A working C code
would be greatly appreciated. Thank you in advance.

#include <windows.h>
#include <winreg.h>


int       rcRegCreateKeyEx;
LONG      rcRegOpenKey;
LPCTSTR   lpszSubKey[20];
HKEY      hkOpenResult;
HKEY      hkCreateResult;
LPDWORD   lpdwCreateDisposition;
LPTSTR    lpszCreateClass;

    strcpy((char *) lpszSubKey,"SOFTWARE");
    rcRegOpenKey = RegOpenKeyEx( HKEY_LOCAL_MACHINE,   // handle of open key
                                 (LPCTSTR) lpszSubKey,   // address of name of subkey to open
                                 0, // reserved
                                 KEY_SET_VALUE  , // security access mask
                                 &hkOpenResult );    // address of handle of open key

    rcRegCreateKeyEx = RegCreateKeyEx(  hkOpenResult,   // handle of an open key
                                        (LPCTSTR) lpszSubKey,   // address of subkey name
                                        0,  // reserved
                                        (LPTSTR)  "",  // address of class string
                                        (DWORD) REG_OPTION_NON_VOLATILE   ,  // special options flag
                                        (REGSAM) KEY_SET_VALUE , // desired security access
                                        (LPSECURITY_ATTRIBUTES)  NULL,   // address of key security structure
                                        (PHKEY)  &hkCreateResult,   // address of buffer for opened handle  
                                        (LPDWORD)  &lpdwCreateDisposition );  // address of disposition value buffer
   RegCloseKey( (HKEY)  hkOpenResult );   // handle of key to close
0
august97
Asked:
august97
  • 3
1 Solution
 
alexoCommented:
You're almost there!  All you need is RegSetValueEx() and RegQueryValueEx() and you're done.
0
 
alexoCommented:
Example:
=======

HKEY hk;  
DWORD dwData;
UCHAR szBuf[80];

/*
 * Add your source name as a subkey under the Application
 * key in the EventLog service portion of the registry.
 */

if (RegCreateKey(HKEY_LOCAL_MACHINE,
    "SYSTEM\\CurrentControlSet\\Services\
    \\EventLog\\Application\\SamplApp",
    &hk))
    ErrorExit("could not create registry key");

/* Set the Event ID message-file name. */

strcpy(szBuf, "%SystemRoot%\\System\\SamplApp.dll");

/* Add the Event ID message-file name to the subkey. */

if (RegSetValueEx(hk,             /* subkey handle         */
        "EventMessageFile",       /* value name            */
        0,                        /* must be zero          */
        REG_EXPAND_SZ,            /* value type            */
        (LPBYTE) szBuf,           /* address of value data */
        strlen(szBuf) + 1))       /* length of value data  */
    ErrorExit("could not set event message file");

/* Set the supported types flags. */

dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
EVENTLOG_INFORMATION_TYPE;

if (RegSetValueEx(hk,      /* subkey handle                */
        "TypesSupported",  /* value name                   */
        0,                 /* must be zero                 */
        REG_DWORD,         /* value type                   */
        (LPBYTE) &dwData,  /* address of value data        */
        sizeof(DWORD)))    /* length of value data         */
    ErrorExit("could not set supported types");

RegCloseKey(hk);

0
 
alexoCommented:
Sample Code to Change Hard Error Popup Mode
-------------------------------------------
 
The following function changes the hard error popup mode. If the function
succeeds, the return value is TRUE. If the function fails, the return value
is FALSE.
 
BOOL SetGlobalErrorMode(
    DWORD dwErrorMode   // specifies new ErrorMode value
    )
{
    HKEY hKey;
    LONG lRetCode;
 
    // make sure the value passed isn't out-of-bounds
    if (dwErrorMode > 2) return FALSE;
 
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                    "SYSTEM\\CurrentControlSet\\Control\\Windows",
                    0,
                    KEY_SET_VALUE,
                    &hKey) != ERROR_SUCCESS) return FALSE;
 
    lRetCode=RegSetValueEx(hKey,
                           "ErrorMode",
                           0,
                           REG_DWORD,
                           (CONST BYTE *) &dwErrorMode,
                           sizeof(DWORD) );
 
    RegCloseKey(hKey);
 
    if (lRetCode != ERROR_SUCCESS) return FALSE;
 
    return TRUE;
}
 
Sample Code to Obtain Hard Error Popup Mode
-------------------------------------------
 
The following function obtains the hard error popup mode. If the function
succeeds, the return value is TRUE. If the function fails, the return value
is FALSE. If the function succeeds, dwErrorMode contains the error popup
mode. Otherwise, dwErrorMode is undefined.
 
BOOL GetGlobalErrorMode(
    LPDWORD dwErrorMode // Pointer to a DWORD to place popup mode
    )
{
    HKEY hKey;
    LONG lRetCode;
    DWORD cbData=sizeof(DWORD);
 
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                    "SYSTEM\\CurrentControlSet\\Control\\Windows",
                    0,
                    KEY_QUERY_VALUE,
                    &hKey) != ERROR_SUCCESS) return FALSE;
 
    lRetCode=RegQueryValueEx(hKey,
                            "ErrorMode",
                            0,
                            NULL,
                            (LPBYTE) dwErrorMode,
                            &cbData );
 
    RegCloseKey(hKey);
 
    if (lRetCode != ERROR_SUCCESS) return FALSE;
 
    return TRUE;
}

0
 
august97Author Commented:
Thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now