[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 673
  • Last Modified:

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
0
jltari
Asked:
jltari
  • 4
  • 4
  • 4
  • +4
1 Solution
 
KangaRooCommented:
If there is a user logged on at all....
0
 
KangaRooCommented:
Please disregard that, some mishap with the refresh button.

I suppose you mean something like 'finger' on unix?
0
 
jltariAuthor Commented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AxterCommented:
What method did you use to getthe user name?

Did you use the GetUserName() API?
0
 
AxterCommented:
>>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.

0
 
AxterCommented:
Are you saying you want to know when the user logs on so you then can get the userID?
0
 
jhanceCommented:
Well, it's not a simple as you might expect.  The problem is that there can be any number of logged on users, from 0 - N.  Which one do you want?  Each user has his own desktop.  

To determine a logged on user, first figure out which desktop you want to find the current user for.  Use EnumWindowStations to find all the window stations.  Usually there is only one of interest but there is no guarantee of this.  

Then, for each window station call EnumDesktops to find all the desktops in the window station.  

Then for the desktop you are interested in finding the user for, use GetUserObjectInformation to get the SID for the user of that desktop.
0
 
jltariAuthor Commented:
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?
0
 
AxterCommented:
Did you use the GetUserName() API???
0
 
jhanceCommented:
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....
0
 
jhanceCommented:
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.
0
 
jltariAuthor Commented:
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
0
 
jkrCommented:
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    ();

                    }
0
 
jkrCommented:
Ooops, 'LookupAccountSid()' is actually enough, as you alread have a PSID...
0
 
jhanceCommented:
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.
0
 
jkrCommented:
>>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.
0
 
jltariAuthor Commented:
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
0
 
griesshCommented:
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: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

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

0
 
ComTechCommented:
Dormant question accepted the recommened answer.

ComTech
CS Admin @ EE
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now