Forum Discussion

scot1967's avatar
scot1967
Frequent Contributor
8 days ago

How To: Read data from the Windows Registry

Hello all,

I have seen a few posts here about registry access and editing.  This is  how I retrieve data from the Windows registry using JavaScript.  I am using this to return the OS information and application path information. This is very useful when added to the EventControl_OnStartTest event code. This will allow you to return OS information and other needed data at each test run.  Some test management systems may provide this information for you or it may be logged in the in data produced in a pipeline run.  This will embed the information directly into your test log. 

SmartBear KB Links:

This bit of code will return the Product Name and Current Build from the registry. This location may vary between OS's so you will want to check this with RegEdit. 

let Section = Storages.Registry("SOFTWARE\\Microsoft\\Windows NT", HKEY_LOCAL_MACHINE);
let regKeyString = Section.GetSubSection("CurrentVersion").Name;
let productIdString = Storages.Registry(regKeyString, HKEY_LOCAL_MACHINE, 1, true).GetOption("ProductName", "");
let currentBuildString = Storages.Registry(regKeyString, HKEY_LOCAL_MACHINE, 1, true).GetOption("CurrentBuild", "");
Log.Message("Windows Version: " + productIdString + " Build: " + currentBuildString )

I have also found the need to find and set an application path and work folder in the project TestedApp for running through a pipeline because the pipeline deploys the application to a non-standard path.

let Section = Storages.Registry("SOFTWARE\\WOW6432Node\\<_yourSectionName>\\", HKEY_LOCAL_MACHINE);
let regKey = Section.GetSubSection(<_yourSubSectionName>).Name;
let Path = Storages.Registry(regKey, HKEY_LOCAL_MACHINE, 0, true).GetOption("", "");
let WorkFolder = Storages.Registry(regKey, HKEY_LOCAL_MACHINE, 0, true).GetOption("Path", "");
let appIndex = TestedApps.Find(<_yourAppName>);
if (appIndex >= 0){
  if(TestedApps.Items(<_yourAppName>).Path != Path){
    TestedApps.Items(<_yourAppName>).Path = Path
  }
  if(TestedApps.Items(<_yourAppName>).WorkFolder != WorkFolder){
    TestedApps.Items(<_yourAppName>).Params.ActiveParams.WorkFolder = WorkFolder; 
  }
}
else{
  Log.Error("TestedApp " + <_yourAppName> + " does not Exist.")
  Runner.Stop(true); 
}   

I hope you find these links and code examples as useful as I have!

Have a great day!

Scott Holmes
WW Wood Products Inc.

  • JDR2500's avatar
    JDR2500
    Frequent Contributor

    Using VBScript we write string, DWORD and binary values anywhere in HKEY_CURRENT_USER using the following routine.

    'Example usage:
    '	SetRegistryValue ("Software\SmartBear\TestComplete\15.0", "EdgeReloadRequired", 1, "DWORD")
    '___________________________________________________________________________________________________________________________________________________
    Sub SetRegistryValue(s_KeyPath, s_ValueName, s_Value, s_Type)
    	
       Const HKEY_CURRENT_USER = &H80000001
       strComputer = "."
       Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
    	
    	If (s_Type = "DWORD") Then
    		objRegistry.SetDWORDValue HKEY_CURRENT_USER, s_KeyPath, s_ValueName, s_Value
    	ElseIf (s_Type = "Binary") Then 'If the registry is binary 
    		uBinary = Split(s_Value) 'This will split up a string into an array using the space as a splitter. 
                    'This will convert each segment of the array into hex rather than string
    		For i = 0 To UBound(uBinary) : uBinary(i) = CByte("&H" & uBinary(i)) : Next 
                    'This will write the array of hex values to a binary value in the registry
    		Call objRegistry.SetBinaryValue(HKEY_CURRENT_USER, s_KeyPath, s_ValueName, uBinary) 
    	ElseIf (s_Type = "String") Then
    		objRegistry.SetStringValue HKEY_CURRENT_USER, s_KeyPath, s_ValueName, s_Value
    	End If
       Set objRegistry = Nothing
    
    End Sub

    To read DWORD or String values from HKEY_CURRENT_USER we use this function:

    'Example:
    '	AtsGetRegistryValue ("Software\SmartBear\TestComplete\15.0", "EdgeReloadRequired", "DWORD")
    '___________________________________________________________________________________________________________________________________________________
    Function AtsGetRegistryValue(s_KeyPath, s_ValueName, s_Type)
    	
       Const HKEY_CURRENT_USER = &H80000001
       strComputer = "."
       Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
    	If (s_Type = "DWORD") Then
    		objRegistry.GetDWORDValue HKEY_CURRENT_USER, s_KeyPath, s_ValueName, AtsGetRegistryValue 'Assign the DWORD to the "Function". This 
                    will make the DWORD the returned value for the function. 
    	ElseIf (s_Type = "String") Then
    		objRegistry.GetStringValue HKEY_CURRENT_USER, s_KeyPath, s_ValueName, AtsGetRegistryValue
    	End If
      Set objRegistry = Nothing
       
    End Function