Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

User Name and Company

When we first install Windows, we're asked to enter our name and company.  How is it possible to fetch them with VB?
0
Sylvania
Asked:
Sylvania
1 Solution
 
gcs001Commented:
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
 
SylvaniaAuthor Commented:
I would be happy to learn the easiest way to obtain these data via code.

I thank you.
0
 
SylvaniaAuthor Commented:
I have Windows 95 and the key in registry is exactly the same... :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vbWayneCommented:
'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
 
vbWayneCommented:
'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
 
vbWayneCommented:
'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
 
vbWayneCommented:
dumb refresh
0
 
vbWayneCommented:
'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
 
SylvaniaAuthor Commented:
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
 
vbWayneCommented:
owner is company
0
 
SylvaniaAuthor Commented:
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
 
vbWayneCommented:
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
 
SylvaniaAuthor Commented:
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
 
vbWayneCommented:
You're right..I don't have the code for the organization.
0
 
SylvaniaAuthor Commented:
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
 
pclementCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now