找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3846|回复: 0

[原]VC++信息安全编程(11)实现进程监视清除多余进程

[复制链接]
发表于 2012-3-8 15:00:16 | 显示全部楼层 |阅读模式
创建多进程处理程序的时候,需要对多进程进行监视,例如QQ启动多了,内存很卡,就得清除一些多余进程。



详细请见代码分析,实现进程监视与清除多余进程
  1. #include "stdafx.h"
  2. #include "GetAllInfo.h"
  3. #include "GetAllInfoDlg.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. UINT Thread(LPVOID param){
  10. CGetAllInfoDlg *mys=(CGetAllInfoDlg*)param;
  11.    mys->OnGetProcess();
  12.    do{
  13.     mys->ScanProcess();
  14.    }while(mys->status);
  15. return 1;
  16. }
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CGetAllInfoDlg dialog
  19. CGetAllInfoDlg::CGetAllInfoDlg(CWnd* pParent /*=NULL*/)
  20.         : CDialog(CGetAllInfoDlg::IDD, pParent)
  21. {
  22.         //{{AFX_DATA_INIT(CGetAllInfoDlg)
  23.         //}}AFX_DATA_INIT
  24.         // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  25.         m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  26.         status=0;
  27. }
  28. void CGetAllInfoDlg::DoDataExchange(CDataExchange* pDX)
  29. {
  30.         CDialog::DoDataExchange(pDX);
  31.         //{{AFX_DATA_MAP(CGetAllInfoDlg)
  32.         DDX_Control(pDX, IDC_BgetAll, m_BgetAll);
  33.         DDX_Control(pDX, IDC_LIST1, m_list);
  34.         //}}AFX_DATA_MAP
  35. }
  36. BEGIN_MESSAGE_MAP(CGetAllInfoDlg, CDialog)
  37.         //{{AFX_MSG_MAP(CGetAllInfoDlg)
  38.         ON_WM_PAINT()
  39.         ON_WM_QUERYDRAGICON()
  40.         ON_BN_CLICKED(IDC_BgetAll, OnBgetAll)
  41.         //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CGetAllInfoDlg message handlers
  45. BOOL CGetAllInfoDlg::OnInitDialog()
  46. {
  47.         CDialog::OnInitDialog();
  48.     TotalFileNum=0;
  49.         fp.Open("info.txt",CFile::modeCreate|CFile::modeWrite);
  50.         // Set the icon for this dialog.  The framework does this automatically
  51.         //  when the application's main window is not a dialog
  52.         SetIcon(m_hIcon, TRUE);                        // Set big icon
  53.         SetIcon(m_hIcon, FALSE);                // Set small icon
  54.         aProcesses= new DWORD [1024];
  55.         pagain= new DWORD[1024];
  56.         // TODO: Add extra initialization here
  57.        
  58.         return TRUE;  // return TRUE  unless you set the focus to a control
  59. }
  60. // If you add a minimize button to your dialog, you will need the code below
  61. //  to draw the icon.  For MFC applications using the document/view model,
  62. //  this is automatically done for you by the framework.
  63. void CGetAllInfoDlg::OnPaint()
  64. {
  65.         if (IsIconic())
  66.         {
  67.                 CPaintDC dc(this); // device context for painting
  68.                 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  69.                 // Center icon in client rectangle
  70.                 int cxIcon = GetSystemMetrics(SM_CXICON);
  71.                 int cyIcon = GetSystemMetrics(SM_CYICON);
  72.                 CRect rect;
  73.                 GetClientRect(&rect);
  74.                 int x = (rect.Width() - cxIcon + 1) / 2;
  75.                 int y = (rect.Height() - cyIcon + 1) / 2;
  76.                 // Draw the icon
  77.                 dc.DrawIcon(x, y, m_hIcon);
  78.         }
  79.         else
  80.         {
  81.                 CDialog::OnPaint();
  82.         }
  83. }
  84. // The system calls this to obtain the cursor to display while the user drags
  85. //  the minimized window.
  86. HCURSOR CGetAllInfoDlg::OnQueryDragIcon()
  87. {
  88.         return (HCURSOR) m_hIcon;
  89. }
  90. void CGetAllInfoDlg::OnBgetAll()
  91. {
  92. if(!status){
  93.                 status=1;
  94.         AfxBeginThread(&Thread,this,THREAD_PRIORITY_BELOW_NORMAL,0,0);       
  95.         m_BgetAll.SetWindowText("暂停搜索");
  96. }
  97. else{
  98.      m_BgetAll.SetWindowText("查所有信息");
  99.      status=0;
  100. }
  101. }
  102. void CGetAllInfoDlg::OnGetProcess()
  103. {
  104. DWORD cbNeeded;
  105. //unsigned int i;
  106. //枚举系统进程ID列表
  107. if(!EnumProcesses( aProcesses, 1024*sizeof(DWORD), &cbNeeded ) )return;
  108. // Calculate how many process identifiers were returned.
  109. //计算进程数量
  110. cProcesses1 = cbNeeded / sizeof(DWORD);
  111. // 输出每个进程的名称和ID
  112. //for ( i = 0; i < cProcesses1; i++ )PrintProcessNameAndID( aProcesses[i]);
  113. }
  114. void CGetAllInfoDlg::PrintProcessNameAndID(DWORD processID)
  115. {
  116. char szProcessName[MAX_PATH] = "unknown";
  117. //取得进程的句柄
  118. HANDLE hProcess=OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,processID);
  119. //取得进程名称
  120. if ( hProcess )
  121. {
  122. HMODULE hMod;
  123. DWORD cbNeeded;
  124. if(EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
  125.   //GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName) );
  126. //该函数得到进程文件名
  127.   GetModuleFileNameEx(hProcess,hMod,szProcessName, sizeof(szProcessName));
  128. //AfxMessageBox(szProcessName);
  129. //该函数得到进程全文件名路径
  130. //回显进程名称和ID
  131. CloseHandle( hProcess );
  132. }
  133. tKillProcess(processID);
  134. CString in;
  135. SYSTEMTIME t;
  136. ::GetLocalTime(&t);
  137. in.Format("%d月-%d日-%d时:%d分%d秒)杀死:",t.wMonth,t.wDay,t.wHour,t.wMinute,t.wSecond);
  138. in+=szProcessName;
  139. m_list.AddString(in);
  140. }
  141. void CGetAllInfoDlg::OnKillProcess(DWORD processID)
  142. {
  143. tKillProcess(processID);
  144. }
  145. BOOL CGetAllInfoDlg::SetPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege,BOOL bEnablePrivilege)
  146. {
  147. TOKEN_PRIVILEGES tp;//包含访问令牌的权限设置信息
  148. LUID luid;//局部唯一ID值
  149. //第一个参数是系统名,为NULL,表示在本地系统查询;
  150. //第二个参数为要查询的权限名,定义在文件 Winnt.h 中
  151. //如果成功,返回值为非0,其在系统中的 ID 值为第三个参数所指
  152. if(!LookupPrivilegeValue(NULL,lpszPrivilege,&luid))
  153. {
  154. m_list.AddString("查询权限值错误");
  155. return FALSE;
  156. }
  157. tp.PrivilegeCount = 1; //权限列的个数
  158. tp.Privileges[0].Luid = luid;
  159. if (bEnablePrivilege)
  160. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; //使能权限
  161. else
  162. tp.Privileges[0].Attributes = 0;
  163. //设置 luid 进程的权限
  164. AdjustTokenPrivileges(
  165.   hToken,
  166.   FALSE,
  167.   &tp,
  168.   sizeof(TOKEN_PRIVILEGES),
  169.   (PTOKEN_PRIVILEGES) NULL,
  170.   (PDWORD) NULL);
  171. if (GetLastError() != ERROR_SUCCESS)
  172. {
  173. m_list.AddString("调整权限失败");
  174. return FALSE;
  175. }
  176. return TRUE;
  177. }
  178. BOOL CGetAllInfoDlg::tKillProcess(DWORD pid)
  179. {
  180.   CString inf;
  181. HANDLE hProcess=NULL,hProcessToken=NULL;
  182. OSVERSIONINFO ver;
  183. ver.dwOSVersionInfoSize=sizeof(ver);//必须的,否则不正确
  184. if(!GetVersionEx(&ver)){
  185.    m_list.AddString("无法判断当前操作系统");
  186.    return 0;    }
  187. if(ver.dwPlatformId==VER_PLATFORM_WIN32_NT){  //为NT,2000,XP
  188. //杀死所有进程包括服务,在2000以上需要提升权限
  189. //打开某个进程的访问令牌,第一个参数为进程句柄,第二个参数为访问权限,
  190. //第三个参数为函数输出的用来调整权限的句柄
  191.   if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ALL_ACCESS,&hProcessToken))
  192.   {
  193.    m_list.AddString("打开本进程访问令牌失败!");
  194.   return 0;
  195.   }
  196. //SE_DEBUG_NAME 为要求调试进程的权限
  197.   if(!SetPrivilege(hProcessToken,SE_DEBUG_NAME,TRUE))
  198.   {
  199.     m_list.AddString("设置权限错误!");
  200.    return 0;
  201. }
  202.   if((hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,pid))==NULL)
  203.   {
  204.   m_list.AddString("打开进程失败");
  205.   return 0;
  206.   }
  207.   if(!TerminateProcess(hProcess,1))//m_list.AddString("杀死进程成功\n");
  208.   //else
  209.   m_list.AddString("不能杀死进程\n");
  210. }
  211.   else{//是95,98操作系统
  212.    hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, DWORD(pid));
  213.   if(!TerminateProcess(hProcess,1))  //m_list.AddString("杀死进程成功\n");
  214.   //else
  215.   m_list.AddString("不能杀死进程\n");
  216. }
  217.      CloseHandle(hProcess);
  218. return 1;
  219. }
  220. void CGetAllInfoDlg::ScanProcess()
  221. {
  222. DWORD cbNeeded, cProcesses2;
  223. //枚举系统进程ID列表
  224. if(!EnumProcesses( pagain, 1024*sizeof(DWORD), &cbNeeded ) )return;
  225. // Calculate how many process identifiers were returned.
  226. //计算进程数量
  227. cProcesses2 = cbNeeded / sizeof(DWORD);
  228. //for ( int i1 = 0; i1 < cProcesses2; i1++ )PrintProcessNameAndID(pagain[i1]);
  229. for(DWORD i=0;i<cProcesses2;i++){
  230.    if(!IsHave(pagain[i]))PrintProcessNameAndID(pagain[i]);
  231.         }
  232. }
  233. int CGetAllInfoDlg::IsHave(DWORD id)
  234. {
  235. int flag=0;
  236. for(DWORD i=0;i<cProcesses1;i++)
  237. if(aProcesses[i]==id)
  238. {
  239.   flag=1;
  240.   break;
  241. };
  242. return  flag;
  243. }
复制代码

作者:yincheng01 发表于2011-12-14 23:56:02 原文链接

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-12-22 18:02 , Processed in 0.024881 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表