Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

User Name and Company

Posted on 2000-03-20
16
Medium Priority
?
217 Views
Last Modified: 2010-05-02
When we first install Windows, we're asked to enter our name and company.  How is it possible to fetch them with VB?
0
Comment
Question by:Sylvania
16 Comments
 
LVL 4

Expert Comment

by:gcs001
ID: 2636249
You should be able to retrieve these values from the registry.
Under Windows NT the key that it exists in is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
and the values can be obtained from
RegisteredOrganization and RegisteredOwner.

If you require some sample code let me know and I will be happy to post some here.

Regards,
Grant.
0
 

Author Comment

by:Sylvania
ID: 2636295
I would be happy to learn the easiest way to obtain these data via code.

I thank you.
0
 

Author Comment

by:Sylvania
ID: 2636320
I have Windows 95 and the key in registry is exactly the same... :)
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 1

Expert Comment

by:vbWayne
ID: 2636345
'get the Window's username of system
'Make a new project. Add a module.
'To the form add a label.

'Code:
'Add this code to the module:

Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) _
As Long

'this is form load


Dim s As String
Dim cnt As Long
Dim dl As Long
Dim CurUser As String
cnt = 199
s = String$(200, 0)
dl = GetUserName(s, cnt)
If dl <> 0 Then CurUser = Left$(s, cnt) Else CurUser = ""
msgbox  CurUser

0
 
LVL 1

Expert Comment

by:vbWayne
ID: 2636350
'get the user name of the computer
'put this in your bas module
'
Public Declare Function GetComputerName Lib _
"kernel32" Alias "GetComputerNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Public Function sGetComputerName() As String
  Dim sBuffer As String
  Dim lBufSize As Long
  Dim lStatus As Long
 
  lBufSize = 255
  sBuffer = String$(lBufSize, " ")
  lStatus = GetComputerName(sBuffer, lBufSize)
  sGetComputerName = ""
  If lStatus <> 0 Then
     sGetComputerName = Left(sBuffer, lBufSize)
  End If
  Form1.Label2 = sGetComputerName
 
End Function

'=====================================
'
'put this in the click event (load...whatever)
'
'
      Dim x
      x = sGetComputerName
0
 
LVL 1

Expert Comment

by:vbWayne
ID: 2636359
'get the user name of the computer
'put this in your bas module
'
Public Declare Function GetComputerName Lib _
"kernel32" Alias "GetComputerNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Public Function sGetComputerName() As String
  Dim sBuffer As String
  Dim lBufSize As Long
  Dim lStatus As Long
 
  lBufSize = 255
  sBuffer = String$(lBufSize, " ")
  lStatus = GetComputerName(sBuffer, lBufSize)
  sGetComputerName = ""
  If lStatus <> 0 Then
     sGetComputerName = Left(sBuffer, lBufSize)
  End If
  Form1.Label2 = sGetComputerName
 
End Function

'=====================================
'
'put this in the click event (load...whatever)
'
'
      Dim x
      x = sGetComputerName
0
 
LVL 1

Expert Comment

by:vbWayne
ID: 2636360
dumb refresh
0
 
LVL 1

Expert Comment

by:vbWayne
ID: 2636390
'you can change them using this code.

'Add the following code to a module.
'remember, you are playing with the registry...
'what happens happens at your own risk
'
'
Option Explicit

Public Const HKEY_LOCAL_MACHINE = &H80000002
'
Declare Function RegCreateKey Lib _
"advapi32.dll" Alias "RegCreateKeyA" _
(ByVal Hkey As Long, ByVal lpSubKey As _
String, phkResult As Long) As Long

Declare Function RegCloseKey Lib _
"advapi32.dll" (ByVal Hkey As Long) As Long

Declare Function RegSetValueEx Lib _
"advapi32.dll" Alias "RegSetValueExA" _
(ByVal Hkey As Long, ByVal _
lpValueName As String, ByVal _
Reserved As Long, ByVal dwType _
As Long, lpData As Any, ByVal _
cbData As Long) As Long

Public Const REG_SZ = 1
Public Const REG_DWORD = 4

