PerfectScript Journal
 

September 1997: Using the Registry to store information (WPWin 7 and 8 for Win95/NT) 

The Registry commands in PerfectScript allow you to get information from the Windows Registry, and to store your own information there also. The Registry files, SYSTEM.DAT and USER.DAT, are hidden files in the Windows directory. These are the files that actually contain the Registry data.

As a macro writer, you may have occasion to store information too. For example, if you create an invoicing system, you probably want to store the current invoice number somewhere so that each invoice will have a different number.

Why use the Registry rather than a BIF file or INI file?

Using the Registry is a Windows 95 and NT standard. BIF files are easily corrupted, and may not be supported in future versions of WordPerfect. Plus, WPWin 7 and 8 don't come with a tool for editing the BIF file as WPWin 6.1 did.

An INI file can only have a 2-level heirarchy, where as the Registry is capable of a many-level heirarchy.

However, if you need to store information that must be shared by multiple users over a network, you'll need to use a BIF or INI file that is stored in a location that all users have access and rights to.

Looking at your Registry

You can use the REGEDIT.EXE program that comes with Win95 or REGEDT32.EXE that comes with Windows NT 4.0 to take a look at your registry. Just run it from Start > Run. If you choose to do this, take care not to change any settings you are not familiar with.

Entire books have been written about the Windows Registry. If you want to know everything there is to know about the Registry, check your local bookstore or on the Internet.

How the Registry is Organized

Information is stored in the Registry under keys. Under each of these keys are additional keys. The keys can be several layers deep, and will finally end in a value. WordPerfect stores information under the keys HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE. This is where you will probably want to store your own information also.

Accessing Information from the Registry

To access or store information in the Registry, you must open the subkey you are interested in. You'll use the RegistryOpenKey command to open the key. The RegistryQueryValue command is used to get information from a particular key in the Registry, and the RegistrySetValue command to set a value in the registry. (This command also creates the key if it does not already exist.)

For example, if you want to know the current Invoice number for your invoicing system, you could use the following command:


HKey:=RegistryOpenKey(CurrentUser!; "Software\Custom Invoices\")
         

Once the key is open, you can read from it:


vInvoiceNum:=RegistryQueryValue(HKey; "Current Invoice Number")
         

  Or write to it: 


RegistrySetValue(HKey; "Current Invoice Number"; "10424"; String!)
           

When you are finished with a key in the Registry, You should close it:


RegistryCloseKey(HKey)         


  Putting it all together, you can get information stored in the Registry:   


HKey:=RegistryOpenKey(CurrentUser!; "Software\Custom Invoices")
vInvoiceNum:=RegistryQueryValue(HKey; "Current Invoice Number")
RegistryCloseKey(HKey)
MESSAGEBOX(;;"The current invoice number is: "+ vQuery)
         

  Or you can add or change information stored in the registry: 


vInvoiceNum:=vInvoiceNum + 1
HKey:=RegistryOpenKey(CurrentUser!; "Software\Custom Invoices")
RegistrySetValue(HKey; "Current Invoice Number"; vInvoiceNum; String!)
RegistryCloseKey(HKey)
          

To learn more about the Registry commands, including commands to find the number of keys, delete a key, create a key, etc., you can look in the WordPerfect Macros help file, or purchase a copy of my book.

 

Copyright Notice: The information included in the PerfectScript Journal is protected by US Copyright. The author grants you the right to use the routines in your own macros as needed. You may not sell, distribute, or publish them in any form.
If you choose to use the information here, you do so entirely at your own risk. No representations are made regarding the fitness of this information for your   particular purpose, or for your ability or inability to use the information. You   are advised to make backups of all relevant files before implementing any suggestion  or technique.
© Copyright 1997 by J. Jeppson.