Getting a Windows username

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
Echo
Posts: 11
Joined: Tue Jun 04, 2019 2:48 am

Getting a Windows username

Post by Echo » Wed Jul 03, 2019 7:50 am

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.

tsupartono

Re: Getting a Windows username

Post by tsupartono » Wed Jul 03, 2019 11:42 am

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.

Echo
Posts: 11
Joined: Tue Jun 04, 2019 2:48 am

Re: Getting a Windows username

Post by Echo » Thu Jul 04, 2019 1:54 am

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.

atostaine
Posts: 442
Joined: Wed Jan 20, 2016 7:38 am

Re: Getting a Windows username

Post by atostaine » Sat Jul 06, 2019 12:27 am

Is there any documentation on #XPRIM_OSUtil?
Art Tostaine


Echo
Posts: 11
Joined: Tue Jun 04, 2019 2:48 am

Re: Getting a Windows username

Post by Echo » Thu Jul 11, 2019 3:54 am

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.

Post Reply