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: 761
  • Last Modified:

Need to create registry hive under (HKLM) in W/NT

I need to create a registry hive in Windows NT under HKLM. I get an error trying to write to the registry. How, and what code can I use to get past this problem? Do I need to call an API? if so how and could you please illustrate. Also am I able to have the program call a .reg file to make the changes?
Thanks
Pote
0
pote
Asked:
pote
  • 3
  • 3
  • 3
  • +1
1 Solution
 
tchalkovCommented:
You. first of all need to be an administrator to write to HKLM.

If you can't do it through Registry Editor, then you can't do it with a program too.

If you are an administrator, then there are a few ways to do this.

1. Create a reg file, and execute it trhough ShellExecute

2. Use the Reg... API (RegCreateKey etc.). If you choose this way and need an exmaple on this I can send you.
0
 
poteAuthor Commented:

Actually yes, I will need an example of using the Reg...API(RegCreateKey etc..)
Thanks for your help..
Pote
0
 
tchalkovCommented:
Here is a sample class for registry access. It is far from perfect(a friend wrote it while learning the API), but it demonstrates the nasic use of Reg... API.

For Creating keys and values see the setposition and setkey functions.

#include "stdafx.h"
#include "windows.h"
#include "winerror.h"
#include "shlwapi.h"
//this convert char* to unsigned char*
#define char2unsigned(x) ((unsigned char *) x)

#define DEBUG_CREG

#ifdef DEBUG_CREG
#include "iostream.h"
#define ERRORMSG(X) OutputDebugString("\nerreor\tX1")
#endif

class CReg
{
      unsigned int len ( register const unsigned char * a)
      {
            for (register unsigned int i=0;*(a+i);++a,++i);
            return i+1;
      }
protected:
      HKEY curpos;
public:
      CReg(HKEY key=HKEY_CLASSES_ROOT):curpos(key){}      
      virtual ~CReg()//this may be to save unsaved key or value in registry
      {
            try
            {
            long res=::RegCloseKey(curpos);
            if (!(res==ERROR_SUCCESS));
            }
            catch(...)
            {
                  ERRORMSG(1);
            }

      }
      HKEY setposition(HKEY key) { return curpos=key;}
      //--------------------
      //this function for save String value
      int setvalue(const unsigned char * val,const char * nameval,HKEY pos=0)
      {
            try
            {
            long errcode=::RegSetValueEx ((pos?pos:curpos),nameval,0,REG_SZ,val,len(val)+1);
            if (errcode==ERROR_SUCCESS)
                  return 0;
            return 1;
            }
            catch(...)
            {
                  ERRORMSG(2);
            }
      }
      //--------------------
      //this function for save a DWORD value 32 bit number
      int setvalue( unsigned int  val,const char * nameval,HKEY pos=0)
      {
            try
            {
            long a=a/0;
            long errcode=::RegSetValueEx ((pos?pos:curpos),nameval,0,REG_DWORD, (const unsigned char *)&val,sizeof(unsigned int));
            if (errcode==ERROR_SUCCESS)
                  return 0;
            return 1;
            }
            catch(...)
            {
                  ERRORMSG(3);
            }
      }
      //--------------------
      //this fuction for save a BINARY value with size
      int setvalue(const unsigned char * val,const char * nameval,unsigned int size,HKEY pos=0)
      {
            try
            {
            long errcode=::RegSetValueEx ((pos?pos:curpos),nameval,0,REG_BINARY,val,size);
            if (errcode==ERROR_SUCCESS)
                  return 0;
            return 1;
            }
            catch(...)
            {
                  ERRORMSG(4);
            }
      }
      //create simple key
      HKEY setkey(char * namekey,HKEY key=0);
      HKEY setposition(char *namekey,HKEY key=0)
      {
            try
            {
                  HKEY k;
                  ::RegOpenKeyEx((key?key:curpos),namekey,0,KEY_ALL_ACCESS,&k);
                  curpos=k;
                  return k;
            }
            catch(...)
            {
                  ERRORMSG(5);
            }
      }
      long delvalue(char * nameval,HKEY key=0)
      {
            try
            {
                  return ::RegDeleteValue((key?key:curpos),nameval);
            }
            catch(...)
            {
                  OutputDebugString("\nerreor\n");
            }
      }
      long delkey(char * name,HKEY key=0)
      {
            try
            {
                  DWORD er=::RegDeleteKey((key?key:curpos),name);
                  if (!(er==ERROR_SUCCESS))
                  {
#ifdef DEBUG_CREG
                        cout <<"The key is not empty";
#endif
                  //if Win ÍÒ
                        er=::SHDeleteKey((key?key:curpos),name);
                        if(!(er==ERROR_SUCCESS)) er=::SHDeleteEmptyKey((key?key:curpos),name);
                  }
                  else
                  {
#ifdef DEBUG_CREG
                        cout <<"The key is empty";
#endif
                  }
                  return er;
            }
            catch(...)
            {
                  ERRORMSG(5);
            }
      }
};

