[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Registry Key LOCKED?  What the ?????

Posted on 2005-05-05
Medium Priority
Last Modified: 2008-02-01
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!
Question by:mydware
  • 2
  • 2
LVL 13

Assisted Solution

rettiseert earned 200 total points
ID: 13936516
Sounds your program is not closing the handle of the key properly.

What method are you using to deal with registry?

I've used
with no problems. Maybe that can help you.
LVL 11

Expert Comment

ID: 13940359
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.


Author Comment

ID: 13940398
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
    '   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)
                GetString = strBuf
            End If
        End If
    End If
    Exit Function
    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
    '    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
    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.
LVL 11

Accepted Solution

andrewbleakley earned 300 total points
ID: 13940449
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.

Author Comment

ID: 13940464
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 :)

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

873 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