Solved

Read a value from Registry

Posted on 2001-08-02
13
1,282 Views
Last Modified: 2008-02-26
I'm getting the feeling it is not easy, or perhaps even possible, but does anyone have a way to read the registry either directly within VBScript in an ASP, or with a COM object within an ASP, or ...

TIA, K
0
Comment
Question by:KMAN
13 Comments
 
LVL 7

Expert Comment

by:John844
Comment Utility
I have some code to do it within a com object.
0
 
LVL 7

Accepted Solution

by:
John844 earned 100 total points
Comment Utility
'sample usage - Debug.Print RegistryGetKeyValue(HKEY_CLASSES_ROOT, "COMCTL.ListviewCtrl.1\CLSID", "")


'Registry Key Security Options...
Private Const READ_CONTROL = &H20000
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
Private Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
Private Const KEY_EXECUTE = KEY_READ
Private Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
                       KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
                       KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
'Registry Return Values
Private Const REGISTRY_ERROR_SUCCESS = 0

'Registry Data Types...
Private Const REG_SZ = 1                         ' Unicode nul terminated string
Private Const REG_EXPAND_SZ = 2                  ' Unicode nul terminated string
Private Const REG_DWORD = 4                      ' 32-bit number

Private Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted


Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Boolean
End Type

Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" _
 (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
  ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" _
 (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
  ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" _
 (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
  ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
  ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, _
  ByRef lpdwDisposition As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" _
 (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
  ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long


' Reg Key ROOT Types...
Public Enum HKEYTypes
    HKEY_CLASSES_ROOT = &H80000000
    HKEY_CURRENT_USER = &H80000001
    HKEY_LOCAL_MACHINE = &H80000002
    HKEY_USERS = &H80000003
    HKEY_PERFORMANCE_DATA = &H80000004
End Enum


'-------------------------------------------------------------------------------------------------
'sample usage - Debug.Print RegistryGetKeyValue(HKEY_CLASSES_ROOT, "COMCTL.ListviewCtrl.1\CLSID", "")
'-------------------------------------------------------------------------------------------------
Public Function RegistryGetKeyValue(KeyRoot As HKEYTypes, KeyName As String, SubKeyRef As String) As String
    Dim i As Long                                           ' Loop Counter
    Dim rc As Long                                          ' Return Code
    Dim hKey As Long                                        ' Handle To An Open Registry Key
    Dim hDepth As Long                                      '
    Dim sKeyVal As String
    Dim lKeyValType As Long                                 ' Data Type Of A Registry Key
    Dim tmpVal As String                                    ' Tempory Storage For A Registry Key Value
    Dim KeyValSize As Long                                  ' Size Of Registry Key Variable
   
    ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
    '------------------------------------------------------------
    rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_QUERY_VALUE, hKey) ' Open Registry Key
   
    If (rc <> REGISTRY_ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Error...
   
    tmpVal = String$(1024, 0)                             ' Allocate Variable Space
    KeyValSize = 1024                                       ' Mark Variable Size
   
    '------------------------------------------------------------
    ' Retrieve Registry Key Value...
    '------------------------------------------------------------
    rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
                         lKeyValType, tmpVal, KeyValSize)    ' Get/Create Key Value
                       
    If (rc <> REGISTRY_ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Errors
     
    tmpVal = Left$(tmpVal, InStr(tmpVal, Chr(0)) - 1)

    '------------------------------------------------------------
    ' Determine Key Value Type For Conversion...
    '------------------------------------------------------------
    Select Case lKeyValType                                  ' Search Data Types...
    Case REG_SZ, REG_EXPAND_SZ                              ' String Registry Key Data Type
        sKeyVal = tmpVal                                     ' Copy String Value
    Case REG_DWORD                                          ' Double Word Registry Key Data Type
        For i = Len(tmpVal) To 1 Step -1                    ' Convert Each Bit
            sKeyVal = sKeyVal + Hex(Asc(Mid(tmpVal, i, 1)))   ' Build Value Char. By Char.
        Next
        sKeyVal = Format$("&h" + sKeyVal)                     ' Convert Double Word To String
    End Select
   
    RegistryGetKeyValue = sKeyVal                                   ' Return Value
    rc = RegCloseKey(hKey)                                  ' Close Registry Key
    Exit Function                                           ' Exit
   
GetKeyError:    ' Cleanup After An Error Has Occured...
    RegistryGetKeyValue = vbNullString                      ' Set Return Val To Empty String
    rc = RegCloseKey(hKey)                                  ' Close Registry Key
End Function
0
 
LVL 5

Author Comment

by:KMAN
Comment Utility
I have not created a COM object (DLL?) with VB before... Would I paste this code into a new VB project?  What kind of VB Project? Or is there some other way?

Thanks, K
0
 
LVL 7

Expert Comment

by:John844
Comment Utility
I would paste the code into a module

then In you class module add a function that calls RegistryGetKeyValue
0
 
LVL 7

Expert Comment

by:John844
Comment Utility
create an activeX.dll project
change your project name to something like prjRegistry

add a class module.  change it's name to something like GetRegistry

add a module. change its name

add the code to the module

create a public function and call it something like getRegistrySetting

in the function call the RegistryGetKeyValue and pass the needed info.

compile the dll

call the dll from your asp page like

dim obj

set obj = server.createobject("prjRegistry.GetRegistry")
strValue = obj.getRegistrySetting()

you can add more paramters to make it more functional, but this is the basic template.
0
 
LVL 5

Author Comment

by:KMAN
Comment Utility
Where do I copy the dll to?  Do I have to register the DLL?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 7

Expert Comment

by:John844
Comment Utility
copy the dll to your web server and run regsvr32 c:\winnt\system32\prjRegistry.dll

or you can copy the dll to the web server
open mts console, create a package and add the component to the package.  This will be the best method to do as it will let you make updates to the dll without stoping the web services.
0
 
LVL 2

Expert Comment

by:shirjeel
Comment Utility
Dear KMAN,

Can u tell me that whose registry you wana read. If u wana read the registry of client and u have to make active x control which will download on the client system and reads the registry. Otherwise there is no way to read client registry.

Regards,
Shirjeel.
0
 
LVL 5

Author Comment

by:KMAN
Comment Utility
shirjeel -
Its a local value.

John -

I think I am close, but I am not able to recompile my DLL after I've registered it.  I try unregistering it, but it still is locked.  I will try the MTS Suggestion but not familiar with that piece.

Thanks, K
0
 
LVL 5

Expert Comment

by:raizon
Comment Utility
listening

John I've been looking for something like that.  Looks good :-)

Raizon
0
 
LVL 5

Author Comment

by:KMAN
Comment Utility
shirjeel -
Its a local value.

John -

I think I am close, but I am not able to recompile my DLL after I've registered it.  I try unregistering it, but it still is locked.  I will try the MTS Suggestion but not familiar with that piece.

Thanks, K
0
 
LVL 18

Expert Comment

by:mgfranz
Comment Utility
Nice work John!  Here's another way;

'PLACE ALL THIS INTO A NEW MODULE or IN Declarations OF YOUR FORM

Public Sub CreateKey(Folder As String, Value As String)

Dim b As Object
On Error Resume Next
Set b = CreateObject("wscript.shell")
b.RegWrite Folder, Value

End Sub

Public Sub CreateIntegerKey(Folder As String, Value As Integer)

Dim b As Object
On Error Resume Next
Set b = CreateObject("wscript.shell")
b.RegWrite Folder, Value, "REG_DWORD"


End Sub

Public Function ReadKey(Value As String) As String

'READ FROM WINDOWS REGISTRY
'.........................
Dim b As Object
On Error Resume Next
Set b = CreateObject("wscript.shell")
r = b.RegRead(Value)
ReadKey = r
End Function


Public Sub DeleteKey(Value As String)
'DELETE VALUES FROM WINDOWS REGISTRY
'-----------------------------------
Dim b As Object
On Error Resume Next
Set b = CreateObject("Wscript.Shell")
b.RegDelete Value
End Sub

'form==============
'To Create an registry key:
CreateKey "HKEY_CURRENT_USER\Software\My Software\Version","1.45"

'To Create an Integer registry key:
CreateIntegerKey "HKEY_CURRENT_USER\Software\My Software\Number","50"

'To Read from the registry
msgbox "Version is " & ReadKey("HKEY_CURRENT_USER\Software\My Software\Version"

'To Delete from the registry
DeleteKey "HKEY_CURRENT_USER\Software\My Software\Version"

' Instead of writing out HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER etc, you can
' use abbreviations such as "HKCU\Software"



0
 
LVL 5

Author Comment

by:KMAN
Comment Utility
Works real well, Thanks!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now