Accessing the Registry Remotely

[Previous] [Next]

Windows allows you to access the registry on a remote machine in much the same way you access the registry on the local machine. This greatly eases the task of developing software that can be configured from a remote location on the network.

NOTE
To give remote registry access under Windows its fair consideration, I would need to delve into security. I'll defer a more in-depth discussion on security to Chapter 10.

The designers of the Windows registry functions really simplified the task of remotely accessing the registry by allowing application developers to use the same functions they use to access the local registry. For example, you can use RegOpenKeyEx to open keys on remote machines and on the local machine. Likewise, RegSetValueEx, RegQueryValueEx, RegEnumKey, and so on can be used to manipulate the registry on a remote machine. The only difference is in obtaining a handle to one of the system's predefined registry keys on the remote machine. This is done via a call to RegConnectRegistry, which is prototyped as follows:

 LONG RegConnectRegistry( PTSTR pszMachineName, HKEY hkey, PHKEY phkResult); 

As you can see, RegConnectRegistry is fairly straightforward. You pass the name of the machine to which you want to connect in the pszMachineName parameter. You also pass one of the predefined registry keys (such as HKEY_LOCAL_MACHINE or HKEY_USERS) to indicate which portion of the remote machine's registry you wish to access. Finally, you provide RegConnectRegistry with a pointer to an HKEY variable in which the function will store an open handle to the remote registry's root key.

NOTE
Your application can pass NULL as the pszMachineName parameter to indicate a connection to the local machine. This is a convenient mechanism for creating registry tools that work remotely and locally with minimal extra logic.

To retrieve values from a remote registry, you can use the RegQueryMultipleValues function. The RegQueryMultipleValues function is similar to RegQueryValueEx, but it can request multiple values. The RegQueryMultipleValues function allows your application to minimize network traffic by requesting multiple values in a single network operation. RegQueryMultipleValues is not exclusively available for remote registry manipulation, but it is in this capacity that it really shines. In an industry where network bandwidth is at a premium, each hit to the network comes at a cost. Here is the prototype for RegQueryMultipleValues:

 LONG RegQueryMultipleValues( HKEY hkey, PVALENT val_list, DWORD num_vals, PTSTR pszValueBuf, PDWORD dwTotsize); 

Your application must pass an array of VALENT structures to the function via the val_list parameter. The system then uses this array to find the values to be retrieved, and populates the structure with the information for each value. The VALENT structure is declared as follows:

 typedef struct value_ent { PTSTR ve_valuename; DWORD ve_valuelen; DWORD ve_valueptr; DWORD ve_type; } VALENT; 

It is odd that the designers of the registry functions didn't also implement a function to set multiple values in the registry as a single operation. I assume that this was not done because the registry is typically read from much more often than it is written to. Of course, you could implement your own "RegSetMultipleValues" by making multiple calls to RegSetValueEx. Network traffic, however, would not be reduced by this implementation of the function.

NOTE
The RegQueryMultipleValues function is limited to reading no more than 1 MB of data per call. The function will succeed only if it can return all of the requested values. It will fail if collectively more than 1 MB of data is requested.



Programming Server-Side Applications for Microsoft Windows 2000
Programming Server-Side Applications for Microsoft Windows 2000 (Microsoft Programming)
ISBN: 0735607532
EAN: 2147483647
Year: 2000
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net