Prøv at kikke på:
#define _WIN32_IE 0x0501
#include <windows.h>
#include <sstream>
#include <commctrl.h>
#include <psapi.h>
#include <vector>
enum WindowsId
{
ListViewId = 1024,
ButtonId
};
HINSTANCE InstanceHandle;
void GetFileAndExt(const char *FullPath, char *Name)
{
char Ext[MAX_PATH];
_splitpath(FullPath, NULL, NULL, Name, Ext);
strcat(Name, Ext);
}
template <typename T>
std::string ToString(T aValue)
{
std::stringstream ss;
ss << aValue;
return ss.str();
}
class ItemDataClass
{
public:
std::string ProcessName;
std::string MemoryUsage;
int ImageIdx;
};
std::vector<ItemDataClass> ItemDataVector;
void UpdateList(HWND aHwnd)
{
HWND List = GetDlgItem(aHwnd, ListViewId);
SendMessage(List, LVM_DELETEALLITEMS, 0, 0);
ItemDataVector.clear();
DWORD Processes[1024], Num;
EnumProcesses(Processes, sizeof(Processes), &Num);
DWORD i;
HIMAGELIST ImageList;
for(i = 0; i < Num/sizeof(DWORD); i++)
{
HMODULE ModHandle[1024];
DWORD ModulesNeeded;
HANDLE ProcessHandle;
ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, Processes[ i ]);
if(EnumProcessModules(ProcessHandle, ModHandle, sizeof(ModHandle), &ModulesNeeded))
{
char ModuleName[MAX_PATH], FileName[MAX_PATH];
if(GetModuleFileNameEx(ProcessHandle, ModHandle[0], ModuleName, sizeof(ModuleName)))
{
GetFileAndExt(ModuleName, FileName);
ItemDataClass ItemData;
ItemData.ProcessName = FileName;
SHFILEINFO ShFileInfo;
ImageList = (HIMAGELIST )SHGetFileInfo(ModuleName, 0, &ShFileInfo, sizeof(ShFileInfo), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
ItemData.ImageIdx = ShFileInfo.iIcon;
PROCESS_MEMORY_COUNTERS ProcessMemoryCounters;
GetProcessMemoryInfo(ProcessHandle, &ProcessMemoryCounters, sizeof(ProcessMemoryCounters));
ItemData.MemoryUsage = ToString(ProcessMemoryCounters.WorkingSetSize);
ItemDataVector.push_back(ItemData);
}
}
}
SendMessage(List, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM )ImageList);
for(i = 0; i < ItemDataVector.size(); i++)
{
LVITEM lvc;
memset(&lvc, 0, sizeof(lvc));
lvc.mask = LVIF_TEXT | LVIF_IMAGE;
lvc.pszText = (char *)ItemDataVector[ i ].ProcessName.c_str();
lvc.iItem = i;
lvc.iImage = ItemDataVector[ i ].ImageIdx;
SendMessage(List, LVM_INSERTITEM, 0, (LPARAM )&lvc);
lvc.pszText = (char *)ItemDataVector[ i ].MemoryUsage.c_str();
lvc.iSubItem = 1;
SendMessage(List, LVM_SETITEMTEXT, i, (LPARAM )&lvc);
}
}
LRESULT CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
HWND List = CreateWindowEx(WS_EX_CLIENTEDGE,
WC_LISTVIEW,
"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP | LVS_REPORT /* | LVS_OWNERDRAWFIXED */,
5, 5, 280, 290,
hwndDlg,
(HMENU)(ListViewId),
InstanceHandle,
0);
ListView_SetExtendedListViewStyle(List, LVS_EX_CHECKBOXES | LVS_EX_GRIDLINES);
LVCOLUMN Column;
memset(&Column, 0, sizeof(Column));
Column.mask = LVCF_TEXT | LVCF_WIDTH;
Column.cx = 125;
Column.pszText = "Process Name";
SendDlgItemMessage(hwndDlg, ListViewId, LVM_INSERTCOLUMN, 0, (LPARAM )&Column);
Column.pszText = "Memory Usage";
Column.mask |= LVCF_FMT;
Column.fmt = LVCFMT_RIGHT;
SendDlgItemMessage(hwndDlg, ListViewId, LVM_INSERTCOLUMN, 1, (LPARAM )&Column);
CreateWindow("BUTTON",
"Refresh",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
5, 300, 120, 20,
hwndDlg,
(HMENU)(ButtonId),
InstanceHandle,
0);
SendDlgItemMessage(hwndDlg, ButtonId, WM_SETFONT, (WPARAM )GetStockObject(DEFAULT_GUI_FONT), FALSE);
SendDlgItemMessage(hwndDlg, ListViewId, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
UpdateList(hwndDlg);
}
break;
case WM_COMMAND:
if(HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ButtonId)
{
UpdateList(hwndDlg);
}
break;
}
return DefWindowProc(hwndDlg, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
InstanceHandle = hInstance;
INITCOMMONCONTROLSEX cc;
memset(&cc, 0, sizeof(cc));
cc.dwSize = sizeof(cc);
cc.dwICC = 0xFFFFFFFF;
InitCommonControlsEx(&cc);
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = DialogProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
wc.lpszClassName = "WhateverClass";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if(!RegisterClass(&wc))
return FALSE;
HWND WindowHandle = CreateWindow("WhateverClass",
"ProcessMon",
WS_MINIMIZEBOX | WS_VISIBLE | WS_CLIPSIBLINGS |
WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_CAPTION | WS_BORDER | WS_SYSMENU,
100, 100, 300, 360,
NULL,
NULL,
InstanceHandle,
0);
MSG Msg;
while(GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
{
if(!IsDialogMessage(WindowHandle, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return 0;
}