Every so often you get these development requests that seem to push Dexterity to its limits. One of such requests is being able to read a Windows Registry key value using nothing more than SanScript.
The following example will show how to use the Microsoft Windows Management Instrumentation (WMI) Scripting library, ADVAPI32.DLL to retrieve a Windows Registry key value by examining how to retrieve the default Internet browser software being used.
The code will use the RegOpenKeyA and RegQueryValueExA DLL functions to a) return a handle for the registry path where we can found the key, b) then retrieve the actual key value. In order to access external DLL functions, it is necessary to create Dexterity prototype global procedures for the external DLL functions.
Now that we have the prototype functions, we can proceed to use the Constant Definition window in Dexterity to define values for each Registry hive.
Registry Hive Constants
NOTE: These constants are usually known by their hexadecimal values, but Dexterity does treats hexadecimal constants as strings, hence the decimal notation used.
Once the prototype functions have been defined, we can wrap these in an API that isolates the developer from dealing with the innerworks of the calls. We will define to global functions as follow:
RegKeyExists accepts the registry hive parameter (hkey) and the registry path and returns a handler if the path is valid (phkey).
RegGetKeyValue in turn will take the handler (returned by RegKeyExists) and attempt to retrieve a value for the Default entry of the path previously given. If the function succeeds, it will return a string with the actual value, else the user will get a KEY_ERROR message.
Making it all work together..
For this project, I have created the following form:
The form contains 4 local variables:
‘(L) RegistryHive’: is a drop-down list with the following string values corresponding to each registry hive, as follows: HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_PERFORMANCE_DATA, and HKEY_CURRENT_CONFIG.
‘(L) RegistryPath’: is a string of 255 characters in length.
‘(L) RegistryKey’: is a string of 50 characters in length, not used in this project. I will leave this for a future post.
‘(L) RegKeyValue’: is a string of 255 characters in length. Set the property to Editable to False.
Now that you have the strings and drop-down list, drag the ‘OK Button’ push button control to finish — I am working in the Dynamics dictionary, DYNAMICS.DIC.
The actual production form will look like this:
We can now add the following Sanscript code to the OK Button change script:
syGetRegistryKey OK Button K_CHG
You can compile and run the code in test mode and use the Developer Assistant form to open the newly created form. For example, let’s check the registry for the default Internet browser running:
I will be taking a shot a two other functions in the Microsoft WMI Scripting library in a futute installment. Hope you enjoy this article and can’t wait to hear your comments.
A big thank you to Jon Eastman for his insight on the ADVAPI32.DLL. He gave me the idea for this post… that’s what it’s all about!
Until next post!
Mariano Gomez, MVP
Maximum Global Business, LLC