disable defining ntstatus codes in windows.h/winnt.h when using ndk
[reactos.git] / rosapps / sysutils / ctm / ctm.c
index ad58130..b221817 100644 (file)
@@ -3,11 +3,11 @@
    ctm.c - main program file
 
    Written by: Aleksey Bragin (aleksey@studiocerebral.com)
-   
+
    Most of the code dealing with getting system parameters is taken from
    ReactOS Task Manager written by Brian Palmer (brianp@reactos.org)
-   
-   Localization features added by HervĂ© Poussineau (hpoussineau@fr.st)
+
+   Localization features added by HervĂ© Poussineau (hpoussin@reactos.org)
 
    History:
    24 October 2004 - added localization features
        20 March 2003 - v0.03, works good under ReactOS, and allows process
                        killing
        18 March 2003 - Initial version 0.01, doesn't work under RectOS
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 
-//#define WIN32_LEAN_AND_MEAN          // Exclude rarely-used stuff from Windows //headers
+#define WIN32_LEAN_AND_MEAN            // Exclude rarely-used stuff from Windows //headers
+#define WIN32_NO_STATUS
 #include <windows.h>
+
 #include <stdlib.h>
 #include <malloc.h>
 #include <memory.h>
 #include <process.h>
 #include <stdio.h>
 
-#include <ddk/ntddk.h>
-#include <epsapi.h>
-#include <ntos/zwtypes.h>
+#define NTOS_MODE_USER
+#include <ndk/ntndk.h>
+
+#include <epsapi/epsapi.h>
 
 #include "ctm.h"
 #include "resource.h"
@@ -61,7 +64,7 @@ DWORD columnRightPositions[5];
 TCHAR lpSeparator[80];
 TCHAR lpHeader[80];
 TCHAR lpMemUnit[3];
-TCHAR lpIdleProcess[80];;
+TCHAR lpIdleProcess[80];
 TCHAR lpTitle[80];
 TCHAR lpHeader[80];
 TCHAR lpMenu[80];
@@ -136,10 +139,9 @@ void DisplayScreen()
 {
        COORD pos;
        TCHAR lpStr[80];
-       int posStr;
        DWORD numChars;
        int lines;
-       int idx, i;
+       int idx;
 
        if (first == 0)
        {
@@ -159,7 +161,7 @@ void DisplayScreen()
                // Footer
                pos.X = 2; pos.Y = ProcPerScreen+6;
                WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
-               
+
                // Menu
                pos.X = 2; pos.Y = ProcPerScreen+7;
                WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
@@ -168,20 +170,19 @@ void DisplayScreen()
                first = 1;
        }
 
-       // Processess
+       // Processess
        lines = ProcessCount;
        if (lines > MAX_PROC)
                lines = MAX_PROC;
        for (idx=0; idx<MAX_PROC; idx++)
        {
                int len, i;
-               TCHAR imgName[MAX_PATH];
                TCHAR lpPid[8];
                TCHAR lpCpu[6];
                TCHAR lpMemUsg[12];
                TCHAR lpPageFaults[15];
                WORD wColor;
-               
+
                for (i = 0; i < 80; i++)
                        lpStr[i] = _T(' ');
 
@@ -189,8 +190,8 @@ void DisplayScreen()
                if (idx < lines && scrolled + idx < ProcessCount)
                {
                        // image name
-#ifdef _UNICODE                
-                  len = wcslen(pPerfData[scrolled+idx].ImageName);  
+#ifdef _UNICODE
+                  len = wcslen(pPerfData[scrolled+idx].ImageName);
 #else
                   WideCharToMultiByte(CP_ACP, 0, pPerfData[scrolled+idx].ImageName, -1,
                                       imgName, MAX_PATH, NULL, NULL);
@@ -237,25 +238,25 @@ void DisplayScreen()
                pos.X = 3; pos.Y = 6+idx;
                if (selection == idx)
                {
-                       wColor = BACKGROUND_GREEN | 
-                               FOREGROUND_RED | 
-                               FOREGROUND_GREEN | 
+                       wColor = BACKGROUND_GREEN |
+                               FOREGROUND_RED |
+                               FOREGROUND_GREEN |
                                FOREGROUND_BLUE;
                }
                else
                {
                        wColor = BACKGROUND_BLUE |
-                                       FOREGROUND_RED | 
-                                       FOREGROUND_GREEN | 
+                                       FOREGROUND_RED |
+                                       FOREGROUND_GREEN |
                                        FOREGROUND_BLUE;
                }
 
-               FillConsoleOutputAttribute( 
-                       hStdout,          // screen buffer handle 
-                       wColor,           // color to fill with 
+               FillConsoleOutputAttribute(
+                       hStdout,          // screen buffer handle
+                       wColor,           // color to fill with
                        columnRightPositions[0] - 1,    // number of cells to fill
-                       pos,            // first cell to write to 
-                       &numChars);       // actual number written 
+                       pos,            // first cell to write to
+                       &numChars);       // actual number written
        }
 
        return;
@@ -292,7 +293,7 @@ int ProcessKeys(int numEvents)
                        HANDLE hProcess;
                        pId = pPerfData[selection+scrolled].ProcessId;
                        hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pId);
-                       
+
                        if (hProcess)
                        {
                                if (!TerminateProcess(hProcess, 0))
@@ -318,7 +319,7 @@ int ProcessKeys(int numEvents)
                                Sleep(1000);
                        }
                }
