• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 817
  • Last Modified:

Registry Key LOCKED? What the ?????

Hello Experts, here’s hoping someone out there has already gone though this problem and found a solution.  It’s kind of a strange issue so I will do my best to describe it.  The simplest way to describe it would be that the registry just LOCKS the key that my program is trying to access.  Or maybe its vise versa and my program locks the Registry Key, I just don’t know.

Here is a run down.  I am developing in Visual Basic 6.  Inside my app I have a timer that goes off in X minute intervals where X can be defined by the user of the program.  The program then reads a value which happens to be a website URL from the registry.  This URL may also be changed by the user (but anyway that’s probably not important).  Ok so the app works great, and reads from the registry, but then when you come back to the computer a few days later its like the registry key is completely locked.  My app has generated an error that it can not read from the registry.  To rule out a false error message I opened RegEdit.exe and navigate to the key in question.  It is indeed LOCKED.

As soon as I try to click on the key using RegEdit.exe I get this error message:
“Cannot Open KeyName:  Error while opening key”

I have checked permissions on the key and they are set properly, but it’s not a permission issue because the app works find for a few days before the key becomes locked.

Ok so the solution to unlocking they key is to close the App that I programmed and re-open it.  Once I do that RegEdit.exe can access the key, and my app is also able to access the key for another couple days.

So my questions are…. What is locking the key?  Why is the key locking, and why does it happen for no apparent reason after a few days.  Even the timing on it is not consistent.

Please someone help!
0
mydware
Asked:
mydware
  • 2
  • 2
2 Solutions
 
rettiseertCommented:
Sounds your program is not closing the handle of the key properly.

What method are you using to deal with registry?

I've used
http://www.vbaccelerator.com/codelib/inireg/registry.htm
with no problems. Maybe that can help you.
0
 
andrewbleakleyCommented:
let go of the key once you have finished reading it, and close every handle explicitly, plain and simple you have left it open at some point and no one else (including windows) will be able to get to it.

Check you application, it may crap out somewhere or hit a GoTo that results in the handle staying open after the routine has completed - check that everywhere in the routine that reads the key that you close all handles at every exit point.

0
 
mydwareAuthor Commented:
Ahhhhh thats starts to make some sense :)

Here is my code to Get registry values
Public Function GetString(hKey As Long, strPath As String, strValue As String)
    ' *** Setup error hanlder
    On Error GoTo errHand
   
    'EXAMPLE:
    '   text1.text = getstring(HKEY_CURRENT_USER, "Software\VBW\Registry", "String")
   
    Dim lValueType As Long
    Dim r As Long
    Dim keyhand As Long
    Dim datatype As Long
    Dim lResult As Long
    Dim strBuf As String
    Dim lDataBufSize As Long
    Dim intZeroPos As Integer
    r = RegOpenKey(hKey, strPath, keyhand)
    lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)


    If lValueType = thisREG_SZ Then
        strBuf = String(lDataBufSize, " ")
        lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)


        If lResult = ERROR_SUCCESS Then
            intZeroPos = InStr(strBuf, Chr$(0))


            If intZeroPos > 0 Then
                GetString = left$(strBuf, intZeroPos - 1)
            Else
                GetString = strBuf
            End If
        End If
    End If
       
    Exit Function
errHand:
    Call GLOBAL_ERROR_HANDLER(Err.number, Err.Description, "modReg", Err.Source, "GetString()", Erl(), True, True)
End Function


And here is my code to Set Registry Values
Public Sub SaveString(hKey As Long, strPath As String, strValue As String, strdata As String)
    ' *** Setup error hanlder
    On Error GoTo errHand
     
    'EXAMPLE:
    '    Call savestring(HKEY_CURRENT_USER, "Software\VBW\Registry", "String", text1.text)
    Dim keyhand As Long
    Dim r As Long
    r = RegCreateKey(hKey, strPath, keyhand)
    r = RegSetValueEx(keyhand, strValue, 0, thisREG_SZ, ByVal strdata, Len(strdata))
    r = RegCloseKey(keyhand)
       
    Exit Sub
errHand:
    Call GLOBAL_ERROR_HANDLER(Err.number, Err.Description, "modReg", Err.Source, "SaveString()", Erl(), True, True)
End Sub


But I think the problem must be in the GetString function.  Just wondering what the experts think about this.....
Thanks for the posts.
0
 
andrewbleakleyCommented:
Add a RegCloseKey(keyhand) to the error handler and just abobe the Exit function in your  GetString function.

Windows gets antsy when you leave file handles open like that.

The reason that restarting you application fixes it is because when you application exits all its open file handles are closed.
0
 
mydwareAuthor Commented:
Ok those changes have been made.  And they make sense too :)

Now we have to wait a few days to see if the app does not crash.  Usually it takes 1-3 days before it gives an error.

I will keep you guys posted, and thanks for the good answers.  Was scratching my head on that one a bit.

Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now