[INTL]
[reactos.git] / base / applications / logoff / logoff.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS logoff utility
4 * FILE: base\applications\logoff\logoff.c
5 * PURPOSE: Logoff current session, or another session, potentially on another machine
6 * AUTHOR: 30.07.2007 - Frode Lillerud
7 */
8
9 /* Note
10 * This application is a lightweight version of shutdown.exe. It is intended to be function-compatible
11 * with Windows' system32\logoff.exe commandline application.
12 */
13
14 #include "precomp.h"
15
16 #include <stdio.h>
17 #include <tchar.h>
18
19 //Commandline argument switches
20 LPTSTR szRemoteServerName = NULL;
21 BOOL bVerbose;
22
23 //----------------------------------------------------------------------
24 //
25 //Retrieve resource string and output the Usage to the console
26 //
27 //----------------------------------------------------------------------
28 static void PrintUsage() {
29 LPTSTR lpUsage = NULL;
30
31 if (AllocAndLoadString(&lpUsage, GetModuleHandle(NULL), IDS_USAGE)) {
32 _putts(lpUsage);
33 }
34
35 }
36
37 //----------------------------------------------------------------------
38 //
39 // Writes the last error as both text and error code to the console.
40 //
41 //----------------------------------------------------------------------
42 void DisplayLastError()
43 {
44 int errorCode = GetLastError();
45 LPTSTR lpMsgBuf;
46
47 // Display the error message to the user
48 FormatMessage(
49 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
50 NULL,
51 errorCode,
52 LANG_USER_DEFAULT,
53 (LPTSTR) &lpMsgBuf,
54 0,
55 NULL);
56
57 _ftprintf(stderr, lpMsgBuf);
58 _ftprintf(stderr, _T("Error code: %d\n"), errorCode);
59
60 LocalFree(lpMsgBuf);
61 }
62
63 //----------------------------------------------------------------------
64 //
65 //Sets flags based on commandline arguments
66 //
67 //----------------------------------------------------------------------
68 BOOL ParseCommandLine(int argc, TCHAR *argv[])
69 {
70 int i;
71 LPTSTR lpIllegalMsg;
72
73 //FIXME: Add handling of commandline arguments to select the session number and name, and also name of remote machine
74 //Example: logoff.exe 4 /SERVER:Master should logoff session number 4 on remote machine called Master.
75
76 for (i = 1; i < argc; i++) {
77 switch(argv[i][0]){
78 case '-':
79 case '/':
80 // -v (verbose)
81 if (argv[i][1] == 'v') {
82 bVerbose = TRUE;
83 break; //continue parsing the arguments
84 }
85 // -? (usage)
86 else if(argv[i][1] == '?') {
87 return FALSE; //display the Usage
88 }
89 default:
90 //Invalid parameter detected
91 if (AllocAndLoadString(&lpIllegalMsg, GetModuleHandle(NULL), IDS_ILLEGAL_PARAM))
92 _putts(lpIllegalMsg);
93 return FALSE;
94 }
95 }
96
97 return TRUE;
98 }
99
100 //----------------------------------------------------------------------
101 //
102 //Main entry for program
103 //
104 //----------------------------------------------------------------------
105 int _tmain(int argc, TCHAR *argv[])
106 {
107 LPTSTR lpLogoffRemote, lpLogoffLocal;
108
109 //
110 // Parse command line
111 //
112 if (!ParseCommandLine(argc, argv)) {
113 PrintUsage();
114 return 1;
115 }
116
117 //
118 //Should we log off session on remote server?
119 //
120 if (szRemoteServerName) {
121 if (bVerbose) {
122 if (AllocAndLoadString(&lpLogoffRemote, GetModuleHandle(NULL), IDS_LOGOFF_REMOTE))
123 _putts(lpLogoffRemote);
124 }
125
126 //FIXME: Add Remote Procedure Call to logoff user on a remote machine
127 _ftprintf(stderr, "Remote Procedure Call in logoff.exe has not been implemented");
128 }
129 //
130 //Perform logoff of current session on local machine instead
131 //
132 else {
133 if (bVerbose) {
134 //Get resource string, and print it.
135 if (AllocAndLoadString(&lpLogoffLocal, GetModuleHandle(NULL), IDS_LOGOFF_LOCAL))
136 _putts(lpLogoffLocal);
137 }
138
139 //Actual logoff
140 if (!ExitWindows(NULL, NULL)) {
141 DisplayLastError();
142 return 1;
143 }
144 }
145
146 return 0;
147 }
148 /* EOF */