-               
+
                first = 0;
        }
        else if (key == VK_UP)
@@ -335,7 +336,7 @@ int ProcessKeys(int numEvents)
                else if ((selection == MAX_PROC-1) && (selection+scrolled < ProcessCount-1))
                        scrolled++;
        }
-       
+
        return FALSE;
 }
 
@@ -349,14 +350,15 @@ void PerfDataRefresh()
        LONG                                                    status;
        ULONG                                                   ulSize;
        LPBYTE                                                  pBuffer;
-       ULONG                                                   BufferSize;
        ULONG                                                   Idx, Idx2;
-       HANDLE                                                  hProcess;
-       HANDLE                                                  hProcessToken;
        PSYSTEM_PROCESS_INFORMATION             pSPI;
        PPERFDATA                                               pPDOld;
+#ifdef EXTRA_INFO
+       HANDLE                                                  hProcess;
+       HANDLE                                                  hProcessToken;
        TCHAR                                                   szTemp[MAX_PATH];
        DWORD                                                   dwSize;
+#endif
 #ifdef TIMES
        LARGE_INTEGER                                           liCurrentKernelTime;
        LARGE_INTEGER                                           liCurrentIdleTime;
@@ -371,9 +373,9 @@ void PerfDataRefresh()
        if (status != NO_ERROR)
                return;
 #endif
-       // Get processor information    
-       SysProcessorTimeInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)malloc(sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberProcessors);
-       status = NtQuerySystemInformation(SystemProcessorPerformanceInformation, SysProcessorTimeInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberProcessors, &ulSize);
+       // Get processor information
+       SysProcessorTimeInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)malloc(sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors);
+       status = NtQuerySystemInformation(SystemProcessorPerformanceInformation, SysProcessorTimeInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors, &ulSize);
 
 
        // Get process information
@@ -382,7 +384,7 @@ void PerfDataRefresh()
 #ifdef TIMES
        liCurrentKernelTime.QuadPart = 0;
        liCurrentIdleTime.QuadPart = 0;
-       for (Idx=0; Idx<SystemBasicInfo.NumberProcessors; Idx++) {
+       for (Idx=0; Idx<SystemBasicInfo.NumberOfProcessors; Idx++) {
                liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].KernelTime.QuadPart;
                liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].DpcTime.QuadPart;
                liCurrentKernelTime.QuadPart += SysProcessorTimeInfo[Idx].InterruptTime.QuadPart;
@@ -402,10 +404,10 @@ void PerfDataRefresh()
                // CurrentCpuIdle = IdleTime / SystemTime
                dbIdleTime = dbIdleTime / dbSystemTime;
                dbKernelTime = dbKernelTime / dbSystemTime;
