Link to home
Start Free TrialLog in
Avatar of jltari
jltari

asked on

Get the the current user name under NT

I run a program with the scheduler (the AT command at the DOS prompt) that needs to know the username of the currently logged on user. Since the scheduler is a service, all I can get as a username is "SYSTEM".
Would anyone know how to get this info?

I did find some information in the registry under HKEY_LOCAL_MACHINE\system\currentcontrolset\control\hivelist but there is probably an easier and more proper way to get it.

Thanks
Avatar of KangaRoo
KangaRoo

If there is a user logged on at all....
Please disregard that, some mishap with the refresh button.

I suppose you mean something like 'finger' on unix?
Avatar of jltari

ASKER

I don't know what finger does in UNIX, sorry.
But I can try to be a bit clearer:
When the computer starts, the scheduler service starts also.
At some point, independently of a user logging in, the service will launch my program.
But since the scheduler is a service, it runs with the user "SYSTEM", and that doesn't suit my needs, for I need to know the user name of the user who's connected at that moment, if any.
Avatar of Axter
What method did you use to getthe user name?

Did you use the GetUserName() API?
>>user name of the user who's connected at that moment, if
>>any.
But if the user is not logged on, you're going to get a "SYSTEM" user ID.

Are you saying you want to know when the user logs on so you then can get the userID?
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jltari

ASKER

OK, here is what I do:
The scheduler is started on 2000 computers and runs once per hour to execute a bach file over the network.
Among other things, that batch file should get the value of %username%. Naturally, if I do exactly that, I will get the user "SYSTEM".
So I wanted to write a little program that the batch file would run and that would print the list of the currently logged in users to the screen.
It so happens that there always is a user (called "WINST") logged in, but I can sort the user names later on.

I am not familiar to those functions "EnumWindowStations" and "EnumDesktops". Would I get the actual user names after that?
Did you use the GetUserName() API???
No, you'll get the SID of the users of the various desktops.  (Of course under most circumstances there will be only one but it's foolish to assume that this will always be the case!!)  With the SID you can find the user name with LookupAccountSid().

These functions are not as difficult as they appear at first glance.  The Enum... functions just enumerate all the possibilities and you just call the Get... or Lookup.. functions on the returned handles until you find what you are looking for.  Do you have MSDN?  It's all documented in there....
Axter,

GetUserName() is not applicable here.  The process that calls GetUserName() is running under the local system account.  It will ALWAYS return SYSTEM (or another account name if the service is running using another account).  So it is of no value here.
Avatar of jltari

ASKER

It looks like it's what I need, but I can't seem to find an example of the use of those functions, neither from the Borland Help, nor the MSDN library. Internet was not of much help either.
Would you happen to have an example somewhere you could show me?

Thanks
It's actually a lot easier:

- Enumerate all running apps, looking for 'explorer.exe'
- Call 'OpenProcessToken( ..., TOKEN_QUERY,...);'
- Use the resulting token in a call to 'QueryTokenInformation(..., TokenOwner, ...)'
- Use the returned SID to retrieve the user name:

                if  (   !GetSecurityDescriptorOwner (   psd,
                                                        &psid,
                                                        &bDefaulted
                                                    )
                    )
                    {
                        dwErr   =   GetLastError    ();

                    }
                                                   
                //  lookup clear text name of the owner
                if  (   !LookupAccountSid   (   NULL,
                                                psid,
                                                acBuffer,
                                                &dwNameSize,
                                                acReferencedDomain,
                                                &dwDomainBufSize,
                                                &eUse
                                            )
                    )
                    {
                        dwErr   =   GetLastError    ();

                    }
Ooops, 'LookupAccountSid()' is actually enough, as you alread have a PSID...
jkr,

As always, there are multiple ways of doing things in Windows but there may be multiple instances of EXPLORER running among different users.  Consider the Windows Terminal Services case as one possibility.
>>Consider the Windows Terminal Services case as one
>>possibility.

Yes, you're correct - but the same problem would arise with desktops IIRC (not too familiar with TS)

Anyway, in both cases there is not a single logged on user...

Another idea that would (should) also work for both cases is enumerating the HKEY_USERS key - the subkeys are the direct textual representations of the logged on users' SIDs.
Avatar of jltari

ASKER

I like the way jhance gets around the problem. But as I said earlier, I'd need an example of the use of each function. I increased the question points to motivate you :)

Thanks
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "jhance" comment(s) as an answer since you seemed to prefer his solution.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner

Dormant question accepted the recommened answer.

ComTech
CS Admin @ EE