Solved

User Name and Company

Posted on 2000-03-20
16
209 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
 
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
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

 

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 60 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

706 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

17 Experts available now in Live!

Get 1:1 Help Now