-               
+
                // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
-               dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.NumberProcessors;// + 0.5; 
-               dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.NumberProcessors;// + 0.5;
+               dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors;// + 0.5;
+               dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors;// + 0.5;
        }
 
        // Store new CPU's idle and system time
@@ -470,7 +472,7 @@ void PerfDataRefresh()
                        double  CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
                        double  OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
                        double  CpuTime = (CurTime - OldTime) / dbSystemTime;
-                       CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.NumberProcessors; // + 0.5;
+                       CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; // + 0.5;
 
                        pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
 #else
@@ -499,7 +501,7 @@ void PerfDataRefresh()
                //pPerfData[Idx].SessionId = pSPI->SessionId;
 
 #ifdef EXTRA_INFO
-               hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pSPI->UniqueProcessId);
+               hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, (DWORD)pSPI->UniqueProcessId);
                if (hProcess) {
                        if (OpenProcessToken(hProcess, TOKEN_QUERY|TOKEN_DUPLICATE|TOKEN_IMPERSONATE, &hProcessToken)) {
                                ImpersonateLoggedOnUser(hProcessToken);
@@ -508,16 +510,6 @@ void PerfDataRefresh()
                                GetUserName(szTemp, &dwSize);
 #ifndef UNICODE
                                MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szTemp, -1, pPerfData[Idx].UserName, MAX_PATH);
-/*
-int MultiByteToWideChar(
-  UINT CodePage,         // code page
-  DWORD dwFlags,         // character-type options
-  LPCSTR lpMultiByteStr, // string to map
-  int cbMultiByte,       // number of bytes in string
-  LPWSTR lpWideCharStr,  // wide-character buffer
-  int cchWideChar        // size of buffer
-);
- */
 #endif
                                RevertToSelf();
                                CloseHandle(hProcessToken);
@@ -539,7 +531,6 @@ int MultiByteToWideChar(
 // Code partly taken from slw32tty.c from mc/slang
 unsigned int GetKeyPressed(int events)
 {
-       long key;
        DWORD bytesRead;
        INPUT_RECORD record;
        int i;
@@ -559,11 +550,11 @@ unsigned int GetKeyPressed(int events)
 }
 
 
-int main(int *argc, char **argv)
+int main(int argc, char **argv)
 {
        int i;
        TCHAR lpStr[80];
-       
+
        for (i = 0; i < 80; i++)
                lpEmpty[i] = lpHeader[i] = _T(' ');
        lpEmpty[79] = _T('\0');
@@ -595,7 +586,7 @@ int main(int *argc, char **argv)
                columnRightPositions[4] = columnRightPositions[3] + _tcslen(lpStr) + 3;
                _tcsncpy(&lpHeader[columnRightPositions[3] + 2], lpStr, _tcslen(lpStr));
        }
-       
+
        for (i = 0; i < columnRightPositions[4]; i++)
                lpSeparator[i] = _T('-');
        lpHeader[0] = _T('|');
@@ -608,7 +599,7 @@ int main(int *argc, char **argv)
        lpSeparator[columnRightPositions[4] + 1] = _T('\0');
        lpHeader[columnRightPositions[4] + 1] = _T('\0');
 
-       
+
        if (!LoadString(hInst, IDS_APP_TITLE, lpTitle, 80))
                lpTitle[0] = _T('\0');
        if (!LoadString(hInst, IDS_COLUMN_MEM_UNIT, lpMemUnit, 3))
@@ -617,7 +608,7 @@ int main(int *argc, char **argv)
                lpMenu[0] = _T('\0');
        if (!LoadString(hInst, IDS_IDLE_PROCESS, lpIdleProcess, 80))
                lpIdleProcess[0] = _T('\0');
-       
+
        if (LoadString(hInst, IDS_MENU_QUIT, lpStr, 2))
                KEY_QUIT = lpStr[0];
        if (LoadString(hInst, IDS_MENU_KILL_PROCESS, lpStr, 2))