mfc的相关链接问题
SetTimer(g_hDlg, 1, 4000, NULL);//定时器消息响应
void OnTimer()
{
HWND hCtrl = GetDlgItem(g_hDlg, IDC_CHK_AUTO);
UINT nCheck = SendMessage(hCtrl, BM_GETCHECK, 0, 0);
if(nCheck)
{
GetProcessList();
}
}
//取得当前系统进程列表
BOOL GetProcessList()
{
//清空列表控件
ListView_DeleteAllItems(g_hList);
//初始缓冲区数组大小
int nLenArray = 32;
//缓冲区大小的字节数
int nLenBytes = nLenArray * sizeof(DWORD);;
//申请缓冲区
DWORD *pdwPID = new DWORD[nLenArray];
DWORD dwNeeded = 0;
//获取进程列表
BOOL bRet = EnumProcesses(pdwPID, nLenBytes, &dwNeeded);
//如果缓冲区不够大,增大缓冲区并重新得到进程列表
while(bRet && dwNeeded == nLenBytes)
{
delete []pdwPID;
pdwPID = NULL;
nLenArray += 32;
nLenBytes = nLenArray * sizeof(DWORD);
pdwPID = new DWORD[nLenArray];
bRet = EnumProcesses(pdwPID, nLenBytes, &dwNeeded);
}
//得到进程个数
int nCount = dwNeeded / sizeof(DWORD);
//将进程数显示到界面上
TCHAR szCount[128];
_stprintf(szCount, _T("进程数:%d"), nCount);
SetDlgItemText(g_hDlg, IDC_EDT_MSG, szCount);
//取得DEBUG权限
SetDebugPrivilege(TRUE);
//循环获取每个进程信息
DWORD dwPID;
for(int i=0; i<nCount; i++)
{
dwPID = pdwPID[i];
AddProcessRow(dwPID);
}
//释放缓冲区
delete []pdwPID;
//关闭DEBUG权限
SetDebugPrivilege(FALSE);
//重新排序
ListView_SortItems(g_hList, ListSortFunc, g_nSortCol < 0 ? 0 : g_nSortCol);
return TRUE;
}
//取得进程详细信息并显示到列表控件中
void AddProcessRow(DWORD dwPID)
{
HANDLE hProcess;
HMODULE hModule;
DWORD dwPriority;
TCHAR szPID[32] = {0};
TCHAR szPriority[32] = {0};
TCHAR szBaseName[MAX_PATH] = {0};
TCHAR szFileName[MAX_PATH] = {0};
int nIndex;
//向列表控件插入一行
LVITEM item = {0};
item.mask = LVIF_TEXT | LVIF_PARAM;
item.iItem = ListView_GetItemCount(g_hList);
item.lParam = dwPID;
_stprintf(szPID, _T("%d"), dwPID);
item.pszText = szPID;
nIndex = ListView_InsertItem(g_hList, &item);
if(nIndex < 0)
DebugBreak();
//打开进程
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwPID);
if(hProcess != NULL)
{
//得到进程的第一个模块
BOOL bRet;
DWORD dwNeeded;
bRet = EnumProcessModules(hProcess, &hModule, sizeof(HMODULE), &dwNeeded);
if(bRet)
{
//取得进程优先级
dwPriority = GetPriorityClass (hProcess);
_stprintf(szPriority, _T("%d"), dwPriority);
//取得模块映像名称
GetModuleBaseName(hProcess, hModule, szBaseName, MAX_PATH);
//取得模块文件名
GetModuleFileNameEx(hProcess, hModule, szFileName, MAX_PATH);
//显示到列表控件
ListView_SetItemText(g_hList, nIndex, 1, szBaseName);
ListView_SetItemText(g_hList, nIndex, 2, szPriority);
ListView_SetItemText(g_hList, nIndex, 3, szFileName);
}
//关闭打开的进程句柄
CloseHandle(hProcess);
}
}
//打开或关闭当前进程的DEBUG权限
BOOL SetDebugPrivilege(BOOL bEnable)
{
HANDLE hToken;
BOOL bRet;
//获得进程访问令牌句柄
bRet = OpenProcessToken(GetCurrentProcess(),
TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken);
if(bRet)
{
TOKEN_PRIVILEGES TokenPrivileges;
TokenPrivileges.PrivilegeCount = 1;
//取得DEBUG权限的LUID
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &TokenPrivileges.Privileges[0].Luid);
//根据参数,打开或关闭相应权限
TokenPrivileges.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0;
//调整权限
bRet = AdjustTokenPrivileges(hToken,FALSE,&TokenPrivileges,sizeof(TOKEN_PRIVILEGES),NULL,NULL);
//关闭句柄
CloseHandle(hToken);
}
return bRet;
}