HKEY CReg::setkey(char * namekey,HKEY key)
{
            
      try
      {
            DWORD what;                        
            key=(key?key:curpos);
            long errcode=::RegCreateKeyEx(key,namekey,0,0,REG_OPTION_VOLATILE,KEY_ALL_ACCESS,0,&curpos,&what);
            
#ifdef DEBUG_CREG
            if (what==REG_CREATED_NEW_KEY)
                  cout <<"\nnew key";
            else
                  cout <<"\nold key";
#endif
            /*if (errcode==ERROR_SUCCESS)
                  return 0;
            return 1;*/
            return curpos;
      }
      catch(...)
            {
                  ERRORMSG(5);
            }
}




int main(int argc, char* argv[])
{
#ifdef DEBUG_CREG
      CReg a=HKEY_CURRENT_USER;
      
      a.setkey ("opit key\\opit2");
      a.setvalue(355,"opit pri syzdavane na moj key");
      a.setvalue (char2unsigned("opit"),"opit");
      a.setvalue (23,"opitint");
      a.setvalue (char2unsigned("binary"),"binary",2);
      a.delvalue("binary");
      a.setposition (HKEY_CURRENT_USER);
      a.delkey ("opit key");
#endif
      return 0;
}

0
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!

 
WynCommented:
Hi ,pote .
Here is a sample code ,you can simply replace the key name for your need:)


    LONG    lResult;
    HKEY    hRegKey;
    DWORD   nDispos;

    lResult = RegCreateKeyEx(
                HKEY_CLASSES_ROOT,      // handle of an open key
                "my new group",         // address of subkey name
                0,                      // reserved
                "",                     // address of class string
                0,                      // special options flag
                KEY_ALL_ACCESS,         // desired security access
                NULL,                   // address of key security structure
                &hRegKey,               // address of buffer for opened handle
                &nDispos                // address of disposition value buffer
           );

    if (lResult==ERROR_SUCCESS)
    {
        //-- 1. store a number
        DWORD nValue;

        nValue = 123;

        lResult = RegSetValueEx(
                hRegKey,        // handle of key to set value for
                "MyIntegerKey", // address of value to set
                0,              // reserved
                REG_DWORD,      // flag for value type
                (const BYTE*)&nValue,        // address of value data
                sizeof(DWORD)   // size of value data
               );

        //-- 2. store a string
        char *txt = "Hello World!";

        lResult = RegSetValueEx(
                hRegKey,        // handle of key to set value for
                "MyStringKey",  // address of value to set
                0,              // reserved
                REG_SZ,      // flag for value type
                (const BYTE*)txt,        // address of value data
                strlen(txt)+1   // size of value data
               );


        RegCloseKey(hRegKey);
     }

0
 
poteAuthor Commented:
I'll give both examples a try, and respond whit results/
Thanks
0
 
