Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting currently logged in user name from service

121 views
Skip to first unread message

kunal s patel

unread,
Mar 14, 2007, 3:25:00 AM3/14/07
to
Hi all,

I have a question regarding retrieving currently logged on user.....now i
know, windows have one api called GetUserName(). Now if i use this api from
my service, i get SYSTEM as the user name.....but i want to retrieve the user
name of the person who is currently logged in the system....is there some
other apis for that.....

I searched through codeproject....n someone suggested was to retrieve the
username of the explorer process since it runs under the credentials of
currently logged on user.......but how do i go about it??.....some sample
code or link would be really helpful....Also is there some other way of
retrieving it???....

kunal

Don Burn

unread,
Mar 14, 2007, 8:17:11 AM3/14/07
to
What will you do if there is more than one user? Terminal services has
been there a long time, and you need to handle 0 to N users logged in.


--
Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr
Remove StopSpam to reply


"kunal s patel" <kunal...@discussions.microsoft.com> wrote in message
news:EB535823-22C4-4F8E...@microsoft.com...

Jeff Henkels

unread,
Mar 14, 2007, 8:21:31 AM3/14/07
to
First of all, there may not be a "currently logged on user" -- e.g. after
the machine has come up but no one's logged on yet. Also, if the machine
runs Terminal Services or Citrix, there could be multiple users logged on at
any given time. There's also the issue of fast user switching, in which
multiple users are logged in but only one has access to the console.

Assuming that you've considered those cases, there are several ways to get
this information:

1. Write a small app that runs as part of user logon (e.g. put it in All
Users startup group or in the
HKLM\Software\Microsoft\Windows\CurrentVersion\Run regkey). This app will
get the user's name and send it to your service via your favorite IPC
mechanism.

2. Use a HandlerEx function in your service (as opposed to the
old-fashioned Handler) and process the SERVICE_CONTROL_SESSIONCHANGE event.
It's been a while since I worked with this, but IIRC in earlier versions of
XP it would sometimes miss events.

3. Write a Winlogon Notification Package -- it will catch logon, logoff,
and other events and can send information to your service.

"kunal s patel" <kunal...@discussions.microsoft.com> wrote in message
news:EB535823-22C4-4F8E...@microsoft.com...

Uwe Sieber

unread,
Mar 14, 2007, 1:34:58 PM3/14/07
to

For the user of the active session:

DWORD dwSessionId = WTSGetActiveConsoleSessionId(); // 0, 1, 2...
if ( dwSessionId == 0xFFFFFFFF ) {
return;
}

HANDLE hToken = NULL;
WTSQueryUserToken(dwSessionId, &hToken);
if ( hToken == NULL ) {
return;
}

HANDLE hDupToken = NULL;
DuplicateToken(hToken, SecurityImpersonation, &hDupToken);
if ( hDuoToken == NULL ) {
CloseHandle(hToken);
return;
}

BOOL bRes = ImpersonateLoggedOnUser(hDupToken);
if ( bRes ) {
// GetUserName here
RevertToSelf();
}

CloseHandle(hDupToken);
CloseHandle(hToken);

Uwe

Skywing [MVP]

unread,
Mar 14, 2007, 2:23:14 PM3/14/07
to
Of course, this will completely break if you're working over RDP/Terminal
Server, or started the program via RunAs, or any other myriad scenarios..

--
Ken Johnson (Skywing)
Windows SDK MVP
http://www.nynaeve.net
"Uwe Sieber" <ma...@uwe-sieber.de> wrote in message
news:55qno7F...@mid.individual.net...

Uwe Sieber

unread,
Mar 15, 2007, 4:05:33 AM3/15/07
to
Skywing [MVP] wrote:
> Of course, this will completely break if you're working over
> RDP/Terminal Server, or started the program via RunAs, or any other
> myriad scenarios..
>

Of course. As requested it works used in a local service.

ritaagarwala

unread,
Nov 7, 2009, 11:19:53 PM11/7/09
to
I have a scenario, where I want to get user name from WIA service.WTSQueryUsertoken is working fine in XP32. As on XP 32 WIA service runs in context of Local System. But the same all is failing for XP 64 with error code ERROR_PRIVILEGE_NOT_HELD. Because On XP 64 WIA service runs in context of Local Service. How to retrieve user name on XP64?

Uwe Sieber wrote:

