Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get registry info like current user and ip address 3

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
0
0
CN
Does anybody know how to get windows registry settings like the current user or the IP addres of the computer running the application?
And do I need to read this info out of the registry or is there some command wich will do it?
 
Put the following 2 declarations in a module:
Public Declare Function GetComputerName Lib "KERNEL32" Alias "GetComputerNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

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


''The Code Below To Call The API
'****************************************************************Call For PC Name
lBuffLen = 128
sBuffer = String$(lBuffLen, vbNullChar)
lRet = GetComputerName(sBuffer, lBuffLen)
If lRet < 0 Then
''Handle error from API here
End If
sName = Left$(sBuffer, lBuffLen)
'*****************************************************************Call For User Name
lBuffLen = 128
sBuffer = String$(lBuffLen, vbNullChar)
lRet = GetUserName(sBuffer, lBuffLen)
If lRet < 0 Then
'Handle error from API here
End If
sName = Left$(sBuffer, lBuffLen - 1)
 
I tried this on a windows ME and sName are 4 strange carakters wich I cannot print (in a debug.print).
 
Try adding this:

Static sName As String

If Len(sName) > 0 Then
GetComputerName = sName
Exit Function
End if
 
No, compile error function call on left hand side of assignment must return variant or object.
 
Sorry,
Make the entire post a function

private Function GetPCName() as String
Static sName As String
dim lBufLen as long
dim sBuffer as string
dim lRet as long
If Len(sName) > 0 Then
GetPCName = sName
Exit Function
else
lBuffLen = 128
sBuffer = String$(lBuffLen, vbNullChar)
lRet = GetComputerName(sBuffer, lBuffLen)
If lRet < 0 Then
''Handle error from API here
End If
sName = Left$(sBuffer, lBuffLen)
GetPCName = sname
end if
 
Works like a charm, sorry for the format date bit.
 
Absolutly no reason to be sorry. I have a nack for sounding like a jerk when I am trying to make a point. My apologies
 
i was able to use this code. Thanks to you guys. But how about getting the Windows Registered Organization?

Godspeed!!!
 
For the username you can use:
MsgBox Environ(&quot;USERNAME&quot;)
MsgBox Environ(&quot;USERDOMAIN&quot;)
There are more contstants to be used with environ but they cannot easily be found (searched the Internet for the last 15 minutes but could not find an overview)

To get regvalues from the current computer (or another computer) you can use this:
save the following file as a frm to get remote reg values:

VERSION 5.00
Begin VB.Form Form1
Caption = &quot;Form1&quot;
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = &quot;Form1&quot;
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command1
Caption = &quot;Command1&quot;
Height = 735
Left = 1200
TabIndex = 0
Top = 720
Width = 615
End
End
Attribute VB_Name = &quot;Form1&quot;
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003

Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_ALL_ACCESS = &H3F

Private Const REG_SZ As Long = 1
Private Const ERROR_SUCCESS = 0&
Private Declare Function RegConnectRegistry Lib &quot;advapi32.dll&quot; _
Alias &quot;RegConnectRegistryA&quot; _
(ByVal lpMachineName As String, _
ByVal hKey As Long, _
phkResult As Long) As Long

Private Declare Function RegCloseKey Lib &quot;advapi32.dll&quot; _
(ByVal hKey As Long) As Long

Private Declare Function RegOpenKeyEx Lib &quot;advapi32.dll&quot; _
Alias &quot;RegOpenKeyExA&quot; _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
phkResult As Long) As Long

Private Declare Function RegQueryValueExString Lib &quot;advapi32.dll&quot; _
Alias &quot;RegQueryValueExA&quot; _
(ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
ByVal lpData As String, _
lpcbData As Long) As Long

Private hRemoteReg As Long





Private Sub openRootKey(strComputerName, Rootkey)
Dim lRet As Long

'Connect to the remote registry
lRet = RegConnectRegistry(strComputerName, _
Rootkey, _
hRemoteReg)

If (lRet = ERROR_SUCCESS) Then

Else
MsgBox &quot;Error:&quot; & Err.LastDllError
Unload Me
Exit Sub
End If
End Sub


Private Sub closeRootKey()
Dim lRet As Long
If hRemoteReg <> 0 Then
lRet = RegCloseKey(hRemoteReg)
End If
End Sub

