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

Current Logged User In C/C++

2,753 views
Skip to first unread message

ppx

unread,
May 20, 1998, 3:00:00 AM5/20/98
to

Hello,

I wrote a C++ program as a service on NT4 WS.
I want to know in this program how to check if someone has opened a session
and is working.
How can I retrieve the name account of this user.


Best regards
--
Pascal Prioux
Chef de projet informatique
Laboratoire informatique
Enita de Bordeaux
1,cours du général de gaulle
33170 GRADIGNAN
Tel:05 57 35 07 94
Email : p...@enitab.fr

Denis Makarenko

unread,
May 20, 1998, 3:00:00 AM5/20/98
to

BOOL GetUserName(
LPTSTR lpBuffer, // address of name buffer
LPDWORD nSize // address of size of name buffer
);

Regards,
Denis Makarenko

ppx <p...@enitab.fr> wrote in article <6ju2lm$q...@news.u-bordeaux.fr>...

Chris Marriott

unread,
May 20, 1998, 3:00:00 AM5/20/98
to

Denis Makarenko wrote in message <01bd83d5$55cd42f0$41ba08c2@den>...


>BOOL GetUserName(
> LPTSTR lpBuffer, // address of name buffer
> LPDWORD nSize // address of size of name buffer
> );
>
>Regards,
>Denis Makarenko


How does that help, Denis? Calling "GetUserName" from a service will return
the name of the account under which the service is running (normally
"SYSTEM"); the original questioner wants to get the name of the
"interactive" user. How do you do that?

Chris
----------------------------------------------------------------------------
-----
Chris Marriott, SkyMap Software, UK (ch...@skymap.com)
Visit our web site at http://www.skymap.com
Astronomy sofftware written by astronomers, for astronomers

dave porter

unread,
May 20, 1998, 3:00:00 AM5/20/98
to

ppx <p...@enitab.fr> wrote in article <6ju2lm$q...@news.u-bordeaux.fr>...
> Hello,
>
> I wrote a C++ program as a service on NT4 WS.
> I want to know in this program how to check if someone has opened a
session
> and is working.
> How can I retrieve the name account of this user.
>

Here's a start - this gets an access token, not the user
name, but you should be able to get from there to a user name.

If all you want to know is whether there IS a user (your
question is not clear) then the answer can probably be
deduced from whether this function returns null.

No warranty expressed or implied!


// This function is used on Windows NT to get a token for the
// currently logged-in user, if indeed there is such a person.
// This is not as easy as you might think it ought to be. We
// resort to looking for a window which we 'know' belongs to
// the shell, and from there we follow a route which leads to
// getting a handle on an access token owned by the shell.
//
// The return value is either a handle to a suitable token,
// or else null.
//
// One of the times that this function might return null
// is when there is no logged-in user. Other cases include
// insufficient access to the desktop, etc.
//
HANDLE GetUserToken(DWORD access)
{
HANDLE hTok = NULL;
DWORD pid = 0, tid = 0;

// Try it the easy way first - look for a window owned by the shell on
// our current desktop. If we find one, use that to get the process
id.
HWND shell = FindWindowEx(NULL, NULL, __T("Progman"), NULL);
if (shell != NULL)
tid = GetWindowThreadProcessId(shell, &pid);

// We are possibly running on a private window station and desktop: we
must
// switch to the default (which we suppose is where we will find the
// running shell).
else {
HWINSTA saveWinSta = GetProcessWindowStation();
HDESK saveDesk = GetThreadDesktop(GetCurrentThreadId());
HWINSTA winSta = NULL;
HDESK desk = NULL;
bool changeFlag = false;
bool dummy = saveWinSta != NULL &&
saveDesk != NULL &&
(winSta = OpenWindowStation(__T("WinSta0"), false,
MAXIMUM_ALLOWED)) != NULL &&
(changeFlag = SetProcessWindowStation(winSta)) != 0 &&
(desk = OpenDesktop(__T("Default"), 0, false,
MAXIMUM_ALLOWED)) != NULL &&
SetThreadDesktop(desk) != 0;

// Now find the window and process on this desktop
shell = FindWindowEx(NULL, NULL, __T("Progman"), NULL);
if (shell != NULL)
tid = GetWindowThreadProcessId(shell, &pid);

// Restore our own window station and desktop
if (changeFlag) {
if (!SetProcessWindowStation(saveWinSta))
g_eventLog.LogError(CMEV_UNXERR, GetLastError());
if (!SetThreadDesktop(saveDesk))
g_eventLog.LogError(CMEV_UNXERR, GetLastError());
}

// Close temporary objects
if (winSta != NULL)
CloseWindowStation(winSta);
if (desk != NULL)
CloseDesktop(desk);
}

// If we have a process id, use that to get the process handle and
// from there the process' access token.
if (pid != 0) {
HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid);
if (hProc != NULL) {
OpenProcessToken(hProc, access, &hTok) || (hTok = NULL);
CloseHandle(hProc);
}
}

