MFC中进程查看,结束进程,关机等实现

每天都学习一点东西,自己慢慢累积就会进步。。今天来看看电脑进程的查看、进程结束、关机等功能的实现,先上图:
MFC中进程查看,结束进程,关机等实现
                 关机功能(XP及以上系统可能由于安全性不适用)
MFC中进程查看,结束进程,关机等实现

首先我们要查看自己电脑的版本信息,如果不是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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值