Public Function getKey(strComputer, strPath, strKeyName) As String
Dim strRootkey As String
Dim Rootkey As Long
Dim strRestPath As String
Dim lRetVal As Long
Dim hKey As Long
Dim sValue As String
sValue = String(256, 0)
Dim lngLength As Long
lngLength = Len(sValue)


strRootkey = Mid(strPath, 1, InStr(1, strPath, &quot;\&quot;, vbTextCompare) - 1)
Select Case strRootkey
Case &quot;HKEY_CLASSES_ROOT&quot;
Rootkey = HKEY_CLASSES_ROOT
Case &quot;HKEY_CURRENT_USER&quot;
Rootkey = HKEY_CURRENT_USER
Case &quot;HKEY_LOCAL_MACHINE&quot;
Rootkey = HKEY_LOCAL_MACHINE
Case &quot;HKEY_USERS&quot;
Rootkey = HKEY_USERS
End Select


strPath = Mid(strPath, InStr(1, strPath, &quot;\&quot;, vbTextCompare) + 1, Len(strPath) - InStr(1, strPath, &quot;\&quot;, vbTextCompare))
openRootKey strComputer, Rootkey
lRetVal = RegOpenKeyEx(hRemoteReg, _
strPath, 0, KEY_QUERY_VALUE, hKey)
If lRetVal <> ERROR_SUCCESS Then
MsgBox &quot;Cannot open key&quot;
Else
sValue = String(255, &quot; &quot;)
lRetVal = RegQueryValueExString(hKey, _
strKeyName, 0&, REG_SZ, sValue, lngLength)
If lRetVal <> ERROR_SUCCESS Then
MsgBox &quot;Cannot query value&quot;
Else
getKey = Left$(sValue, lngLength - 1)
End If
lRetVal = RegCloseKey(hKey)
If lRetVal <> ERROR_SUCCESS Then
MsgBox &quot;Cannot close key&quot;
End If
End If
closeRootKey
End Function

Private Sub Command1_Click()
Dim strDomain As String
Dim strUser As String
strDomain = getKey(&quot;computername(without\\)&quot;, &quot;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon&quot;, &quot;DefaultDomainName&quot;)
strUser = getKey(&quot;computername(without\\)&quot;, &quot;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon&quot;, &quot;DefaultUserName&quot;)
MsgBox strDomain & &quot;\&quot; & strUser
End Sub
 
By the way, I am using win2k and my org is in the regestry under:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization
 
Some databases for example MSSQL Server, can give that information to you.

Thus, if you are using a database, try to see if the database cannot give that information to you in a much easier way.
 
Just some FYI for those who might want it.

Here is a brief list of Environment variables for NT, these are also stored in the registry:

COMPUTERNAME - Network name of computer
COMSPEC - Path to NT command Shell executable
HOMEDRIVE - Drive of home directory
HOMEPATH - Path to home directory
HOMESHARE - UNC name of share if home directory is on network
LOGONSERVER - Name of server that performed logon
NUMBER_OF_PROCESSORS - Number of CPUs
OS - name of the running OS
PATH - a semi-colon seperated list of search directories
PATHEXT - a semi-colon list of file extensions to look for when searching for executables
PROCESSOR_ARCHITECTURE - name of CPU architecture
SYSTEMDRIVE - Drive letter of NT installation
SYSTEMTROOT - Path of NT insdtallation
USERDOMAIN - Name of domain or local machine logged into
USERNAME - name of logged on user
USERPROFILE - Path to user profile of current user

System environment variables are stored in HKEY_LOCAL_MACHINE

User environment variables are stored in HKEY_CURRENT_USER

You can also access these variables in the &quot;System&quot; icon of control panel on the &quot;Environment&quot; Tab.
Persistence....Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination alone are omnipotent. -Calvin Coolidge
 
And ANY of the environment variables can be changed or deleted very easily. So they are not reliable.
 
Add a reference to the Windows Script Host Object Model. Then:

[tt]
Private Sub Command1_Click()
Dim WSHNet As WshNetwork
Set WSHNet = New WshNetwork

Debug.Print WSHNet.UserName
Debug.Print WSHNet.UserDomain
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top