poteAuthor Commented:
I've tried both examples.
None work for NT.
The first example from tchalkov  compiles with 3 errors, looking for unresolved externals (probably a lib file not included)
The example provided by Wyn works for Win 95 and 98, but not NT, which is what I need.
Any other suggestions
0
 
WynCommented:
->The example provided by Wyn works for Win 95 and 98, but not NT, which is what I need.
=======================
Nope,it works both but you must the one who can change NT reigstry(e.g:administrator),undertand me?

What problem you meet to change NT if it's not the problem above?

Free to ask
Regards
W.Yinan
0
 
tchalkovCommented:
Here is an update source code.
In order to compile you need to add shlwapi.lib to the libraries in yiur projects.

This is necessary because some actions on the system registry are easier to do through the shlwapi.dll.

Run this program as console application.

This code is tested and works under NT.

If you still have problems under NT, debug the program, and tell me the eror codes you recieve.

#include "windows.h"
#include "winerror.h"
#include "shlwapi.h"
//this convert char* to unsigned char*
#define char2unsigned(x) ((unsigned char *) x)

#define DEBUG_CREG

#ifdef DEBUG_CREG
#include "iostream.h"
#define ERRORMSG(X) OutputDebugString("\nerreor\tX1")
#endif

class CReg
{
      unsigned int len ( register const unsigned char * a)
      {
            for (register unsigned int i=0;*(a+i);++a,++i);
            return i+1;
      }
protected:
      HKEY curpos;
public:
      CReg(HKEY key=HKEY_CLASSES_ROOT):curpos(key){}
      virtual ~CReg()//this may be to save unsaved key or value in registry
      {
            try
            {
                  long res=::RegCloseKey(curpos);
            }
            catch(...)
            {
                  ERRORMSG(1);
            }
            
      }
      HKEY setposition(HKEY key) { return curpos=key;}
      //--------------------
      //this function for save String value
      int setvalue(const unsigned char * val,const char * nameval,HKEY pos=0)
      {
            try
            {
                  long errcode=::RegSetValueEx ((pos?pos:curpos),nameval,0,REG_SZ,val,len(val)+1);
                  if (errcode==ERROR_SUCCESS)
                        return 0;
                  return 1;
            }
            catch(...)
            {
                  ERRORMSG(2);
                  return 1;
            }
      }
      //--------------------
      //this function for save a DWORD value 32 bit number
      int setvalue( unsigned int  val,const char * nameval,HKEY pos=0)
      {
            try
            {
//                  long a=a/0;
                  long errcode=::RegSetValueEx ((pos?pos:curpos),nameval,0,REG_DWORD, (const unsigned char *)&val,sizeof(unsigned int));
                  if (errcode==ERROR_SUCCESS)
                        return 0;
                  return 1;
            }
            catch(...)
            {
                  ERRORMSG(3);
            }
      }
      //--------------------
      //this fuction for save a BINARY value with size
      int setvalue(const unsigned char * val,const char * nameval,unsigned int size,HKEY pos=0)
      {
            try
            {
                  long errcode=::RegSetValueEx ((pos?pos:curpos),nameval,0,REG_BINARY,val,size);
                  if (errcode==ERROR_SUCCESS)
                        return 0;
                  return 1;
            }
            catch(...)
            {
                  ERRORMSG(4);
            }
      }
      //create simple key
      HKEY setkey(char * namekey,HKEY key=0);
      HKEY setposition(char *namekey,HKEY key=0)
      {
            try
            {
                  HKEY k;
                  ::RegOpenKeyEx((key?key:curpos),namekey,0,KEY_ALL_ACCESS,&k);
                  curpos=k;
                  return k;
            }
            catch(...)
            {
                  ERRORMSG(5);
            }
      }
      long delvalue(char * nameval,HKEY key=0)
      {
            try
            {
                  return ::RegDeleteValue((key?key:curpos),nameval);
            }
            catch(...)
            {
                  OutputDebugString("\nerreor\n");
            }
      }
      long delkey(char * name,HKEY key=0)
      {
            try
            {
                  DWORD er=::RegDeleteKey((key?key:curpos),name);
                  if (!(er==ERROR_SUCCESS))
                  {
                        //if Win ÍÒ
                        er=::SHDeleteKey((key?key:curpos),name);
                        if(!(er==ERROR_SUCCESS)) er=::SHDeleteEmptyKey((key?key:curpos),name);
                  }

                  return er;
            }
            catch(...)
            {
                  ERRORMSG(5);
                  return 1;
            }
      }
};

