09 June 2010

Editing / Writing and Retrieving values from the Windows System Registry Keys in Quick Test Professional (QTP)

In our day to day automation, you do need to create a registry key and write a value to it or retrieve / update a key value from an existing registry key. This is a very simple task and all you need is to create an object of the Windows Script Host and access the RegWrite and RegRead methods.

Sub SetRegistryValue(strRegPath, strRegValue)
  Set shObj = CreateObject("WScript.Shell")
  shObj.RegWrite strRegPath, strRegValue
End Sub

Function GetRegistryValue(strRegPath)
  Set shObj = CreateObject("WScript.Shell")
  GetRegistryValue = shObj.RegRead(strRegPath)
End Function

SetRegistryValue is a subroutine that takes the Registry Key Path and the value for the key as parameters. If the registry path does not exist a new registry key is created in the path and the value is set to that key.

GetRegistryValue is a function that takes the Registry Key Path and returns its value. If the path does not exist it will throw up an error.

How do you specify the Registry Key Path ?

The code below creates a key by the name "KEY" in the path HKEY_CLASSES_ROOT\.ANUNAY and stores "LEARNQTP.INFO" as a string data. It is the backslash '\' in the end of the path plays the important role in defining whether KEY is a key entity or a folder in the registry hive.

Msgbox GetRegistryValue("HKEY_CLASSES_ROOT\.ANUNAY\KEY")

The code below creates KEY as a folder within .ANUNAY and save the key value to the 'Default' key entity. So when you specify a key entity name in the Registry Key Path, DO NOT use a back slash '\'.

Msgbox GetRegistryValue("HKEY_CLASSES_ROOT\.ANUNAY\KEY\")

Also, in a similar way you can delete the registry key using RegDelete method of the Windows Script Host.

1 comment:

  1. Do you know, how QTP deals with a 64 Bit system's Registry? I wasted whole lot of time trying to write or modify a registry. for example if I wants to modify registry under HKEY_LOCAL_MACHINE\SOFTWARE\TestQTP\Test. So I used command like regwrite to update that, but it always updates in HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\TestQTP\Test.
    I think they have to work on that. Let me will they help me or not.