Solved

how to log off the interactive user from a service

Posted on 2001-07-02
3
202 Views
Last Modified: 2010-04-06
Hi there,

I have written a Windows NT service in delphi, which runs as the local SYSTEM account.

This service though, needs to know who the current logged on user is, and under certain circimstances, log off that user.

If I ask for the "CurrentUser" the answer I get is SYSTEM. I need to know who is loggued on as Interactive user (on the console) and again, log off that user.

My sservice was created with Delphi 5 Ent. and runs on Windows 2000 Professional.

Best regards,

Luis
0
Comment
Question by:elkavayo
  • 2
3 Comments
 

Author Comment

by:elkavayo
ID: 6246504
I don't know if I made myself clear:

1.- The service runs under the local SYSTEM account.

2.- I need to get the username of the user that's loggued onto the computer (whoever user sits in front of the computer and logs in)

3.- Log off that user if for example, its logon time expires (let's say every user is granted 15 minutes of computer time, it's just an example)
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 6246525
As long as the service is able to interact with the desktop yoy can use this to get the current logged-in user:


function GetCurrentUser: String;
var h: HWND;
    dwProcessId: DWORD;
    hProcess, hToken: THandle;
    a: array[0..255] of Char;
    s: Cardinal;
begin
  h := FindWindow('Progman', nil);
  GetWindowThreadProcessId(h, @dwProcessId);
  hProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
  OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, hToken);
  ImpersonateLoggedOnUser(hToken);
  s := sizeof(a);
  GetUserName(a, s);
  Result := a;
  RevertToSelf;
  CloseHandle(hToken);
  CloseHandle(hProcess);
end;


It's just a rough example (no error checking).

To log the user off try ExitWindows or ExitWindowsEx.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6246532
Like this:

ExitWindowsEx(EWX_FORCE or EWX_LOGOFF, 0);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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

9 Experts available now in Live!

Get 1:1 Help Now