HKEY CReg::setkey(char * namekey,HKEY key)
{
      
      try
      {
            DWORD what;
            key=(key?key:curpos);
            long errcode=::RegCreateKeyEx(key,namekey,0,0,REG_OPTION_VOLATILE,KEY_ALL_ACCESS,0,&curpos,&what);
            return curpos;
      }
      catch(...)
      {
            ERRORMSG(5);
      }
}




int main(int argc, char* argv[])
{
#ifdef DEBUG_CREG
      CReg a=HKEY_CURRENT_USER;
      
      a.setkey ("opit key\\opit2");
      a.setvalue(355,"opit pri syzdavane na moj key");
      a.setvalue (char2unsigned("opit"),"opit");
      a.setvalue (23,"opitint");
      a.setvalue (char2unsigned("binary"),"binary",2);
      a.delvalue("binary");
      a.setposition (HKEY_CURRENT_USER);
      a.delkey ("opit key");
#endif
      return 0;
}
0
 
PacmanCommented:
Wyn, you dont have to be administrator under NT.
You must write the correct code ; )

pote, you must do some "security-things" to access registry fully on NT.
take a look at the following article from the microsofties:

http://support.microsoft.com/support/kb/articles/Q128/7/31.asp?LNG=ENG&SA=ALLKB&FR=0 

let me know if it helped you
0
 
PacmanCommented:
oops,

sorry wrong subject : )
forget my previous comment ..
(where's the trashcan ???)
0
 
PacmanCommented:
Here a short overview on how to write values to registry:

First you have to get a "handle" to a key. Key is like a directory
or like a group.
You can either create a new key (group) or open an existing one.
The function for both cases is RegCreateKeyEx().

Having a valid handle to this key, you can now create new or change
existing sub-keys.
The function is RegSetValueEx()


a sample code:


    LONG    lResult;
    HKEY    hRegKey;
    DWORD   nDispos;

    lResult = RegCreateKeyEx(
                HKEY_CLASSES_ROOT,      // handle of an open key
                "my new group",         // address of subkey name
                0,                      // reserved
                "",                     // address of class string
                0,                      // special options flag
                KEY_ALL_ACCESS,         // desired security access
                NULL,                   // address of key security structure
                &hRegKey,               // address of buffer for opened handle
                &nDispos                // address of disposition value buffer
           );

    if (lResult==ERROR_SUCCESS)
    {
        //-- 1. store a number
        DWORD nValue;

        nValue = 123;

        lResult = RegSetValueEx(
                hRegKey,        // handle of key to set value for
                "MyIntegerKey", // address of value to set
                0,              // reserved
                REG_DWORD,      // flag for value type
                (const BYTE*)&nValue,        // address of value data
                sizeof(DWORD)   // size of value data
               );

        //-- 2. store a string
        char *txt = "Hello World!";

        lResult = RegSetValueEx(
                hRegKey,        // handle of key to set value for
                "MyStringKey",  // address of value to set
                0,              // reserved
                REG_SZ,      // flag for value type
                (const BYTE*)txt,        // address of value data
                strlen(txt)+1   // size of value data
               );


        RegCloseKey(hRegKey);
     }


Hope this helps you.
0
 
WynCommented:
->Wyn, you dont have to be administrator under NT.
You must write the correct code ; )
==========================
Code has no problem as yours but as administrator maybe a wrong word.
Anyway,what problem you meet ,pote?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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