The Artima Developer Community
Sponsored Link

Python Answers Forum
windows service - get current logged on user

3 replies on 1 page. Most recent reply: Nov 7, 2007 3:56 AM by pete siedle

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
pete siedle

Posts: 3
Nickname: siggy
Registered: Nov, 2007

windows service - get current logged on user Posted: Nov 2, 2007 10:02 AM
Reply to this message Reply
Advertisement
Hi All

I'm having a little trouble getting the current logged on user from a pyhton windows service.

I've tried using os.getEnv("username") - same as os.environs["username"] -
which failed - they return None.

So I tried using win32api:

userName = win32api.GetUserName()

which returned SYSTEM

Which I assume is returning the 'user' who's running the service.

So I tried dipping into the registry using:

# hkey = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer')
# userName, type = win32api.RegQueryValueEx(hkey, 'Logon User Name')


However, this again failed returning None or SYSTEM, I can't remeber which.

So I think what's happening is that the pythonservice.exe is running as a SYSTEM process, so when it does a call to get the current logged on user (using any of the methods I've tried) they all ultimately point back to the HKEY_USERS reg entry for SYSTEM & not the user who's logged onto the PC.

Does anyone have any idea to get the actual logged on user of the PC via a windows service?

I'm at a loss other than using a logon script to create an entry / file with the %USERNAME% stored somewhere!?!?

Cheers

Siggy


justin quick

Posts: 3
Nickname: justquick
Registered: Nov, 2007

Re: windows service - get current logged on user Posted: Nov 5, 2007 10:40 AM
Reply to this message Reply
Try the getpass module, it looks for these keys in the os.environ for a suitable username: ('LOGNAME', 'USER', 'LNAME', 'USERNAME') This should work in windows, cant test it, sorry too busy running linux.

pete siedle

Posts: 3
Nickname: siggy
Registered: Nov, 2007

Re: windows service - get current logged on user Posted: Nov 7, 2007 3:50 AM
Reply to this message Reply
Hi Justin

thanks for the reply.

However, it didn't work.

Once again it worked fine when the app is run by the user.
But when I ran the app as a windows service it didn't return a user name, but raised an exception (as per the documentaion for the getuser() method).

Cheers

Siggy

pete siedle

Posts: 3
Nickname: siggy
Registered: Nov, 2007

Re: windows service - get current logged on user Posted: Nov 7, 2007 3:56 AM
Reply to this message Reply
PS. for those interested.

I have managed to get the functionality I want via the use of logon & logoff scripts. When a user logs on his name is added to a text file, when he logs off his name is removed from the file.

My python windows service app reads this text file to see who's logged on!

Not ideal, but as windows doesn't seem able to provide the info straight out of the box.....

Can't believe windows doesn't have a working equivalent to 'who', 'last', etc as in linux!

Siggy

Flat View: This topic has 3 replies on 1 page
Topic: Verify PKCS7 against CA hierarchy Previous Topic   Next Topic Topic: assign values to members of class objects

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use