Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NT 4.0 WKS registry programming, key create

Posted on 1998-07-22
4
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 11

Accepted Solution

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

Expert Comment

by:alexo
ID: 1251776
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
ID: 1251777
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
ID: 1251778
Thank you!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

722 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