Solved

how to log off the interactive user from a service

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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