Solved

NT 4.0 WKS registry programming, key create

Posted on 1998-07-22
4
238 Views
Last Modified: 2010-04-15
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
Comment
Question by:august97
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
Comment Utility
You're almost there!  All you need is RegSetValueEx() and RegQueryValueEx() and you're done.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
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
 
LVL 11

Expert Comment

by:alexo
Comment Utility
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
 

Author Comment

by:august97
Comment Utility
Thank you!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now