Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <tchar.h>
- #include <TlHelp32.h>
- #include "resource.h"
- #define IDLB_LISTBOXPROC 10000
- #define IDLB_LISTBOXMODULE 10001
- #define IDB_REFRESH 10002
- HINSTANCE hInst; // Текущий экземпляр
- HWND hMainWnd; // Handle главного окна
- HWND hListBoxProc, hListBoxModule;
- TCHAR* szTitle; // Текст строки заголовка
- TCHAR* szWindowClass; // имя класса главного окна
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- void WINAPI FillProcessListBox(HWND, HWND);
- void WINAPI FillModulesListBox(HWND, HWND, DWORD);
- TCHAR* WINAPI GetPriorityName(DWORD);
- void WINAPI ChangePriority(HWND, DWORD);
- BOOL WINAPI SetProcessPriority(HWND, DWORD, DWORD);
- BOOL WINAPI DisplayContextMenu(HWND, HWND, WORD, WORD);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- hInst = hInstance;
- szWindowClass = _tcsdup(_T("lab7"));
- szTitle = _tcsdup(_T("Lab7 - Process priority"));
- //--------------------- регистрация класса окна
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, IDC_ICON);
- wcex.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_ARROW));
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = NULL;
- if (!RegisterClassEx(&wcex))
- {
- MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("Lab7"), MB_ICONERROR | MB_OK);
- return 1;
- }
- //---------------------- создание и отрисовка окна
- HWND hWnd = CreateWindow(
- szWindowClass, szTitle,
- WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME,
- CW_USEDEFAULT, CW_USEDEFAULT,
- 805, 599,
- NULL,
- NULL,
- hInstance,
- NULL);
- if (!hWnd)
- {
- MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Lab6"), MB_ICONERROR | MB_OK);
- return 1;
- }
- hMainWnd = hWnd;
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- //-------------------- обработчик сообщений
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!IsDialogMessage(hWnd, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- LRESULT res;
- WORD controlID, controlNotifCode;
- HWND control;
- WORD x, y;
- DWORD processID;
- DWORD priorityClass;
- switch (message)
- {
- case WM_CREATE:
- hListBoxProc = CreateWindow(_T("LISTBOX"), _T(""), WS_VISIBLE | WS_CHILD | WS_TABSTOP | LBS_STANDARD,
- 0, 25,
- 400, 550,
- hWnd, (HMENU)IDLB_LISTBOXPROC,
- hInst, NULL);
- hListBoxModule = CreateWindow(_T("LISTBOX"), _T(""), WS_VISIBLE | WS_CHILD | WS_TABSTOP | LBS_STANDARD & ~LBS_SORT,
- 400, 25,
- 400, 550,
- hWnd, (HMENU)IDLB_LISTBOXMODULE,
- hInst, NULL);
- CreateWindow(_T("STATIC"), _T("Processes:"), WS_VISIBLE | WS_CHILD | SS_CENTER,
- 0, 5,
- 400, 20,
- hWnd, NULL,
- hInst, NULL);
- CreateWindow(_T("STATIC"), _T("Modules:"), WS_VISIBLE | WS_CHILD | SS_CENTER,
- 400, 5,
- 400, 20,
- hWnd, NULL,
- hInst, NULL);
- CreateWindow(_T("BUTTON"), _T("Refresh"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
- 360, 0,
- 60, 25,
- hWnd, (HMENU)IDB_REFRESH,
- hInst, NULL);
- FillProcessListBox(hWnd, hListBoxProc);
- break;
- case WM_COMMAND:
- controlID = LOWORD(wParam);
- controlNotifCode = HIWORD(wParam);
- control = (HWND)lParam;
- switch (controlID)
- {
- case IDLB_LISTBOXPROC:
- if (controlNotifCode == LBN_SELCHANGE)
- {
- res = SendMessage(control, LB_GETCURSEL, NULL, NULL);
- if ( res != LB_ERR)
- {
- res = SendMessage(control, LB_GETITEMDATA, res, NULL);
- if (res != LB_ERR)
- {
- FillModulesListBox(hWnd, hListBoxModule, res);
- }
- }
- }
- break;
- case IDB_REFRESH:
- FillProcessListBox(hWnd, hListBoxProc);
- SendMessage(hListBoxModule, LB_RESETCONTENT, NULL, NULL);
- break;
- case ID_FILE_REALTIME:
- ChangePriority(hWnd, REALTIME_PRIORITY_CLASS);
- break;
- case ID_FILE_HIGH:
- ChangePriority(hWnd, HIGH_PRIORITY_CLASS);
- break;
- case ID_FILE_ABOVENORMAL:
- ChangePriority(hWnd, ABOVE_NORMAL_PRIORITY_CLASS);
- break;
- case ID_FILE_NORMAL:
- ChangePriority(hWnd, NORMAL_PRIORITY_CLASS);
- break;
- case ID_FILE_BELOWNORMAL:
- ChangePriority(hWnd, BELOW_NORMAL_PRIORITY_CLASS);
- break;
- case ID_FILE_IDLE:
- ChangePriority(hWnd, IDLE_PRIORITY_CLASS);
- break;
- }
- break;
- case WM_CONTEXTMENU:
- control = (HWND)wParam;
- x = LOWORD(lParam);
- y = HIWORD(lParam);
- if (!DisplayContextMenu(hWnd, control, x, y))
- return DefWindowProc(hWnd, message, wParam, lParam);
- break;
- case WM_PAINT: //-------------------- отрисовка окна
- hdc = BeginPaint(hWnd, &ps);
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY: //---------------- выход из приложения
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- void WINAPI FillProcessListBox(HWND hWnd, HWND hListBox)
- {
- HANDLE hSnapshot;
- PROCESSENTRY32 pe;
- HANDLE hProcess;
- DWORD processPriorityClass;
- LRESULT res;
- TCHAR processInfo[MAX_PATH + 30];
- SendMessage(hListBox, LB_RESETCONTENT, NULL, NULL);
- hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hSnapshot == INVALID_HANDLE_VALUE)
- {
- MessageBox(NULL, _T("Can't take processess snapshot!"), _T("Lab7"), MB_ICONERROR | MB_OK);
- SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- pe.dwSize = sizeof(PROCESSENTRY32);
- if (!Process32First(hSnapshot, &pe))
- {
- MessageBox(NULL, _T("Can't get processess list"), _T("Lab7"), MB_ICONERROR | MB_OK);
- SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- do
- {
- hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pe.th32ProcessID);
- if (hProcess == NULL)
- processPriorityClass = -1;
- else
- {
- processPriorityClass = GetPriorityClass(hProcess);
- CloseHandle(hProcess);
- }
- _tcscpy(processInfo, pe.szExeFile);
- _tcscat(processInfo, _T(" | Priority: "));
- _tcscat(processInfo, GetPriorityName(processPriorityClass));
- res = SendMessage(hListBox, LB_ADDSTRING, NULL, (LPARAM)processInfo);
- res = SendMessage(hListBox, LB_SETITEMDATA, res, (LPARAM)pe.th32ProcessID);
- if (res == LB_ERR)
- {
- MessageBox(NULL, _T("Can't add info in listbox"), _T("Lab7"), MB_ICONERROR | MB_OK);
- SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- } while(Process32Next(hSnapshot, &pe));
- CloseHandle(hSnapshot);
- }
- TCHAR* WINAPI GetPriorityName(DWORD priority)
- {
- TCHAR* res = NULL;
- if (priority == REALTIME_PRIORITY_CLASS)
- res = _tcsdup(_T("RealTime"));
- else if (priority == HIGH_PRIORITY_CLASS)
- res = _tcsdup(_T("High"));
- else if (priority == ABOVE_NORMAL_PRIORITY_CLASS)
- res = _tcsdup(_T("Above normal"));
- else if (priority == NORMAL_PRIORITY_CLASS)
- res = _tcsdup(_T("Normal"));
- else if (priority == BELOW_NORMAL_PRIORITY_CLASS)
- res = _tcsdup(_T("Below normal"));
- else if (priority == IDLE_PRIORITY_CLASS)
- res = _tcsdup(_T("Idle"));
- else
- res = _tcsdup(_T("Undefined"));
- return res;
- }
- void WINAPI FillModulesListBox(HWND hWnd, HWND hListBox, DWORD processID)
- {
- HANDLE hSnapshot, hProcess;
- MODULEENTRY32 me;
- LRESULT res;
- TCHAR moduleInfo[MAX_PATH];
- SendMessage(hListBox, LB_RESETCONTENT, NULL, NULL);
- hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processID);
- if (hProcess == INVALID_HANDLE_VALUE)
- {
- _tcscpy(moduleInfo, _T("Process doesn't exist!"));
- res = SendMessage(hListBox, LB_ADDSTRING, NULL, (LPARAM)moduleInfo);
- //MessageBox(NULL, _T("Process doesn't exist!"), _T("Lab7"), MB_ICONERROR | MB_OK);
- return;
- //SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- CloseHandle(hProcess);
- hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processID);
- if (hSnapshot == INVALID_HANDLE_VALUE)
- {
- _tcscpy(moduleInfo, _T("Can't take modules snapshot!"));
- res = SendMessage(hListBox, LB_ADDSTRING, NULL, (LPARAM)moduleInfo);
- //MessageBox(NULL, _T("Can't take modules snapshot!"), _T("Lab7"), MB_ICONERROR | MB_OK);
- return;
- //SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- me.dwSize = sizeof(MODULEENTRY32);
- if (!Module32First(hSnapshot, &me))
- {
- MessageBox(NULL, _T("Can't get modules list"), _T("Lab7"), MB_ICONERROR | MB_OK);
- return;
- //SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- do
- {
- _tcscpy(moduleInfo, me.szModule);
- res = SendMessage(hListBox, LB_ADDSTRING, NULL, (LPARAM)moduleInfo);
- if (res == LB_ERR)
- {
- MessageBox(NULL, _T("Can't add info in listbox"), _T("Lab7"), MB_ICONERROR | MB_OK);
- SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- } while(Module32Next(hSnapshot, &me));
- CloseHandle(hSnapshot);
- }
- BOOL WINAPI DisplayContextMenu(HWND hWnd, HWND control, WORD x, WORD y)
- {
- RECT rect;
- POINT p = {x, y};
- GetClientRect(control, &rect);
- rect.top += 30;
- rect.bottom += 30;
- if (p.x > 10000)
- GetCursorPos(&p);
- ScreenToClient(hWnd, &p);
- if (!PtInRect(&rect, p))
- return FALSE;
- ClientToScreen(hWnd, &p);
- HMENU hMenu;
- hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU1));
- if (hMenu == NULL)
- {
- MessageBox(NULL, _T("Can't load menu"), _T("Lab7"), MB_ICONERROR | MB_OK);
- SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- hMenu = GetSubMenu(hMenu, 0);
- TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
- p.x, p.y, NULL, hWnd, NULL);
- DestroyMenu(hMenu);
- return TRUE;
- }
- void WINAPI ChangePriority(HWND hWnd, DWORD priorityClass)
- {
- DWORD line, processID;
- TCHAR processInfo[MAX_PATH + 30];
- line = SendMessage(hListBoxProc, LB_GETCURSEL, NULL, NULL);
- if ( line != LB_ERR)
- {
- processID = SendMessage(hListBoxProc, LB_GETITEMDATA, line, NULL);
- if (processID != LB_ERR)
- {
- if(!SetProcessPriority(hWnd, processID, priorityClass))
- return;
- SendMessage(hListBoxProc, LB_GETTEXT, line, (LPARAM)processInfo);
- TCHAR c = '|';
- TCHAR* str = processInfo;
- size_t pos = 0;
- while (*str != c && *str != '\0')
- {
- pos++;
- str++;
- }
- if (*str == '\0')
- {
- MessageBox(NULL, _T("Can't parse name!"), _T("Lab7"), MB_ICONERROR | MB_OK);
- SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- pos += 2;
- processInfo[pos] = '\0';
- _tcscat(processInfo, _T("Priority: "));
- _tcscat(processInfo, GetPriorityName(priorityClass));
- SendMessage(hListBoxProc, LB_DELETESTRING, line, NULL);
- line = SendMessage(hListBoxProc, LB_INSERTSTRING, line, (LPARAM)processInfo);
- SendMessage(hListBoxProc, LB_SETITEMDATA, line, processID);
- }
- }
- else
- {
- MessageBox(NULL, _T("At first select process."), _T("Lab7"), MB_ICONINFORMATION | MB_OK);
- }
- }
- BOOL WINAPI SetProcessPriority(HWND hWnd, DWORD processID, DWORD priorityClass)
- {
- HANDLE hProcess;
- hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, processID);
- if (hProcess == INVALID_HANDLE_VALUE)
- {
- MessageBox(NULL, _T("Can't open process to set priority!"), _T("Lab7"), MB_ICONERROR | MB_OK);
- return FALSE;
- //SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- if (!SetPriorityClass(hProcess, priorityClass))
- {
- MessageBox(NULL, _T("Can't set priority!"), _T("Lab7"), MB_ICONERROR | MB_OK);
- return FALSE;
- //SendMessage(hWnd, WM_DESTROY, NULL, NULL);
- }
- CloseHandle(hProcess);
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement