Team LiB   Previous Section   Next Section

2.3 Getting Data In and Out

There are several ways to move data into and out of the Registry; which one you use depends on what you're trying to accomplish and the amount of time you're willing to spend. Each of them is covered in more detail in later chapters.

First of all, you can make direct calls to the Win32 Registry API routines. At bottom, this is what all the other methods eventually do; the OS' security components and the undocumented internal format of the hive files ensure that the only way to load data is to use these routines. The basic process is fairly simple: you start by opening a key or subkey by its name. Once you've done so, you can do things to that key or its subkeys: you can query its value, create new subkeys beneath it, or even ask about its security settings. You can continue to use that particular key until you're done it, at which time you must close it again. Here's a small sample that shows these steps in action; it gets the computer's network name and uses it to print a welcome message. You'll learn more about programming for the Registry in C (as in this example) in the section titled Section 8.3 in Chapter 8.

// Hello, World! for the Registry: gets this machine's name and prints
// it out.
#include <windows.h>
#include <winreg.h>
#include <stdio.h>

void main(void)
{
   unsigned char 	lpName[MAX_PATH] = "";
   DWORD	 nNameLen = MAX_PATH;
   HKEY 	hkResult, hStartKey = HKEY_LOCAL_MACHINE;
   LONG 	nResult = ERROR_SUCCESS;
	
    nResult = RegOpenKeyEx(hStartKey, 
	        "SYSTEM\\CurrentControlSet\\Control\\ComputerName", 
	         0L, KEY_READ, &hkResult);
    if (ERROR_SUCCESS == nResult)
    {
           nResult = RegQueryValueEx(hkResult, "ActiveComputerName", 0, 0,
                                       lpName, &nNameLen);
           if (ERROR_SUCCESS == nResult)
                printf("Hello, world, from %s!", lpName);
   }
   RegCloseKey(hkResult);
}

The next step up the evolutionary ladder of Registry access is to use a library or language that removes you from direct contact with the Registry API routines. Depending on your needs and inclinations, there are several ways to accomplish this:

  • If you're using Visual Basic or Delphi, you can use a third-party library such as Desaware's Registry Control for Visual Basic (http://www.desaware.com ). These libraries typically wrap several API calls into one, so you can more easily perform the typical find-query-close cycle by making a single call. The Desaware control is covered at length in Chapter 7 of Inside the Windows 95 Registry.

    In addition, Microsoft makes available another package that simplifies Registry handling from Delphi and VB: see http://www.microsoft.com/vbasic/downloads/download.asp?ID=026.

  • The Win32 version of the Perl programming language includes a number of features that ease access to Registry data from Perl programs. Besides wrapping the find-query-close cycle for you, they make it easy to enumerate and search keys and quickly put the results into associative arrays. You'll see how to harness these features in Section 8.4 in Chapter 8. For a complete treatment of Win32 Perl, see Learning Perl on Win32 Systems by Randal L. Schwartz, Erik Olson, and Tom Christiansen (O'Reilly & Associates).

  • The Windows Scripting Host (WSH) provides a module called the Windows Management Instrumentation (WMI); WMI provides a rash of Registry calls you can use from within your WSH scripts.

  • The Windows 2000 Resource Kit includes a tool called REGINI.EXE that allows you to load text files of settings into the Registry. This is a handy and fast way to take a predefined set of data and jam it into the Registry; best of all, you can easily use REGINI to automate the process of loading Registry data into many different machines. Note that this tool works fine under Windows 2000, even though it's not included in the resource kit.

The final layer of Registry editing and spelunking revolves around using Registry editors. In addition to RegEdt32 and RegEdit, there are a number of freeware and shareware alternatives floating around.

    Team LiB   Previous Section   Next Section