列举当前运行的所有进程

类别:VC语言 点击:0 评论:0 推荐:

void CEmnuprocessDlg::ListProcess()
{
           HANDLE  hProcessSnap=NULL;
           PROCESSENTRY32 pe32;
          //获得句柄
          hProcessSnap = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS,0);
          if( hProcessSnap == (HANDLE)-1)
         {      
                 //printf("\nCreateToolhelp32Snapshot() failed:%d",GetLastError ());
                 AfxMessageBox ("CreateToolhelp32Snapshot() failed!");
          }

         pe32.dwSize = sizeof(PROCESSENTRY32);
         //列据进程名称
         if( Process32First (hProcessSnap,&pe32) )
        {      
               do{    
                        m_ctrlListProcesses.AddString ( pe32.szExeFile  );
                   }
                  while(Process32Next (hProcessSnap,&pe32));
         }
       else
       {       

                AfxMessageBox ("Process32finst() failed!");

                // printf("\nProcess32finst() failed:%d",GetLastError ()); 
         }
                CloseHandle (hProcessSnap);
        }

注:在StdAfx加入所用到的头文件tlhelp32.h

本文地址:http://com.8s8s.com/it/it757.htm