Solved

writing to HKEY_LOCAL_MACHINE??

Posted on 1998-11-02
4
338 Views
Last Modified: 2013-11-20
Using RegCreateKeyEx it seems that I cannot create a regestry key under HKEY_USERS or HKEY_LOCAL_MACHINE.  Is there a way to get around this problem?
If possible I'd like to steer away from CWinApp class members too.. - is this possible?

Tim
0
Comment
Question by:TimB
  • 2
4 Comments
 
LVL 3

Expert Comment

by:plaroche
ID: 1324163
What kinds of problems are you having?

I write to HKEY_LOCAL_MACHINE\Software quite easily and it works fine.  Are you under NT? If so have you checked that the logged user is administrator of the machine? I think it's needed to write to that registry section.
0
 

Accepted Solution

by:
gunn earned 130 total points
ID: 1324164
Try this function that I often use on WindowsNT/95/98, flawlessly ;)

-Tom

#define D_KEY "SOFTWARE\\Company\\WhateverElse"

char* GetRegKeyCT()

{

  long status;

  HKEY  hKey;

  DWORD type, datasize;

  BYTE *data;



  // Open the Key in the Windows Registry

  status = RegOpenKeyEx( HKEY_LOCAL_MACHINE,

                D_KEY,

                0,

                KEY_QUERY_VALUE,

                &hKey );



  // Check for error in opening

  if( status != ERROR_SUCCESS )

    return NULL;



  // Get the data size of the key first
   // "current_version" being the string you want to get...

  status = RegQueryValueEx( hKey, "current_version", NULL, &type, NULL, &datasize );

  if( datasize == 0 || status != ERROR_SUCCESS )

    return NULL;



  // Allocate the memory for the buffer then get the

  // data again, this time putting it into buffer.

  data   = (unsigned char*)malloc( (size_t)datasize );
   
   // "current_version" being the string you want to get...

  status = RegQueryValueEx( hKey, "current_version", NULL, &type, data, &datasize );



  // Check for error in obtaining data

  if( status != ERROR_SUCCESS )

    return NULL;



  // Close the open key

  status = RegCloseKey( hKey );



  return (char*)data;

}


0
 

Expert Comment

by:gunn
ID: 1324165
Ahhhhhhhhh, stupid me ;)

I read it, thinking 'ahh! I have done that!, only to (after posting the "answer") find out after reading -again- the question, that you were looking for way to *create* the key....doh!  I have done that too, but don't have the code at my fingertips....

sorry about that, you can reject my answer ;(

-Tom

0
 
LVL 1

Author Comment

by:TimB
ID: 1324166
Thanks for the help,  I figured out how to create a key under HKLM, I think it was just me not being able to understand mocrosoft help files - doh!

Here's what I did in the end:

RegCreateKeyEx(HKEY_LOCAL_MACHINE,      // handle of an open key
                  StrKeyPath, //  address of subkey name
                  0, // address of class string
                  NULL, // special options flag
                  0, //
                  KEY_SET_VALUE,  
                  NULL,
                  &hKey,
                          NULL) == ERROR_SUCCESS)


Thanks for your help anyway, and here's 130 points,

Bye-ee ;)

Tim
0

Featured Post

IT, Stop Being Called Into Every Meeting

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

18 Experts available now in Live!

Get 1:1 Help Now