Page 1 of 1

Getting a Windows username

Posted: Wed Jul 03, 2019 7:50 am
by Echo
Hi, I'm trying to find a way to retrieve the username or home directory of the current user logged in to Windows. For those familiar with Python or it would produce a similar result to the below code sample:

Code: Select all

#To get a username
>>> import getpass
>>> username = getpass.getuser()
>>> print(username)
someUser

#To get a user's home directory path
>>> homedir = os.path.expanduser("~")
>>> print(homedir)
C:\Users\someUser
I've searched the Lansa documentation, but haven't found a way to achieve something similar in Lansa. If someone could point me to the documentation for a similar function(one or the other) in Lansa, it would be greatly appreciated.

Re: Getting a Windows username

Posted: Wed Jul 03, 2019 11:42 am
by tsupartono
Not sure it there are other ways, but an easy way would be to read from environment variables.

Below code will get the username, domain, and home directory of the currently logged-in user.

Code: Select all

Define_Com Class(#XPRIM_OSUtil) Name(#OSUtil)
Define_Com Class(#PRIM_BOLN) Name(#Found)

#USERNAME := #OSUtil.GetEnvironmentVariable( 'USERNAME' #Found )
#USERDOMAIN := #OSUtil.GetEnvironmentVariable( 'USERDOMAIN' #Found )
#HOMEDIR := #OSUtil.GetEnvironmentVariable( 'USERPROFILE' #Found )
Note: XPRIM_OSUtil requires at least 14 SP1 with the latest EPC.

Re: Getting a Windows username

Posted: Thu Jul 04, 2019 1:54 am
by Echo
Unfortunately we are running V13.2 in this environment. This is a great solution if we run into it in our V14SP2 environment, so thank you.

Re: Getting a Windows username

Posted: Sat Jul 06, 2019 12:27 am
by atostaine
Is there any documentation on #XPRIM_OSUtil?

Re: Getting a Windows username

Posted: Mon Jul 08, 2019 2:30 pm
by tsupartono

Re: Getting a Windows username

Posted: Thu Jul 11, 2019 3:54 am
by Echo
So, I found a way to do this in V13. While there is no way to grab it directly as with #XPRIM_OSUTIL in V14, what you can do is get it using the following code in V13:

Code: Select all

Use Builtin(GET_REGISTRY_VALUE) With_Args(HKEY_CURRENT_USER 'Volatile Environment\' 'LOCALAPPDATA') To_Get(#UserPath #RegErr)
Use Builtin(GET_REGISTRY_VALUE) With_Args(HKEY_CURRENT_USER 'Volatile Environment\' 'USERNAME') To_Get(#WinUser #RegErr)
Information on the GET_REGISTRY_VALUE function:
https://docs.lansa.com/14/en/lansa015/c ... _value.htm
Information on retrieving registry values for the current user:
https://www.lifewire.com/hkey-current-user-2625901

But still a big thank you to tsupartono for providing exactly what I asked for originally. I look forward to using XPRIM_OSUTIL in our V14 environment.