Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Windows Registry

Status
Not open for further replies.

GregTheGeek

Programmer
Aug 11, 2004
46
US
Hello all,

I have a function that will dynamically build, based on passed parameters, a connection string to our mySQL server.

I need to be able to use this code on different machines, each possibly with different versions of the mySQL ODBC driver installed (3.51, 5.1, etc). I created a work-around basing the decision based on the computer name.

I would like to, instead, read the registry to find which mySQL ODBC driver version is installed. I have found the data I need in: [tt]HKLM\Software\MySQL AB[/tt]. I would like to iterate through the sub keys in the given path and find entries that contain "ODBC", each has a "Version" value that tells me what I should use in the connection string.

I am currently using APIs, but I'll admit that I don't have a lot of experience doing so. The example that I am trying to make work uses: [tt]RegQueryValueEx[/tt] in [tt]advapi32.dll[/tt].

Let me know if I can supply any more information to make my issue more clear. If any of you have a better idea to get this information, your input would certainly be appreciated.

TIA!

Greg The Geek

gtg.jpg

GTG
 
Here's a bit of code that I use to graze my way through the registry (suitably edited to remove extraneous stuff.)

Code:
[blue]... Initialization Code ...[/blue]

' Set the numeric value for the primary key.
MainKey = HKEY_LOCAL_MACHINE

' Set the Sub-key 
SubKey = "SOFTWARE\OLEforRetail\ServiceOPOS\"

' ... the Printer sub-key
PrinterKey = "POSPrinter"

' Select the sub-key for the Printer.
SubKey = SubKey & PrinterKey

' Open the key
Ret = RegOpenKeyExA(MainKey, SubKey, 0&, KEY_READ, lpHKey)
If Ret <> ERROR_SUCCESS Then
    Exit Function  'No key open, so leave
End If

' Set up a buffer to receive returned data.
' Adjust next value for larger buffers.
lpcbData = 255
ReturnedString = Space$(lpcbData)

' Loop through the keys until RegEnumKey returns ERROR_NO_MORE_ITEMS (259)
N = 0
Ret = RegEnumKey(lpHKey, CLng(N), ReturnedString, lpcbData)

Do Until Ret = ERROR_NO_MORE_ITEMS Or Ret <> ERROR_SUCCESS
    ReDim Preserve Devices(N)
    Devices(N) = Left$(ReturnedString, InStr(1, ReturnedString, Chr$(0)) - 1)
    N = N + 1
    Ret = RegEnumKey(lpHKey, CLng(N), ReturnedString, lpcbData)
Loop

[blue]... More Code ...[/blue]

I've omitted the Declares just to keep the size of the post down. You would of course, insert your own logic inside the loop to pick up the information that you need.
 
Golom,

Thank you for the code, it pointed me in the right direction. I finally have something working!

gtg.jpg

GTG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top