2.3 Getting Data In and OutThere 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:
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. |