每天都学习一点东西,自己慢慢累积就会进步。。今天来看看电脑进程的查看、进程结束、关机等功能的实现,先上图:
关机功能(XP及以上系统可能由于安全性不适用)
首先我们要查看自己电脑的版本信息,如果不是windows或者是不符合要求的windows版本,那么这些功能没有办法实现了,所以要在
OnInitDialog()初始化函数中获得当前系统版本信息:
// 得到当前Windows版本
OSVERSIONINFOEX WinVersion;
ZeroMemory(&WinVersion, sizeof(OSVERSIONINFOEX));
WinVersion.dwOSVersionInfoSize =sizeof(OSVERSIONINFOEX);
DWORD dwMajorVersion,dwMinorVersion,dwPlatformId;
BOOL flag=GetVersionEx((OSVERSIONINFO *) &WinVersion);
if(flag)
{
dwMajorVersion=WinVersion.dwMajorVersion;
dwMinorVersion=WinVersion.dwMinorVersion;
dwPlatformId=WinVersion.dwPlatformId;
if(dwMajorVersion==3)
{
m_nWinVersion=WIN_NT351;
m_staWinVersion.SetWindowText(_T("Windows NT 3.51"));
}
else if(dwMajorVersion==4 && dwMinorVersion==0 && dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
{
m_nWinVersion=WIN_95;
m_staWinVersion.SetWindowText(_T("Windows 95"));
}
else if(dwMajorVersion==4 && dwMinorVersion==0 && dwPlatformId==VER_PLATFORM_WIN32_NT)
{
m_nWinVersion=WIN_NT40;
m_staWinVersion.SetWindowText(_T("Windows NT 4.0"));
}
else if(dwMajorVersion==4 && dwMinorVersion==10)
{
m_nWinVersion=WIN_98;
m_staWinVersion.SetWindowText(_T("Windows 98"));
}
else if(dwMajorVersion==4 && dwMinorVersion==90)
{
m_nWinVersion=WIN_ME;
m_staWinVersion.SetWindowText(_T("Windows Me"));
}
else if(dwMajorVersion==5 && dwMinorVersion==0)
{
m_nWinVersion=WIN_2000;
m_staWinVersion.SetWindowText(_T("Windows 2000"));
}
else if(dwMajorVersion==5 && dwMinorVersion==1)
{
m_nWinVersion=WIN_XP;
m_staWinVersion.SetWindowText(_T("Windows XP"));
}
else
{
m_nWinVersion=WIN_UNKNOWN;
m_staWinVersion.SetWindowText(_T("未知系统"));
}
}
// 如果不是以上Windows版本,则退出程序,否则刷新进程列表
if(m_nWinVersion==WIN_UNKNOWN)
{
AfxMessageBox(_T("未知操作系统!"),MB_OK|MB_ICONINFORMATION);
::PostMessage(this->m_hWnd,WM_QUIT,0,0);
}
else
OnRefresh();
如果版本信息符合要求,下面就要读入进程:
// “刷新”按钮响应函数
void CProcessDlg::OnRefresh()
{
// TODO: Add your control notification handler code here
m_wndList.ResetContent();
HANDLE hSnapshot;
// 创建系统快照
hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 pe;
Process32First(hSnapshot,&pe);
do
{
int index=m_wndList.AddString(pe.szExeFile);
m_wndList.SetItemData(index,pe.th32ProcessID);
}
while(Process32Next(hSnapshot,&pe));
CloseHandle(hSnapshot);
GetDlgItem(IDC_STOPPROCESS)->EnableWindow(FALSE);
}
// “结束任务”按钮响应函数
void CProcessDlg::OnStopprocess()
{
// TODO: Add your control notification handler code here
int index = m_wndList.GetCurSel();
DWORD data=m_wndList.GetItemData(index);
HANDLE hProcess;
// 打开进程
hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,data);
if(hProcess)
{
if(!TerminateProcess(hProcess,0))
{
CString strError;
strError.Format("错误号:%d",GetLastError());
AfxMessageBox(strError,MB_OK|MB_ICONINFORMATION,NULL);
}
}
else
{
CString strError;
strError.Format("错误号:%d",GetLastError());
if(GetLastError()==ERROR_ACCESS_DENIED)
strError=_T("拒绝访问!")+strError;
AfxMessageBox(strError,MB_OK|MB_ICONINFORMATION,NULL);
}
Sleep(300);
OnRefresh();
}
// “关机”按钮响应函数
void CProcessDlg::OnShutdown()
{
// TODO: Add your control notification handler code here
if(AfxMessageBox("确定关机么? ",MB_YESNO|MB_ICONQUESTION)==IDYES)
if(!ExitWindowsEx(
EWX_SHUTDOWN
,0))
{
CString strError;
strError.Format("错误码:%d。",GetLastError());
if(m_nWinVersion==WIN_95||m_nWinVersion==WIN_98||m_nWinVersion==WIN_ME)
strError=_T("关机失败!")+strError;
else
strError=_T("您没有关机权限!")+strError;
AfxMessageBox(strError,MB_OK|MB_ICONINFORMATION);
}
}
// List控件框中当选项改变时消息处理函数
void CProcessDlg::OnSelchangeList()
{
if(!GetDlgItem(IDC_STOPPROCESS)->IsWindowEnabled())
GetDlgItem(IDC_STOPPROCESS)->EnableWindow();
}