Solved

how do i display the value of a key in a label?

Posted on 1998-07-07
23
152 Views
Last Modified: 2010-05-03
i need to disply theses keys in a label. BUT how??

HKEY_LOCAL_MACHINES\Software\Microsoft\Windows\CurrentVersion\RegisteredOwner

HKEY_LOCAL_MACHINES\Softeare\Microsoft\Windows\CurrentVersion\RegisteredOrganization

HKEY_LOCAL_MACHINES\Software\Microsoft\Windows\CurrentVersion\RegisteredOwner
0
Comment
Question by:jmharr
  • 13
  • 10
23 Comments
 
LVL 39

Accepted Solution

by:
abel earned 180 total points
Comment Utility
You will have to use RegOpenkey and RegQueryValue (both are API calls). I'll post in ten minutes a full sample code for you.

Regards, Abel
0
 

Author Comment

by:jmharr
Comment Utility
i mean these keys,

HKEY_LOCAL_MACHINES\Software\Microsoft\Windows\CurrentVersion\RegisteredOwner

HKEY_LOCAL_MACHINES\Softeare\Microsoft\Windows\CurrentVersion\RegisteredOrganization

HKEY_LOCAL_MACHINES\Software\Microsoft\Windows\CurrentVersion\productid
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Alright, here it goes, just add this function in a module:

Function ReadKey(Value As String)
    Dim hkeyResult As Long
    Dim lLength As Long
    Dim sdata As String * 255
    lLength = 255

    MsgBox RegOpenKeyEx HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\", 0, KEY_QUERY_VALUE, hkeyResult
    RegQueryValueEx hkeyResult, Value, 0, REG_SZ, sdata, lLength
    RegCloseKey hkeyResult
    ReadKey = sdata
End Function


And add these declarations to your declaration-section:
Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const KEY_QUERY_VALUE = &H1
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long


Call the function like:
Result = ReadKey("RegisteredOwner")
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Sorry, that MsgBox (with "RegOpenKeyEx") is a "Cut 'n' Paste" typo of me. Should be deleted of course...
0
 

Author Comment

by:jmharr
Comment Utility
will that doo all 3 keys? and display in 3 labels??
0
 

Author Comment

by:jmharr
Comment Utility
it does not work!!!
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
OK, my next comment will give you a full code sample with all the three keys in three labels. I thought a function would suffice, I thought you would understand that you could call it like this:

label1.caption = ReadValue("RegisteredOwner")
label2.Caption = ReadValue("RegisteredOrganisation")
label3.Caption = ReadValue("productid")

But you are right about "not working".
Here's a revised sample of ReadValue (a better name than ReadKey, I think). This one is more like what "the books" say. It does some errorhandling and it will not crash the program (I hope).
I added one function more, you might need it more often when using API calls. The function is "StripTerminator". Also: I added the "Declaration section" at the bottom of this, since I altered it a little.

Hope this does the trick for you. Place the "Label.Caption" lines in the Form_Load event and place three labels on the form, than you're complete.


Function ReadValue(Value As String)
    Dim hKey As Long
    Dim lDataBufSize As Long
    Dim lValueType As Long
    Dim strBuf As String
       
    RegOpenKeyEx HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", 0, KEY_QUERY_VALUE, hKey
    lresult = RegQueryValueEx(hKey, Value, 0&, lValueType, ByVal 0&, lDataBufSize)
    If lresult = ERROR_SUCCESS Or lresult = ERROR_MORE_DATA Then
        If lValueType = REG_SZ Then
            strBuf = Space(lDataBufSize)
            lresult = RegQueryValueEx(hKey, Value, 0&, 0&, ByVal strBuf, lDataBufSize)
            If lresult = ERROR_SUCCESS Then
                ReadValue = StripTerminator(strBuf)
            End If
        End If
    End If
End Function


Function StripTerminator(ByVal strString As String) As String
    Dim intZeroPos As Integer

    intZeroPos = InStr(strString, Chr$(0))
    If intZeroPos > 0 Then
        StripTerminator = Left$(strString, intZeroPos - 1)
    Else
        StripTerminator = strString
    End If
End Function


'The declaration-section goes here:
Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const KEY_QUERY_VALUE = &H1
Public Const ERROR_MORE_DATA = 234
Public Const ERROR_SUCCESS = 0&

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByRef lpData As String, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Sorry, I said "my next comment", but I pasted all the code in the same comment. Let me know when you need more info. The sample is thoroughly tested and should work for you, unless you use VB4 16 bit or lower.
Some lines are longer than the could fit on one line and I didn't include the line-continuation character ("_"), but you probably understand how to paste it in you project.

Regards, Abel
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Some more info for your convenience:
The function "StripTerminator" deletes the terminating NULL-character at the end of strings returned from API-calls. Most API-calls return string-values "zero-terminated", that is with a Chr(0) at the end of the string. They do so for standardisation in function-calling/returning. If you know C(++), you probably have heard more often of this.
Since a NULL-terminated string can cause problems in VB, it's better to strip that terminating NULL-character from the string. That's why I added the function "StripTerminator" for you (you will find that function in the VB-setupwizard files as well...).

