[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 1998-07-07
23
Medium Priority
?
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 10
23 Comments
 
LVL 39

Accepted Solution

by:
abel earned 720 total points
ID: 1465114
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
ID: 1465115
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
ID: 1465116
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Expert Comment

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

Author Comment

by:jmharr
ID: 1465118
will that doo all 3 keys? and display in 3 labels??
0
 

Author Comment

by:jmharr
ID: 1465119
it does not work!!!
0
 
LVL 39

Expert Comment

by:abel
ID: 1465120
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
ID: 1465121
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
ID: 1465122
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
ID: 1465123
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
ID: 1465124
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
 

Author Comment

by:jmharr
ID: 1465125
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
ID: 1465126
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
ID: 1465127
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
ID: 1465128
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
ID: 1465129
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
ID: 1465130
I am not gettting the RegistedOrganizion
0
 
LVL 39

Expert Comment

by:abel
ID: 1465131
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
ID: 1465132
I would give u more points, but now, i have no points! i'm broke! Thanks!
0
 
LVL 39

Expert Comment

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

Author Comment

by:jmharr
ID: 1465134
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
ID: 1465135
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
ID: 1465136
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

656 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