Public Sub savestring(Hkey As Long, strPath As String, _
strValue As String, strdata As String)
      Dim keyhand As Long
      Dim r As Long
      r = RegCreateKey(Hkey, strPath, keyhand)
      r = RegSetValueEx(keyhand, strValue, 0, _
      REG_SZ, ByVal strdata, Len(strdata))
      r = RegCloseKey(keyhand)
End Sub

'<<<<<<  Code for event command button  >>>>>>

Private Sub Command1_Click()

'Prompts for the new name of the Registered Organization
      strOrganization$ = InputBox("Organisation:")
      
      If strOrganization$ = "" Then
            MsgBox "Empty String", vbCritical, "Error"
            Exit Sub
      End If

'Saves string (Organization) to the registry
      Call savestring(HKEY_LOCAL_MACHINE, _
      "Software\Microsoft\Windows\CurrentVersion", _
      "RegisteredOrganization", strOrganization$)

'Prompts for the new name of the Registered Owner
      strOwner$ = InputBox("Owner:")
      If strOwner$ = "" Then
                MsgBox "Empty String", vbCritical, "Error"
                Exit Sub
      End If

'Saves string (Owner) to the registry
      Call savestring(HKEY_LOCAL_MACHINE, _
      "Software\Microsoft\Windows\CurrentVersion", _
      "RegisteredOwner", strOwner$)

End Sub


'To see the result go to Control Panel / System / General.

0
 

Author Comment

by:Sylvania
ID: 2636443
Ok, I thank you for your help.
But how can I get the Company Name? (Registered Organization)
You show me how to:
- Get the Owner
- Get the Computer name twice ;) (dumb refresh!)
- Change Owner
- Change Organization

I'd think getting Organization is similar to getting the Owner... but which API should I use?

Thanks!
0
 
LVL 1

Expert Comment

by:vbWayne
ID: 2636461
owner is company
0
 

Author Comment

by:Sylvania
ID: 2636510
Let say My name is John Smith, my company is Microsoft and my computer name is BotKicker
Installing Windows, I would enter:
Owner: John Smith
Organization: Microsoft

At some other place in the system, I can determine the name of my computer, which is, in this example, BotKicker.

Now, using the first API you gave me, I am able to retrieve "John Smith".
Using the second API, I am able to retrieve "BotKicker".
Now, I'd want to retrieve "Microsoft" (or whatever my ORGANIZATION would be)
Can you help me?
Or did you and that's I who is unable to locate the proper code (sorry).
0
 
LVL 1

Expert Comment

by:vbWayne
ID: 2636602
I have no idea..I though there were only 2 boxes to fill in and what you got from the api's I sent you would retrieve what you entered into those two boxes....
If you look in Control Panel/System..it only list the 2

The registery will have it but I am do not know how to retrieve it.

Sorry,
Wayne


0
 

Author Comment

by:Sylvania
ID: 2636658
Yes, you're right: There are only 2 boxes to fill in.  But what you sent me, if I'm not mistaken, does not retrieve these 2 fields: The first retrive the Owner, the second (which you seem to think would retrieve the Organization) retrieve the COMPUTER NAME, which is given to your computer when you have a Network access.

Maybe you just copied/pasted the wrong code?
0
 
LVL 1

Expert Comment

by:vbWayne
ID: 2636905
You're right..I don't have the code for the organization.
0
 

Author Comment

by:Sylvania
ID: 2637106
Ok, thanks for your help!
I'll wait for someone to give me the code to retrieve the Organization, then I'll split the points.
0
 
LVL 1

Accepted Solution

by:
pclement earned 180 total points
ID: 2637228
Rather than post all the Registry code, I use a function from the the following source code example:

http://www.thescarms.com/vbasic/Registry.htm

Private Sub Command1_Click()

Dim sCompanyName As String
Dim sUserName As String

sCompanyName = GetUserInfo("RegisteredOrganization")
sUserName = GetUserInfo("RegisteredOwner")

End Sub

Function GetUserInfo(sKey As String) As String

Dim lVal As Long
Dim sSetting As String
Dim sShellKey As String

sSubKey = "Software\Microsoft\Windows\CurrentVersion"

lVal = fReadValue("HKLM", sSubKey, sKey, "S", "", sSetting)

GetUserInfo = sSetting

End Function

Keep in mind that the sub key path (sSubKey above) will be different for NT.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

885 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