Solved

how to log off the interactive user from a service

Posted on 2001-07-02
3
201 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

16 Experts available now in Live!

Get 1:1 Help Now