RegQueryValueEx

  • Thread starter Steven Licciardi
  • Start date
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
 
D

Daniel Moth

The code you pasted in does not compile so unless you post a full sample I
cannot help

A VB declaration of RegQueryValueEx follows (in case it helps you) but I do
suggest you use the registry class from opennetcf.
<System.Runtime.InteropServices.DllImport("coredll.dll",
EntryPoint:="RegQueryValueEx", SetLastError:=True)> _

Private Shared Function RegQueryValueEx(ByVal hKey As Int32, ByVal
lpValueName As String, ByVal lpReserved As Int32, ByRef lpType As KeyType,
ByVal lpData() As Byte, ByRef lpcbData As Int32) As Int32

End Function

Private Enum KeyType

REG_SZ = 1

REG_EXPAND_SZ = 2

REG_BINARY = 3

REG_DWORD = 4

REG_MULTI_SZ = 7

End Enum


Cheers
Daniel
 
S

Steven Licciardi

Seems that if I change

ByVal lpValueName As Char()

to

ByVal lpValueName As String

in my function declaration (as per the example you sent), it seems to work
more reliably, so far anyway :).

Thanks,

Steven
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top