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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ONLY Reading From The Registry 1

Status
Not open for further replies.

JAG14

Programmer
Sep 26, 2003
469
GB
Hi All,

I know this is well-used subject, however I have always use Save and GetSetting for my Registry values.

Now I need to read one value from the registry and use it in my program.
The previous thread search and FAQ's are very full and complete for read/write, however, I want to compact this code into just READING from the registry, and moreso, just reading ONE key.

I need a very code-efficient solution, and all help will be very gratefully received, I can't make head nor tail of the read/write code to compact it myself!!! (All Say Duh!)

Many Thanks in Advance. [spineyes]


jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
> I need a very code-efficient solution

And you're using VB? [smile]

The below should help you a bit:
Code:
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long


Public Function GetKeyValue(strKey As String, strValue As String) As String
  Const cstrRoutineName As String = "GetKeyValue"
  Const clngHKLM As Long = &H80000002
  Const READ_CONTROL = &H20000
  Const KEY_QUERY_VALUE = &H1
  Const KEY_SET_VALUE = &H2
  Const KEY_CREATE_SUB_KEY = &H4
  Const KEY_ENUMERATE_SUB_KEYS = &H8
  Const KEY_NOTIFY = &H10
  Const KEY_CREATE_LINK = &H20
  Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
                       KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
                       KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
  ' Return code constants
  Const ERROR_NONE = 0
  Const ERROR_BADDB = 1
  Const ERROR_BADKEY = 2
  Const ERROR_CANTOPEN = 3
  Const ERROR_CANTREAD = 4
  Const ERROR_CANTWRITE = 5
  Const ERROR_OUTOFMEMORY = 6
  Const ERROR_ARENA_TRASHED = 7
  Const ERROR_ACCESS_DENIED = 8
  Const ERROR_INVALID_PARAMETERS = 87
  Const ERROR_NO_MORE_ITEMS = 259
  Dim lngR As Long ' Resultcode
  Dim lngH As Long ' Handle
  Dim strX As String ' returned value
  Dim lngX As Long ' returned length
  Dim lngT As Long ' Key type

  lngR = RegOpenKeyEx(clngHKLM, strKey, 0, KEY_ALL_ACCESS, lngH)
    
  strX = vbNullString
  If (lngR = ERROR_NONE) Then
    'key opened successfully
    strX = String(1024, 0)
    lngX = Len(strX)
    ' Get key value
    lngR = RegQueryValueEx(lngH, strValue, 0, lngT, strX, lngX)
    If lngR = 0 Then
      ' Value returned
      If Asc(Mid(strX, lngX, 1)) = 0 Then lngX = lngX - 1
      strX = Left(strX, lngX)
    End If
  End If
  lngR = RegCloseKey(lngH)
  GetKeyValue = strX

End Function

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
I to found ALOT of stuff in working with the registry that I finally built everything into my custom library. I have noted in here before if anyone would like to try it out they are more then welcome to do so. Simply send me an e-mail and I'll ship you off a copy of the library. I do alot of registry access in our projects here and now I can, read, write, create, even scan the registry with only having to create the object and issue 1 to 2 lines of code. If your interested let me know robc@rncdevelopment.com, if not no problem...just like to help out other programmers as I can.
 
Andy,

Looks fantastic.. One Problem... I'm a registry philistine!!!

How would use this to get the value of
HKLM\SOFTWARE\KYE\HardwareID
and load into a string?

Sorry to be a pain in the ass.

Thanks a million..



jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Jag, just FYI...as I was mentioning about my library to retrieve that key if you were using my library would be as simple as

Code:
Dim RegQuery as New RNCDev_Library.Registry_Calls

KeyValue = RegQuery.Key_GetValue(LOCAL_MACHINE, "SOFTWARE\KYE", "HARDWAREID")
 
Jag

I sympathise!
Code:
strKey = "Software\KYE"
strValue = "HardwareID"
So:
Code:
strResult = GetKeyValue("Software\KYE", "HardwareID")
Now that wasn't so bad, was it?

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Rob,

I don't want to have an extra library in my program,

If you red the first post, i'm looking to this with the shortest code (and space) possible. An extra library containing many registry functions would not suit my needs.

However, i'm sure it's simply fantastic.


jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
No problem Jag, as I said if I can help great...if not thats ok but atleast I tried.
 
> However, i'm sure it's simply fantastic.

EVERY piece of code I write is fantastic until I try and get someone else to use it!

Got it working, Jag?

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Andy,

No I didn't.

I just need the code on how to call a the function to get the RegKey.

I tried
[tt]
strX = GetKeyValue("HKEY_LOCAL_MACHINE\Software\Microsoft\DirectX\Version\", "Version")
[/tt]

to no avail. It returns an empty string.!

Sorry again.

Thx for your help


jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Try:
Code:
strX = GetKeyValue("Software\Microsoft\DirectX", "Version")
That should work, but see above disclaimer!


Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Jag

Just tested this myself and it does work.

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Andy,

That works a dream.

A * for you good sir.

Many Thanks..

jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Pleased to be able to help.

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
I stumbled across this topic while looking for a way to detect the presence of QuickTime on a users machine. I tried the example that you showed:

GetKeyValue("Software\Microsoft\DirectX", "Version")

And it returned a value just fine. But When I try to change it to read the following, It is just blank.

GetKeyValue("SOFTWARE\Apple Computer, Inc.\QuickTime", "Version")

Any Ideas?
 
Should that comma be in the key name? Should it be a space?

GetKeyValue("SOFTWARE\Apple Computer Inc.\QuickTime", "Version")

Paul Bent
Northwind IT Systems
 
In Regedit, I right click the key and select "Copy key name" and then I Paste and I get:

HKEY_LOCAL_MACHINE\SOFTWARE\Apple Computer, Inc.

I thaought that was peculure as well....
 
Serial,

Use Andy's Code with the Command

[tt]
GetKeyValue("SOFTWARE\Apple Computer, Inc.\QuickTime\LocalUserPreferences", "FolderPath")
[/tt]

If this value return an empty string then QuickTime is not installed. Anything other than empty and it is installed.

Hope this helps.

jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Jag

I see you're becoming an expert at this!



Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Andy,

The keys I can do, the registry code is new to me... Notice I didn't repost it? I just put a referral to it because I'm having trouble to understanding it.
The way I see it... if it ain't broke!....etc.



jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top