// Return token if we got one
return hTok;
}

--
For email, please remove the 'w' from my address. Sorry.


Pete Delgado

unread,
May 20, 1998, 3:00:00 AM5/20/98
to

Let me preface this by saying that I haven't done this myself...

Couldn't you use the EnumWindowStations () function to
get a handle to the current window station and then use
OpenInputDesktop() to get the current user desktop,
finally call GetObjectSecurity() to get the SID for the user?

I'm sure it's not as easy as it sounds since the devil is always
in the details.... I would imagine that your service would have to be
configured with the "interact with desktop" setting enabled in order to
do some specific things.

Check out the SDK "Window Stations and Desktops", specifically
the section entitled "Interacting with the User by a win32 Service".

Sounds like an interesting problem, let me know how it turns out!

-- Pete
**************************************************
Pete Delgado
Software Development and Support Engineer
Structural Dynamics Research Corporation
2000 Eastman Drive, Milford, OH
**************************************************

Andy Lutomirski

unread,
May 20, 1998, 3:00:00 AM5/20/98
to

I sent this idea to someone else recently, and he said it worked.

Here's a somewhat easier way:

EnumProcesses to get all processes.

GetModuleBaseName(hProc,
0, // For the actual program
...);

If it's explorer.exe, get a token. Then, get an accout name.
An easy way would be to impersonate the token and call GetUserName.


ppx wrote in message <6ju2lm$q...@news.u-bordeaux.fr>...


>Hello,
>
>I wrote a C++ program as a service on NT4 WS.
>I want to know in this program how to check if someone has opened a
session
>and is working.
>How can I retrieve the name account of this user.
>
>

James "Wez" Weatherall

unread,
May 21, 1998, 3:00:00 AM5/21/98
to

Pete Delgado > < wrote in message <01bd8417$9328f0d0$6b317a92@pcw66>...


>Let me preface this by saying that I haven't done this myself...
>
>Couldn't you use the EnumWindowStations () function to
>get a handle to the current window station and then use
>OpenInputDesktop() to get the current user desktop,
>finally call GetObjectSecurity() to get the SID for the user?
>
>I'm sure it's not as easy as it sounds since the devil is always
>in the details.... I would imagine that your service would have to be
>configured with the "interact with desktop" setting enabled in order to
>do some specific things.
>
>Check out the SDK "Window Stations and Desktops", specifically
>the section entitled "Interacting with the User by a win32 Service".
>
>Sounds like an interesting problem, let me know how it turns out!


I've been fiddling with this for a while now. I'm at the stage where I have
an SID for the current user but I can't find out who they are because
performing a LookupAccountSid fails, telling me that the SID I've specified
doesn't map to an account in the current domain. I got the SID by examining
the Winstation that my service is running in, so it should at least return
the unhelpful value of BUILTIN\SYSTEM, if not my user name...

Does anyone have any other ideas? (Please reply to me directly if you
do...)

Cheers,

Wez @ ORL

0 new messages