Results 1 to 2 of 2

Thread: registry - creating a key

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Ireland
    Posts
    29
    When running my app I want to check the registry for
    a test key to see if it is the first time it has been run.
    If this is not found, I want to create the key.

    If the key does not exist, it creates a new key called
    ishere under check when I actually want it to create
    a string under the check key containing "Hello".

    I am using the following code:

    Private Function GetKeys()

    Dim testreturn As Integer
    Dim real As String
    Dim encr As String
    Dim subkeytest As String
    Dim key_Return As Long
    Dim hregkey As Long
    Dim secattr As SECURITY_ATTRIBUTES
    Dim neworused As Long
    Dim stringbuffer As String

    HKEY_LOCAL_MACHINE = &H80000002
    KEY_READ = &H20019
    KEY_WRITE = &H20006

    secattr.nLength = Len(secattr)
    secattr.lpSecurityDescriptor = 0
    secattr.bInheritHandle = 1

    subkeytest = "\SOFTWARE\co_name\sysname\010000\check\ishere"

    stringbuffer = "Hello" + vbNullChar

    REG_SZ = 1

    'Check for test key

    key_Return = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkeytest, 0, KEY_READ, hregkey)
    If key_Return <> 0 Then
    'Error , key not opened - Tell the user
    MsgBox "This appears to be the first time this program has been run." + vbCrLf + "Please wait while the setup sets up!", vbOKOnly, "First Run"
    'Create the keys needed
    key_Return = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkeytest, 0, "", 0, KEY_WRITE, secattr, hregkey, neworused)
    If key_Return <> 0 Then
    'Create key failed
    MsgBox "Could not initialize application" + vbCrLf + "Please contact .....etc.", vbCritical, "Initialization Error - Oops!!"
    Else
    'Success - now set value
    key_Return = RegSetValueEx(HKEY_LOCAL_MACHINE, "SystemTest", 0, REG_SZ, ByVal stringbuffer, Len(stringbuffer))

    End If

    'Close the other key

    retval = RegCloseKey(hKeyreg)
    'Give default values

    Else
    'Get values of data keys
    End If
    End Function
    VB6 Enterprise

    Brian Mc Cashin

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Location
    Ireland
    Posts
    29
    My problem is in the line :

    key_Return = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkeytest, 0, KEY_READ, hregkey)

    It returns '2' which FormatMessage returns as 'Cannot Find File Specified'

    I have created the key ...\check\ishere
    and the string ..\check\ishere
    but I cannot open anything with this line.
    VB6 Enterprise

    Brian Mc Cashin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width