🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #12964

Re: Win32 Service and login notification

#12964
From: Remy Lebeau (TeamB)
Date: Tue, 11 Nov 2003 20:51
46 lines
3283 bytes
"Sergey Muschin" <SMuschin@tonservices.com> wrote in message
news:3fb1350f$1@newsgroups.borland.com...

> 1.Is there any way for service to know that user just logged in?

Yes, but not easily.  There are no notifications for services to determine
that, so short of simply writing a loop that continuously checks the current
username (which may not work with XP Fast User Switching), you would
probably have to write a replacement GINA that intercepts the login attempt
from the WinLogon screen itself.  MSDN has all sorts of information about
writing GINA DLLs.

Actually, under XP only (not NT or Win2K), if you use
RegisterServiceCtrlHandlerEx() instead of RegisterCtrlHandler() when setting
up your service, then your HandlerEx() callback can detect
SERVICE_CONTROL_SESSIONCHANGE notifications, at least.  The notification
parameters are the same as the WM_WTSSESSION_CHANGE message, which indicates
things like users logging in and out.

> 2. which API call gives the name of the current user?

Typically, that would be GetUserName(), however, it will not work correctly
in a service.  It will get the username that the service itself is logged in
as, which will be different than what the user is logged in as.  To
determine the user's logged in username from inside a service, there is a
whole lot of extra work you have to do, especially if your service is going
to support multiple OSs (NT, Win2K, XP), as they handle things differently
from each other in this regard.

Basically, under NT and Win2K, you have to enumerate the current running
processes until you find explorer.exe (the main GUI shell for the OS, which
is only loaded once the user logs in).  If found, then you can use
OpenProcess(), GetProcessToken(), GetTokenInformation(), and
LookupAccountSid() to extract the associated username from the process
token.

Under XP, on the other hand, you can instead use
WTSGetActiveConsoleSessionId() and WTSQuerySessionInfo(), which also helps
to support the Fast User Switching feature (since multiple users can be
logged in at the same time).  If you want to get all of the available
usernames, not just the active one, then use WTSEnumerateSessions() instead
of WTSGetActiveConsoleSessionId().


Gambit
Technical Details
Message-ID: <3fb14b12$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!newsfeed.icl.net!newsfeed.fjserv.net!skynet.be!skynet.be!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3fb1350f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,95a854cf233ca6b
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-11-11 13:09:50 PST
From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3fb1350f$1@newsgroups.borland.com>
Subject: Re: Win32 Service and login notification
Date: Tue, 11 Nov 2003 12:51:41 -0800
Lines: 48
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 67.22.137.149
Message-ID: <3fb14b12$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1068583698 67.22.137.149 (11 Nov 2003 12:48:18 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!newsfeed.icl.net!newsfeed.fjserv.net!skynet.be!skynet.be!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail