🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Hook Windows logon / logoff?

#8680
From: Remy Lebeau (TeamB)
Date: Fri, 24 Sep 2004 17:03
39 lines
2868 bytes
"TerryC" <terryc.DONTSPAMME@sweetwaterhsa.DONTSPAMME.com> wrote in message
news:41543ff8$1@newsgroups.borland.com...

> I am creating a WIN32 service program that needs to know
> which user logs on and off and when those occur.  How
> would I go about finding these events?

First off, are you using Borland's TService framework?  If so, then it does
not natively support what you are asking for.

Under Windows NT and 2K, there are no notifications for user logins/logouts
(that I know of).  The only way I know of to detect the logins/logouts is to
determine them manually by regularly polling to see who the current logged
in user is.  Getting the current username from within a service is not a
simple matter, though, since you can't use GetUserName().  You would have to
enumerate the running processes on the machine looking for the OS's shell
executable (usually explorer.exe), then retreive its access token via
OpenProcessToken(), then pass the token to GetTokenInformation() to retreive
its SID, and then finally pass the SID to LookupAccountSid() to retreive its
username.

Under Windows XP and 2003 Server, if you are writting the service code
yourself from scratch instead of using TService, then you can use
RegisterServiceCtrlHandlerEx() instead of RegisterServiceCtrlHandler().  The
HandlerEx() callback can then receive SERVICE_CONTROL_SESSIONCHANGE
notifications, which will include user information.  Otherwise, if you are
using TService then you will have to regularly poll the username instead of
using a callback.  However, because of XP's Fast User Switching feature,
there are different functions used.  Call WTSGetActiveConsoleSessionId() to
get the session ID of the active user, and then pass the ID to
WTSQuerySessionInformation() to get the username from it.

If you find a better solution, let me know.  My company also has services
that need to know when users log in and out, and this is what we came up
with so far.


Gambit
Technical Details
Message-ID: <4154536a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41543ff8$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,de743ddd08421201
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <41543ff8$1@newsgroups.borland.com>
Subject: Re: Hook Windows logon / logoff?
Date: Fri, 24 Sep 2004 10:03:53 -0700
Lines: 41
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <4154536a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1096045418 68.69.17.41 (24 Sep 2004 10:03:38 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail