Hi
Try calling EnumProcesses() to enumerate the running processes. EnumProcesses() will get you an array of process ids. If you need the file name of the file that has spawned each process, try using GetModuleFileNameEx() . Before calling this make sure you enumerate all the modules associated with the process by calling EnumerateProcessModules() .
That complete's half of your work.
After you get the handles to all the processes, from the user interface you have designed take the user input(user clicked on xxx process). Try getting the handle to that process. Manage the process from there.
If you want to terminate the process user selected, use TerminateProcess() function.
The above process WILL NOT work for smart devices.
If your project is for smart devices, use the following technique:
<br />
#include "stdafx.h"<br />
#include "SmartPhoneTaskManager.h"<br />
#include "Tlhelp32.h"<br />
#include <windows.h><br />
#include <commctrl.h><br />
<br />
#define MAX_LOADSTRING 100<br />
<br />
HINSTANCE g_hInst;
HWND g_hWndMenuBar;
<br />
ATOM MyRegisterClass(HINSTANCE, LPTSTR);<br />
BOOL InitInstance(HINSTANCE, int);<br />
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);<br />
<br />
BOOL CALLBACK TaskManagerDlgProc(const HWND hTaskManDlg, const UINT uIMessage, <br />
const WPARAM wParam, LPARAM lParam);<br />
<br />
int WINAPI WinMain(HINSTANCE hInstance,<br />
HINSTANCE hPrevInstance,<br />
LPTSTR lpCmdLine,<br />
int nCmdShow)<br />
{<br />
MSG msg;<br />
<br />
if (!InitInstance(hInstance, nCmdShow)) <br />
{<br />
return FALSE;<br />
}<br />
<br />
<br />
while (GetMessage(&msg, NULL, 0, 0)) <br />
{<br />
{<br />
TranslateMessage(&msg);<br />
DispatchMessage(&msg);<br />
}<br />
}<br />
<br />
return (int) msg.wParam;<br />
}<br />
<br />
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)<br />
{<br />
WNDCLASS wc;<br />
<br />
wc.style = CS_HREDRAW | CS_VREDRAW;<br />
wc.lpfnWndProc = WndProc;<br />
wc.cbClsExtra = 0;<br />
wc.cbWndExtra = 0;<br />
wc.hInstance = hInstance;<br />
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMARTPHONETASKMANAGER));<br />
wc.hCursor = 0;<br />
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
wc.lpszMenuName = 0;<br />
wc.lpszClassName = szWindowClass;<br />
<br />
return RegisterClass(&wc);<br />
}<br />
<br />
HWND hTaskManagerDlg;<br />
HWND hListWnd;<br />
DWORD WINAPI ProcessViewThread(PVOID lParam);<br />
<br />
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)<br />
{<br />
HWND hWnd;<br />
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
<br />
g_hInst = hInstance;
<br />
<br />
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); <br />
LoadString(hInstance, IDC_SMARTPHONETASKMANAGER, szWindowClass, MAX_LOADSTRING);<br />
<br />
hWnd = FindWindow(szWindowClass, szTitle); <br />
if (hWnd) <br />
{<br />
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));<br />
return 0;<br />
} <br />
<br />
if (!MyRegisterClass(hInstance, szWindowClass))<br />
{<br />
return FALSE;<br />
}<br />
<br />
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,<br />
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);<br />
<br />
if (!hWnd)<br />
{<br />
return FALSE;<br />
}<br />
<br />
ShowWindow(hWnd, nCmdShow);<br />
UpdateWindow(hWnd);<br />
<br />
hTaskManagerDlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG_TASKMAN),NULL,(DLGPROC)TaskManagerDlgProc);<br />
if(!hTaskManagerDlg)<br />
{<br />
MessageBox(NULL,TEXT("Could not start taskman"),TEXT("Taskman Failed"),MB_OK|MB_ICONERROR);<br />
return FALSE;<br />
<br />
}<br />
<br />
ShowWindow(hTaskManagerDlg,SW_SHOWMAXIMIZED);<br />
UpdateWindow(hTaskManagerDlg);<br />
hListWnd = ::GetDlgItem(hTaskManagerDlg,IDC_LIST_ENUM_TASKS);<br />
::SetFocus(hListWnd);<br />
DWORD threadID;<br />
CreateThread(NULL,0,&ProcessViewThread,0,0,&threadID);<br />
<br />
return TRUE;<br />
}<br />
<br />
<br />
<br />
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
int wmId, wmEvent;<br />
PAINTSTRUCT ps;<br />
HDC hdc;<br />
<br />
<br />
switch (message) <br />
{<br />
case WM_COMMAND:<br />
wmId = LOWORD(wParam); <br />
wmEvent = HIWORD(wParam); <br />
switch (wmId)<br />
{<br />
case IDM_OK:<br />
break;<br />
case IDM_CANCEL:<br />
DestroyWindow(hWnd);<br />
default:<br />
return DefWindowProc(hWnd, message, wParam, lParam);<br />
<br />
}<br />
break;<br />
case WM_CREATE:<br />
SHMENUBARINFO mbi;<br />
<br />
memset(&mbi, 0, sizeof(SHMENUBARINFO));<br />
mbi.cbSize = sizeof(SHMENUBARINFO);<br />
mbi.hwndParent = hWnd;<br />
mbi.nToolBarId = IDR_MENU;<br />
mbi.hInstRes = g_hInst;<br />
<br />
if (!SHCreateMenuBar(&mbi)) <br />
{<br />
g_hWndMenuBar = NULL;<br />
}<br />
else<br />
{<br />
g_hWndMenuBar = mbi.hwndMB;<br />
}<br />
<br />
break;<br />
case WM_PAINT:<br />
hdc = BeginPaint(hWnd, &ps);<br />
<br />
EndPaint(hWnd, &ps);<br />
break;<br />
case WM_DESTROY:<br />
CommandBar_Destroy(g_hWndMenuBar);<br />
PostQuitMessage(0);<br />
break;<br />
<br />
<br />
default:<br />
return DefWindowProc(hWnd, message, wParam, lParam);<br />
}<br />
return 0;<br />
}<br />
<br />
<br />
<br />
BOOL CALLBACK TaskManagerDlgProc(const HWND hTaskManDlg, const UINT uIMessage, <br />
const WPARAM wParam, LPARAM lParam)<br />
{<br />
<br />
HWND statusHandle = GetDlgItem(hTaskManagerDlg,IDC_STATIC_STATUS);<br />
<br />
BOOL hIsHandled = TRUE;<br />
switch(uIMessage)<br />
{<br />
case WM_COMMAND:<br />
{ <br />
switch(LOWORD(wParam))<br />
{<br />
case IDCANCEL:<br />
hIsHandled = TRUE;<br />
DestroyWindow(hTaskManagerDlg);<br />
PostQuitMessage(0);<br />
break;<br />
case IDC_BUTTON_STOPPROCESS:<br />
<br />
hIsHandled = TRUE;<br />
default:<br />
hIsHandled = FALSE;<br />
}<br />
}<br />
case WM_PAINT:<br />
SetWindowText(statusHandle,TEXT("Running..."));<br />
break;<br />
<br />
}<br />
<br />
return hIsHandled;<br />
<br />
}<br />
<br />
<br />
<br />
DWORD WINAPI ProcessViewThread(PVOID lParam)<br />
{<br />
<br />
<br />
<br />
static int index = 0;<br />
HANDLE snapHand;<br />
PROCESSENTRY32 procEntry;<br />
procEntry.dwSize = sizeof(PROCESSENTRY32);<br />
snapHand = CreateToolhelp32Snapshot(TH32CS_SNAPALL|TH32CS_SNAPPROCESS,0);<br />
if(INVALID_HANDLE_VALUE == snapHand)<br />
return -1;<br />
if(!Process32First(snapHand,&procEntry))<br />
{<br />
if(GetLastError() == ERROR_NO_MORE_FILES)<br />
{<br />
SendMessage(hListWnd,LB_ADDSTRING,0,LPARAM(TEXT("EndOfListing")));<br />
}<br />
return -1;<br />
}<br />
procEntry.dwSize = sizeof(procEntry);<br />
do<br />
{<br />
SendMessage(hListWnd,LB_ADDSTRING,index,LPARAM(procEntry.szExeFile));<br />
SendMessage(hListWnd,LB_SETCURSEL,index,0);<br />
index++;<br />
}while(Process32Next(snapHand,&procEntry));<br />
<br />
HWND stopHandle = GetDlgItem(hTaskManagerDlg,IDC_BUTTON_STOPPROCESS);<br />
::SetFocus(stopHandle);<br />
<br />
<br />
CloseToolhelp32Snapshot(snapHand);<br />
<br />
<br />
<br />
return 0;<br />
<br />
<br />
}<br />
|