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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i get the computer name of a user?

Status
Not open for further replies.

tristero

Programmer
Jan 24, 2003
95
US
i want to be able to log users of my app? i know there is a function which gets the users comp name... right?

i can't remember it though...
 


?SYS(0)

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
I use getenv("COMPUTERNAME") instead of sys(0) because it seems to me according to the VFP help that sys(0) may not return the computer name if no network but I have not confirmed this. I actually have a post to list many different ways to get computer name. I will find the post and rely in here again.
 
The thread is thread184-719449. I don't know how I can link the thread directly.
 
You may want to try this, when all you need is the user's name. SYS(0) will return the host name and the user name.
Code:
*!* username.prg
LOCAL lcUserName, lnLen, llRetVal
DECLARE GetUserName IN WIN32API String@, Integer@
lcUserName = SPACE(20)
lnLen = 20
llRetVal = GetUserName(@lcUserName, @lnLen)
RETURN IIF(llRetVAl, LEFT(ALLTRIM(lcUserName),LEN(ALLTRIM(lcUserName))-1), NULL)
 


Hete is an even move complete versions (it may even all the information about all the users if run a server...I cannot test that right now)
Code:
On Error *
lcComputer = "."
loWMIService = GetObject("winmgmts:\\" + lcComputer + "\root\cimv2")
colItems = loWMIService.ExecQuery("Select * from Win32_NetworkLoginProfile")
For Each loItem in colItems
dtmWMIDate = loItem.AccountExpires
lcReturn = WMIDateStringToDate(dtmWMIDate)
?"Account Expires: " + lcReturn
?"Authorization Flags: " + loItem.AuthorizationFlags
?"Bad Password Count: " + loItem.BadPasswordCount
?"Caption: " + loItem.Caption
?"CodePage: " + loItem.CodePage
?"Comment: " + loItem.Comment
?"Country Code: " + loItem.CountryCode
?"Description: " + loItem.Description
?"Flags: " + loItem.Flags
?"Full Name: " + loItem.FullName
?"Home Directory: " + loItem.HomeDirectory
?"Home Directory Drive: " + loItem.HomeDirectoryDrive
dtmWMIDate = loItem.LastLogoff
lcReturn = WMIDateStringToDate(dtmWMIDate)
?"Last Logoff: " + lcReturn
dtmWMIDate = loItem.LastLogon
lcReturn = WMIDateStringToDate(dtmWMIDate)
?"Last Logon: " + lcReturn
?"Logon Hours: " + loItem.LogonHours
?"Logon Server: " + loItem.LogonServer
?"Maximum Storage: " + loItem.MaximumStorage
?"Name: " + loItem.Name
?"Number Of Logons: " + loItem.NumberOfLogons
?"Password Age: " + loItem.PasswordAge
dtmWMIDate = loItem.PasswordExpires
lcReturn = WMIDateStringToDate(dtmWMIDate)
?"Password Expires: " + lcReturn
?"Primary Group ID: " + loItem.PrimaryGroupId
?"Privileges: " + loItem.Privileges
?"Profile: " + loItem.Profile
?"Script Path: " + loItem.ScriptPath
?"Setting ID: " + loItem.SettingID
?"Units Per Week: " + loItem.UnitsPerWeek
?"User Comment: " + loItem.UserComment
?"User Id: " + loItem.UserId
?"User Type: " + loItem.UserType
?"Workstations: " + loItem.Workstations
?
Next

Function WMIDateStringToDate(dtmWMIDate)
If !IsNull(dtmWMIDate)
WMIDateStringToDate = CTOT(Substr(dtmWMIDate, 5, 2) + "/" + ;
Substr(dtmWMIDate, 7, 2) + "/" + Left(dtmWMIDate, 4) ;
+ " " + Mid (dtmWMIDate, 9, 2) + ":" + ;
Substr(dtmWMIDate, 11, 2) + ":" + Substr(dtmWMIDate, ;
13, 2))
ENDIF
ENDFUNC

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
This works too:
?getenv("USERNAME")

Brian
 
Mike,

You're kind of cross-breeding data type there. The code needs to be changed to either:
?"Account Expires: "
??lcReturn
?"Authorization Flags: "
??loItem.AuthorizationFlags
?"Bad Password Count: "
??loItem.BadPasswordCount
?"Caption: "
??loItem.Caption
.
.
.
-or-

?"Account Expires: " + TRANSFORM(lcReturn)
?"Authorization Flags: " + TRANSFORM(loItem.AuthorizationFlags)
?"Bad Password Count: " + TRANSFORM(loItem.BadPasswordCount)
?"Caption: " + loItem.Caption
.
.
.



-Dave S.-
[cheers]
Even more Fox stuff at:
 
DSummZZZ

Really? I don't see a problem on my end, I do see a lot of nulls on one of the accounts on my computer, but on the admin one all the values are there untransformed wether numeric or character.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Well that's interesting. Then again, I got rid of the "ON ERROR * " statement to see all of the values.

Anyway, for example, these return numeric:
?"Bad Password Count: "
?"CodePage: "
?"Country Code: "
?"Flags: "

(Win2k SP4, VFP 7 SP1)


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top