S
Steven Licciardi
I am using RegQueryValueEx to get a particular registry value, and so
identify a comport used by a bluetooth card. Usually this method works, but
sometimes it just returns nothing, when I know that the registry value
definately exists. The failure seems to happen quite randomly, below is an
example of what I am doing along with the P/Invoke bit.
Thanks,
Steven
Private Function GetValue(ByVal hKey As Int32, ByVal key As String, ByVal
valueName As String) As String
Dim value() As Byte
Dim size As String = 256
Dim kt As KeyType
Dim result As Int32 = RegQueryValueEx(hKey, valueName, 0, kt, Nothing,
size)
If result = 87 Then value =
System.Text.Encoding.ASCII.GetBytes(valueName & " does not exists")
If kt = KeyType.strng Then
Dim buffer(size) As Byte
RegQueryValueEx(hKey, valueName, 0, kt, buffer, size)
value = buffer
End If
If Not value Is Nothing Then
Return System.Text.Encoding.Unicode.GetString(value, 0,
value.Length)
Else
Return Nothing
End If
End Function
Public Function FindCommPort() As Integer
Dim comPortDetails As String =
"Socket-CF+_Personal_Network_Card_Rev_2.5-E3CE\2"
''get a list of available keys
Dim keys As ArrayList = EnumerateKeys("\Drivers\Active")
Dim hKey As Int32
Dim port, portdetails As String
Dim key As String
For Each key In keys
Try
If RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, 0, hKey) =
ERROR_SUCCESS Then
portdetails = GetValue(hKey, key, "key")
RegCloseKey(hKey)
End If
If RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, 0, hKey) =
ERROR_SUCCESS Then
port = GetValue(hKey, key, "Name")
RegCloseKey(hKey)
End If
If portdetails.IndexOf(comPortDetails) > -1 Then Return
Convert.ToInt32(port.Substring(3, 1))
Catch ex As Exception
''find out whats going wrong
Dim reg_error As String = ex.Message
End Try
Next
Return Nothing
End Function
Public Declare Function RegQueryValueEx Lib "coredll" _
Alias "RegQueryValueExW" ( _
ByVal hKey As Int32, _
ByVal lpValueName As Char(), _
ByVal Reserved As Int32, _
ByRef dwType As Int32, _
ByVal lpValue As Byte(), _
ByRef cbData As Int32) As Int32
identify a comport used by a bluetooth card. Usually this method works, but
sometimes it just returns nothing, when I know that the registry value
definately exists. The failure seems to happen quite randomly, below is an
example of what I am doing along with the P/Invoke bit.
Thanks,
Steven
Private Function GetValue(ByVal hKey As Int32, ByVal key As String, ByVal
valueName As String) As String
Dim value() As Byte
Dim size As String = 256
Dim kt As KeyType
Dim result As Int32 = RegQueryValueEx(hKey, valueName, 0, kt, Nothing,
size)
If result = 87 Then value =
System.Text.Encoding.ASCII.GetBytes(valueName & " does not exists")
If kt = KeyType.strng Then
Dim buffer(size) As Byte
RegQueryValueEx(hKey, valueName, 0, kt, buffer, size)
value = buffer
End If
If Not value Is Nothing Then
Return System.Text.Encoding.Unicode.GetString(value, 0,
value.Length)
Else
Return Nothing
End If
End Function
Public Function FindCommPort() As Integer
Dim comPortDetails As String =
"Socket-CF+_Personal_Network_Card_Rev_2.5-E3CE\2"
''get a list of available keys
Dim keys As ArrayList = EnumerateKeys("\Drivers\Active")
Dim hKey As Int32
Dim port, portdetails As String
Dim key As String
For Each key In keys
Try
If RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, 0, hKey) =
ERROR_SUCCESS Then
portdetails = GetValue(hKey, key, "key")
RegCloseKey(hKey)
End If
If RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, 0, hKey) =
ERROR_SUCCESS Then
port = GetValue(hKey, key, "Name")
RegCloseKey(hKey)
End If
If portdetails.IndexOf(comPortDetails) > -1 Then Return
Convert.ToInt32(port.Substring(3, 1))
Catch ex As Exception
''find out whats going wrong
Dim reg_error As String = ex.Message
End Try
Next
Return Nothing
End Function
Public Declare Function RegQueryValueEx Lib "coredll" _
Alias "RegQueryValueExW" ( _
ByVal hKey As Int32, _
ByVal lpValueName As Char(), _
ByVal Reserved As Int32, _
ByRef dwType As Int32, _
ByVal lpValue As Byte(), _
ByRef cbData As Int32) As Int32