I was kind of blank when I started off with the link you provided but with
some searching i got into a post which got Queries and i used it this way :

objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
strComputer = "."
objSWbemServices = objWMIService.ConnectServer(strComputer, "root\cimv2")
oos = objSWbemServices.ExecQuery("SELECT UserName FROM Win32_ComputerSystem")
oo = oos[0]

# list all object properties
for op in oo.Properties_:
    username = op.Value

while debugging it, i got <COMPUTER_NAME>\<USERNAME> but when i
installed the service and started it it kept crashing and the reason
behind it was :

(-2147221008, 'CoInitialize has not been called.', None, None)

i have no idea whether i did it correctly, please guide me if it's wrong !

Thank you a lot !


On Sat, Apr 11, 2020 at 11:49 PM Tim Roberts <t...@probo.com> wrote:

> On Apr 11, 2020, at 6:32 AM, Waseem Afser <waseem.af...@gmail.com> wrote:
>
>
> Is there any possible method to get the logged in user's username from a
> windows service ?
>
>
> Your assumption that there is only one "logged in user” is false, which is
> why the information you seek is not readily available.
>
> There is a WMI query that can return the name of all logged in users:
>
>
> https://stackoverflow.com/questions/5218778/how-do-i-get-the-currently-logged-username-from-a-windows-service-in-net
> —
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.
>
> _______________________________________________
> python-win32 mailing list
> python-win32@python.org
> https://mail.python.org/mailman/listinfo/python-win32
>
_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to