Solved

Get the the current user name under NT

Posted on 2001-06-22
19
632 Views
Last Modified: 2012-08-13
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
Comment
Question by:jltari
  • 4
  • 4
  • 4
  • +4
19 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6218352
If there is a user logged on at all....
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6218377
Please disregard that, some mishap with the refresh button.

I suppose you mean something like 'finger' on unix?
0
 
LVL 3

Author Comment

by:jltari
ID: 6218463
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
 
LVL 30

Expert Comment

by:Axter
ID: 6218469
What method did you use to getthe user name?

Did you use the GetUserName() API?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6218477
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 6218481
Are you saying you want to know when the user logs on so you then can get the userID?
0
 
LVL 32

Accepted Solution

by:
jhance earned 200 total points
ID: 6218494
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
 
LVL 3

Author Comment

by:jltari
ID: 6218599
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
 
LVL 30

Expert Comment

by:Axter
ID: 6218612
Did you use the GetUserName() API???
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:jhance
ID: 6218621
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
 
LVL 32

Expert Comment

by:jhance
ID: 6218626
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
 
LVL 3

Author Comment

by:jltari
ID: 6218849
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
 
LVL 86

Expert Comment

by:jkr
ID: 6218896
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
 
LVL 86

Expert Comment

by:jkr
ID: 6218902
Ooops, 'LookupAccountSid()' is actually enough, as you alread have a PSID...
0
 
LVL 32

Expert Comment

by:jhance
ID: 6219934
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
 
LVL 86

Expert Comment

by:jkr
ID: 6219959
>>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
 
LVL 3

Author Comment

by:jltari
ID: 6223808
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
 
LVL 11

Expert Comment

by:griessh
ID: 6820036
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
 

Expert Comment

by:ComTech
ID: 6837690
Dormant question accepted the recommened answer.

ComTech
CS Admin @ EE
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now