• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

Windows API / Registry - simple example code needed.

How do I use the Windows API to access the registry?  I would like to store a "long" value at a specific location in the registry.  More specifically,

1)How do I check if the value is already sotred?
2)How do I read the value?
3)How do I write the value?

I need to do this with just Windows API calls, without using borland's registry implementation.
0
wolsen
Asked:
wolsen
  • 3
  • 3
  • 3
  • +3
3 Solutions
 
wolsenAuthor Commented:
correction:
1)How do I check if the value is already *stored*?
0
 
Jase-CoderCommented:
there is a datatype called TRegistry use that. I wrote an example of accessing the registry in delphi: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1360&lngWId=7 the code is very similar.
0
 
Jase-CoderCommented:
just noticed you dont want the borland class , check out: http://support.microsoft.com/default.aspx?scid=kb;en-us;145679
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
wolsenAuthor Commented:
Thanks for the reply,  but I need to do this with just Windows API calls, without using borland's registry implementation. ( without using TRegistry ).
0
 
wolsenAuthor Commented:
I would like a C++ example, if possible.
0
 
George TokasCommented:
At your header file add:
#include <Registry.hpp>

Also at public or private section add:
TRegistry    *Registry;//or a name you choose.

Now at .cpp file:
If you want to use it wherever at your program initialise it at OnCreate using:
Registry = new TRegistry();
Registry->RootKey = HKEY_......;//The section you want.
Registry->LazyWrite = false;//If you want changes to be made immediatetly..
//If not then ignore this line..
Now you can use the Registry object to read write whatever you want...
Registry->OpenKey(.....);//the key you want to open.
Registry->Readxxx...//Function to read..Check online help for details.. You can read strings, integers, binary..
Registry->Writexxx...//Function to write to registry..Same as above..
Registry->CloseKey();

Important... We first use OpenKey do what we want and then use CloseKey..

Thats for starters.. Online help is detailed on that subject...

gtokas.
0
 
CayceCommented:
0
 
AmigoJackCommented:
eat this (no lame TRegistry):



DWORD reg_getdatasize(LPCSTR _RegValueName, HANDLE _RegCurrentKey) {
  DWORD _RegDataType, result= 0;

  return (RegQueryValueEx(_RegCurrentKey, _RegValueName, NULL, &_RegDataType, NULL, &result)!= ERROR_SUCCESS? 0: result);
}

DWORD reg_getdata(LPCSTR _RegValueName, void* Buffer, DWORD BufSize, HANDLE _RegCurrentKey) {
  DWORD _RegDataType= 0;

  return (RegQueryValueEx(_RegCurrentKey, _RegValueName, NULL, &_RegDataType, (char*)Buffer, &BufSize)== 0? BufSize: 0);
}

// --- return MAXDWORD==doesnt exist  :(
// --- return (else)==does exist      :)
DWORD reg_readstring(HKEY hkbase, char* keypath, char* value, char* result) {
  HKEY hk;
  int len, i1;
  char* p1;

  if (RegOpenKeyEx(hkbase, keypath, 0, KEY_EXECUTE| KEY_QUERY_VALUE, &hk)== ERROR_SUCCESS) {
    len= reg_getdatasize(value, hk);
    if (len> 0) {
      p1= (char*)malloc(0xFFFF);
      reg_getdata(value, (void*)p1, len, hk);
      strcpy(result, p1);
      free(p1);

      result[len]= 0;
      RegCloseKey(hk);

      return len;
    }
  }
  return MAXDWORD;
}

// --- return MAXDWORD==writing failed  :(
// --- return (else)==succeeded writing :)
DWORD reg_writestring(HKEY hkbase, char* keypath, char* value, char* data) {
  HKEY hk;
  DWORD i1;

  if (RegOpenKeyEx(hkbase, keypath, 0, KEY_ALL_ACCESS, &hk)== ERROR_SUCCESS) {
    i1= RegSetValueEx(hk, value, 0, REG_SZ, data, strlen(data)+ 1);
    RegCloseKey(hk);

    return (i1== ERROR_SUCCESS? 0: MAXDWORD);
  }
  return MAXDWORD;
}

// --- return MAXDWORD==doesnt exist  :(
// --- return (else)==does exist      :)
DWORD reg_readdword(HKEY hkbase, char* keypath, char* value, DWORD* result) {
  HKEY hk;
  int len;

  if (RegOpenKeyEx(hkbase, keypath, 0, KEY_EXECUTE| KEY_QUERY_VALUE, &hk)== ERROR_SUCCESS) {
    len= reg_getdatasize(value, hk);
    if (len> 0) {
      reg_getdata(value, (void*)result, len, hk);
      RegCloseKey(hk);

      return len;
    }
  }
  return MAXDWORD;
}

// --- return MAXDWORD==writing failed  :(
// --- return (else)==succeeded writing :)
DWORD reg_writedword(HKEY hkbase, char* keypath, char* value, DWORD data) {
  HKEY hk;
  DWORD i1;

  if (RegOpenKeyEx(hkbase, keypath, 0, KEY_ALL_ACCESS, &hk)== ERROR_SUCCESS) {
    i1= RegSetValueEx(hk, value, 0, REG_DWORD, (CONST BYTE*)&data, sizeof(DWORD));
    RegCloseKey(hk);

    return (i1== ERROR_SUCCESS? 0: MAXDWORD);
  }
  return MAXDWORD;
}


//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // checking existance and reading STRING
  char* _p1= (char*)malloc(2000);
  Caption= (reg_readstring((HKEY)HKEY_LOCAL_MACHINE,
                           "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\0",
                           "ProductName\0",
                           _p1)!= MAXDWORD? _p1: "does not exist!");
  free(_p1);

  // writing (which automatically means creating) STRING
  Caption= (reg_writestring((HKEY)HKEY_LOCAL_MACHINE,
                            "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\0",
                            "ProductWhatever\0",
                            "Moooooo!")!= MAXDWORD? "done": "failed");

  // checking existance and reading DWORD
  unsigned long i1;
  Caption= (reg_readdword((HKEY)HKEY_LOCAL_MACHINE,
                          "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\0",
                          "InstallDate\0",
                          &i1)!= MAXDWORD? IntToStr(i1): AnsiString("does not exist!"));
  free(_p1);

  // writing (which automatically means creating) DWORD
  Caption= (reg_writedword((HKEY)HKEY_LOCAL_MACHINE,
                           "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\0",
                           "InstallWhatever\0",
                           101022299)!= MAXDWORD? "done": "failed");
}
//---------------------------------------------------------------------------



looks huge but its eazy - give it a try ;-)
wheres my caffeine?...
0
 
AmigoJackCommented:
i have provided a complete example with API calls only, not using borlands TRegistry. "gtokas" took TRegistry and "Jase-Coder" gave links instead of examples. i wish someone understands my point here
0
 
VenabiliCommented:
And you did it 2 months after the question and the last comment in it. I hope you get the point that usually 2 months later, the answer is not really needed.

And as additional note - this question seems very much as homework :) Full code on such is not really a nice idea :)
0
 
AmigoJackCommented:
wew - 2 months after! ive never noticed that :) okok youre rite - forget about dre :P
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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