COM+ application intermittently stops responding while reading a registry key

The COM+ application randomly stops responding. To fix the problem the COM+ application has to be restarted. After some observation the problem appeared to be with reading a custom registry key.
We were not able to read registry key while it is in use by a COM+ application. Also when we try to access the registry key using regedit we are not able to do it.

We were using RegOpenKeyEx() function to get the handle to the registry key.

Private Function GetKeyValue(ByVal KeyD As Long, ByVal Key As String, ByVal SubKey
As String) As String

Dim value As String
Dim length As Long
Dim hKey As Long

If RegOpenKeyEx(KeyD, Key, 0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then
Err.Raise Err.Number, Err.Source, Err.Description
End If

length = 256
value = Space$(length)

If RegQueryValueEx(hKey, SubKey, 0&, 1&, ByVal value, length) <> ERROR_SUCCESS
Err.Raise Err.Number, Err.Source, Err.Description

value = Left$(value, length - 1)
GetKeyValue = value
End If

End Function



__in HKEY hKey,
__in LPCTSTR lpSubKey,
DWORD ulOptions,
__in REGSAM samDesired,
__out PHKEY phkResult


A pointer to a variable that receives a handle to the opened key. If the key is not
one of the predefined registry keys, call the RegCloseKey function after you have
finished using the handle.

We were reading one of the non-predefined keys so we need to explicitly close the handle to the key.

We implemented the RegCloseKey() function to explicitly release the handle to the registry key.

This resolved the issue.