2007-08-03 15:14:49 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS logoff utility
|
|
|
|
* FILE: base\applications\logoff\logoff.c
|
|
|
|
* PURPOSE: Logoff current session, or another session, potentially on another machine
|
|
|
|
* AUTHOR: 30.07.2007 - Frode Lillerud
|
|
|
|
*/
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
/* Note
|
|
|
|
* This application is a lightweight version of shutdown.exe. It is intended to be function-compatible
|
|
|
|
* with Windows' system32\logoff.exe commandline application.
|
|
|
|
*/
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
#include "precomp.h"
|
|
|
|
|
2014-01-06 20:54:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
//Commandline argument switches
|
|
|
|
LPTSTR szRemoteServerName = NULL;
|
|
|
|
BOOL bVerbose;
|
|
|
|
|
2007-10-19 23:21:45 +00:00
|
|
|
//----------------------------------------------------------------------
|
2007-08-03 15:14:49 +00:00
|
|
|
//
|
|
|
|
//Retrieve resource string and output the Usage to the console
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
static void PrintUsage() {
|
|
|
|
LPTSTR lpUsage = NULL;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
if (AllocAndLoadString(&lpUsage, GetModuleHandle(NULL), IDS_USAGE)) {
|
|
|
|
_putts(lpUsage);
|
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Writes the last error as both text and error code to the console.
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void DisplayLastError()
|
|
|
|
{
|
|
|
|
int errorCode = GetLastError();
|
|
|
|
LPTSTR lpMsgBuf;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
// Display the error message to the user
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL,
|
|
|
|
errorCode,
|
|
|
|
LANG_USER_DEFAULT,
|
|
|
|
(LPTSTR) &lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL);
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
_ftprintf(stderr, lpMsgBuf);
|
|
|
|
_ftprintf(stderr, _T("Error code: %d\n"), errorCode);
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//Sets flags based on commandline arguments
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
BOOL ParseCommandLine(int argc, TCHAR *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
LPTSTR lpIllegalMsg;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
//FIXME: Add handling of commandline arguments to select the session number and name, and also name of remote machine
|
|
|
|
//Example: logoff.exe 4 /SERVER:Master should logoff session number 4 on remote machine called Master.
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
switch(argv[i][0]){
|
|
|
|
case '-':
|
|
|
|
case '/':
|
|
|
|
// -v (verbose)
|
|
|
|
if (argv[i][1] == 'v') {
|
|
|
|
bVerbose = TRUE;
|
|
|
|
break; //continue parsing the arguments
|
|
|
|
}
|
|
|
|
// -? (usage)
|
|
|
|
else if(argv[i][1] == '?') {
|
|
|
|
return FALSE; //display the Usage
|
2007-10-19 23:21:45 +00:00
|
|
|
}
|
2007-08-03 15:14:49 +00:00
|
|
|
default:
|
|
|
|
//Invalid parameter detected
|
|
|
|
if (AllocAndLoadString(&lpIllegalMsg, GetModuleHandle(NULL), IDS_ILLEGAL_PARAM))
|
2007-10-19 23:21:45 +00:00
|
|
|
_putts(lpIllegalMsg);
|
2007-08-03 15:14:49 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//Main entry for program
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int _tmain(int argc, TCHAR *argv[])
|
|
|
|
{
|
|
|
|
LPTSTR lpLogoffRemote, lpLogoffLocal;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Parse command line
|
|
|
|
//
|
2007-10-19 23:21:45 +00:00
|
|
|
if (!ParseCommandLine(argc, argv)) {
|
2007-08-03 15:14:49 +00:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
//
|
|
|
|
//Should we log off session on remote server?
|
|
|
|
//
|
|
|
|
if (szRemoteServerName) {
|
2007-10-19 23:21:45 +00:00
|
|
|
if (bVerbose) {
|
2007-08-03 15:14:49 +00:00
|
|
|
if (AllocAndLoadString(&lpLogoffRemote, GetModuleHandle(NULL), IDS_LOGOFF_REMOTE))
|
2007-10-19 23:21:45 +00:00
|
|
|
_putts(lpLogoffRemote);
|
2007-08-03 15:14:49 +00:00
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
//FIXME: Add Remote Procedure Call to logoff user on a remote machine
|
|
|
|
_ftprintf(stderr, "Remote Procedure Call in logoff.exe has not been implemented");
|
|
|
|
}
|
|
|
|
//
|
|
|
|
//Perform logoff of current session on local machine instead
|
|
|
|
//
|
|
|
|
else {
|
|
|
|
if (bVerbose) {
|
|
|
|
//Get resource string, and print it.
|
|
|
|
if (AllocAndLoadString(&lpLogoffLocal, GetModuleHandle(NULL), IDS_LOGOFF_LOCAL))
|
2007-10-19 23:21:45 +00:00
|
|
|
_putts(lpLogoffLocal);
|
2007-08-03 15:14:49 +00:00
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
//Actual logoff
|
|
|
|
if (!ExitWindows(NULL, NULL)) {
|
|
|
|
DisplayLastError();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2007-08-03 15:14:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* EOF */
|