Re: Getting currently logged in user name from service
15-Mar-07

Skywing [MVP] wrote:

Of course. As requested it works used in a local service.

Previous Posts In This Thread:

On Wednesday, March 14, 2007 2:25 AM
kunalspate wrote:

Getting currently logged in user name from service
Hi all,

I have a question regarding retrieving currently logged on user.....now i
know, windows have one api called GetUserName(). Now if i use this api from
my service, i get SYSTEM as the user name.....but i want to retrieve the user
name of the person who is currently logged in the system....is there some
other apis for that.....

I searched through codeproject....n someone suggested was to retrieve the
username of the explorer process since it runs under the credentials of
currently logged on user.......but how do i go about it??.....some sample
code or link would be really helpful....Also is there some other way of
retrieving it???....

kunal

On Wednesday, March 14, 2007 7:17 AM
Don Burn wrote:

What will you do if there is more than one user?
What will you do if there is more than one user? Terminal services has
been there a long time, and you need to handle 0 to N users logged in.


--
Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr
Remove StopSpam to reply

"kunal s patel" <kunal...@discussions.microsoft.com> wrote in message
news:EB535823-22C4-4F8E...@microsoft.com...

On Wednesday, March 14, 2007 7:21 AM
Jeff Henkels wrote:

First of all, there may not be a "currently logged on user" -- e.g.
First of all, there may not be a "currently logged on user" -- e.g. after
the machine has come up but no one's logged on yet. Also, if the machine
runs Terminal Services or Citrix, there could be multiple users logged on at
any given time. There's also the issue of fast user switching, in which
multiple users are logged in but only one has access to the console.

Assuming that you've considered those cases, there are several ways to get
this information:

1. Write a small app that runs as part of user logon (e.g. put it in All
Users startup group or in the
HKLM\Software\Microsoft\Windows\CurrentVersion\Run regkey). This app will
get the user's name and send it to your service via your favorite IPC
mechanism.

2. Use a HandlerEx function in your service (as opposed to the
old-fashioned Handler) and process the SERVICE_CONTROL_SESSIONCHANGE event.
It's been a while since I worked with this, but IIRC in earlier versions of
XP it would sometimes miss events.

3. Write a Winlogon Notification Package -- it will catch logon, logoff,
and other events and can send information to your service.

"kunal s patel" <kunal...@discussions.microsoft.com> wrote in message
news:EB535823-22C4-4F8E...@microsoft.com...

On Wednesday, March 14, 2007 12:34 PM
Uwe Sieber wrote:

Re: Getting currently logged in user name from service
kunal s patel wrote:

For the user of the active session:

DWORD dwSessionId = WTSGetActiveConsoleSessionId(); // 0, 1, 2...
if ( dwSessionId == 0xFFFFFFFF ) {
return;
}

HANDLE hToken = NULL;
WTSQueryUserToken(dwSessionId, &hToken);
if ( hToken == NULL ) {
return;
}

HANDLE hDupToken = NULL;
DuplicateToken(hToken, SecurityImpersonation, &hDupToken);
if ( hDuoToken == NULL ) {
CloseHandle(hToken);
return;
}

BOOL bRes = ImpersonateLoggedOnUser(hDupToken);
if ( bRes ) {
// GetUserName here
RevertToSelf();
}

CloseHandle(hDupToken);
CloseHandle(hToken);

Uwe

On Wednesday, March 14, 2007 1:23 PM
Skywing [MVP] wrote:

Of course, this will completely break if you're working over RDP/Terminal
Of course, this will completely break if you're working over RDP/Terminal
Server, or started the program via RunAs, or any other myriad scenarios..

--

Ken Johnson (Skywing)
Windows SDK MVP
http://www.nynaeve.net
"Uwe Sieber" <ma...@uwe-sieber.de> wrote in message
news:55qno7F...@mid.individual.net...

On Thursday, March 15, 2007 3:05 AM
Uwe Sieber wrote:

Re: Getting currently logged in user name from service
Skywing [MVP] wrote:

Of course. As requested it works used in a local service.

EggHeadCafe - Software Developer Portal of Choice
ASP.NET - Prevent Multiple Logins Using the Cache
http://www.eggheadcafe.com/tutorials/aspnet/5b7868cf-fc4b-45e5-a75d-1c06d9a53482/aspnet--prevent-multipl.aspx

0 new messages