Regards, Abel
0
 

Author Comment

by:jmharr
Comment Utility
Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const KEY_QUERY_VALUE = &H1
Public Const ERROR_MORE_DATA = 234
Public Const ERROR_SUCCESS = 0&
, they Do not WORK!!, they go in form declaration right?
0
 

Author Comment

by:jmharr
Comment Utility
This does not work,

Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const KEY_QUERY_VALUE = &H1
Public Const ERROR_MORE_DATA = 234
Public Const ERROR_SUCCESS = 0&
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByRef lpData As String, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long.

Its in the form declarations. I get this error;

Constants, fixed-length strings, arrays, and Declare statements not allowed as Public members of an object module


0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:jmharr
Comment Utility
This does not work,

Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const KEY_QUERY_VALUE = &H1
Public Const ERROR_MORE_DATA = 234
Public Const ERROR_SUCCESS = 0&
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByRef lpData As String, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long.

Its in the form declarations. I get this error;

Constants, fixed-length strings, arrays, and Declare statements not allowed as Public members of an object module


0
 
LVL 39

Expert Comment

by:abel
Comment Utility
nop :-)

When I said "add this function in a module", I meant "in a module", the functions as well as the declarations.

I guess you're new to this, so I'll take the chance and give you a little info about scope(s), modules and forms in VB (btw, are you using VB5?).

1.  A module is used to store functions/subs in it that are globally accessable (that is, throughout the project). Only when you write "private" in front of a function/variable, it will be locally accessable (that is, only in that module). The declaration-section of the module is used to store variables/declarations of functions that are globally accessable (that's what the "public" keyword is for)
2.  A form has only the form as default scope (look at all the standard event-functions, they all say "private"). It is not possible to store variables/declarations in the declarations-section of the form as "public". You HAVE to use the declarations-section of the module for it.

All depends on the way you want to built/set up your project. If you want to store every function in the form, you can do that, but make sure you write "private" at all the places where I wrote "public". When you want to write your code in different places (i.e. in modules, classmodules, ocx's, dll's and forms) you can use the instructions I gave you.

If you want some more info on "public", "private" (and maybe "friend") you might better look it up in your VB-reference, since that will be a much more comprehensive story about the scopes (there is much more to say about this subject, but that's beyond the "scope" of your question).

For your convenience: just place everything in the form (the functions as well) and delete every "public" keyword you find. That will work as well.

Hope you'll get it working now..

Regards, Abel
0
 

Author Comment

by:jmharr
Comment Utility
This does not work,

Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const KEY_QUERY_VALUE = &H1
Public Const ERROR_MORE_DATA = 234
Public Const ERROR_SUCCESS = 0&
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByRef lpData As String, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long.

Its in the form declarations. I get this error;

Constants, fixed-length strings, arrays, and Declare statements not allowed as Public members of an object module


0
 
LVL 39

Expert Comment

by:abel
Comment Utility
btw. When you get that error message and hit F1, you'll see some more details about "scope" as well...
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
Sorry, I was not completely correct: you can delete every "public" statement, but you will need to add before every "Declare" statement a "private" statement. Like this (unless, of course, you place it in a module...):

Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByRef lpData As String, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

0
 

Author Comment

by:jmharr
Comment Utility
I am not gettting the RegistedOrganizion
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
The others work fine now?

Try, in stead of "RegistedOrganizion", "RegisteredOrganization", that should be the right one.
If you don't get anything, lookup the key in your Registry, and see if it exists and if it has a value set.

Regards, Abel
0
 

Author Comment

by:jmharr
Comment Utility
I would give u more points, but now, i have no points! i'm broke! Thanks!
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
You welcome. I'm glad to have helped you out.
0
 

Author Comment

by:jmharr
Comment Utility
i also need it to display the values to theses keys in lables; HKEY_LOCAL_MACHINE\Hardware\Description\System\CentralProcessor\VeddorIdentifier

HKEY_LOCAL_MACHINE\Hardware\Description\System\CentralProcessor\Identifier,

and i need it to find out how much ram u have, and display that on a label
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
In stead of this:
RegOpenKeyEx HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion", 0, KEY_QUERY_VALUE, hKey

you should call this:
RegOpenKeyEx HKEY_LOCAL_MACHINE, "Hardware\Description\System\CentralProcessor", 0, KEY_QUERY_VALUE, hKey

To make your code easier to modify, place the key in a variable. By now you should be able to do that.
About the RAM, that's a totally different question. But to direct you in the right way: lookup the documentation about the Win32 API function GlobalMemoryStatus. You can find the declaration in the API-viewer provided with VB.

Reagards, Abel
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
By the way: the information you are trying to retrieve via the Registry, can be easily get with one single API function, namely "GetSystemInfo". It gives you the processsorid, vendorid and much, much more. Lookup the documentation and use that in stead of retrieving it via the registry.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now