栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++快速开发例子--进程查看器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++快速开发例子--进程查看器

 

#include
#pragma hdrstop

#include
#include "proc.h"
#include "another.h"
//---------------------------------------------------------------------
#pragma resource "*.dfm"
#pragma resource "procv2.res"
TFormMain *FormMain;
//---------------------------------------------------------------------
static int stat=1;
int i=0;
//---------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent *Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------
bool __stdcall EnumProc(HWND hWnd,long)
{
   unsigned long* pPid;   //LPDWORD
   unsigned long result;      //DWORD
   void *hg;                  //HGLOBAL
   unsigned long id;

   if(hWnd==NULL)
      return false;

   hg = GlobalAlloc(GMEM_SHARE,sizeof(unsigned long));
   pPid = (unsigned long *)GlobalLock(hg);

   result = GetWindowThreadProcessId(hWnd,pPid);

   if(result){
      char title[110];
      char className[95];
      char totalStr[256];
      GetClassName(hWnd,className,95);
      GetWindowText(hWnd,title,110);
      id=*pPid;
      ultoa(id,totalStr,10);
      strcat(totalStr,"t");
      if(title){
         strcat(totalStr,title);
         strcat(totalStr,"t");
      }
      strcat(totalStr,className);
      FormMain->ListBox1->Items->Add((AnsiString)totalStr);
   }
   else{
      GlobalUnlock(hg);
      GlobalFree(hg);
      return false;
   }
   GlobalUnlock(hg);
   GlobalFree(hg);
   return true;
}

//---------------------------------------------------------------------
void __fastcall TFormMain::Exit1Click(TObject* )
{
   PostQuitMessage(0);
}
//----------------------------------------------------------------------------
void __fastcall TFormMain::About1Click(TObject* )
{
   Form2->ShowModal();
}
//----------------------------------------------------------------------------
 void __fastcall TFormMain::SpeedButton4Click(TObject* )
{
   long lp=0;
   ListBox1->Enabled=true;
   ListBox1->Clear();
   EnumWindows((WNDENUMPROC)EnumProc,lp);
   SpeedButton2->Enabled=true;
   SpeedButton3->Enabled=true;
}
//----------------------------------------------------------------------------
void __fastcall TFormMain::RadioButton1Click(TObject* Sender)
{
   TRadioButton *rbp =(TRadioButton*) Sender;
   ListBox1->Font->Color=rbp->Font->Color;
}
//----------------------------------------------------------------------------
 void __fastcall TFormMain::New1Click(TObject* )
{
   SpeedButton4Click(0);
}
//----------------------------------------------------------------------------
 void __fastcall TFormMain::Refresh1Click(TObject* )
{
      SpeedButton4Click(0);
}
//----------------------------------------------------------------------------
 void __fastcall TFormMain::SpeedButton3Click(TObject* )
{
      ListBox1->Clear();
      SpeedButton3->Enabled=false;
      SpeedButton1->Enabled=false;
}
//----------------------------------------------------------------------------
 void __fastcall TFormMain::ListBox1Click(TObject* )
{
   SpeedButton1->Enabled=true;
   StatusBar1->SimpleText="Select 'Kill Selected PID' to terminate the process";
}
//----------------------------------------------------------------------------

void __fastcall TFormMain::FormShow(TObject *)
{
   if(stat){
      stat= 0;
      ListBox1->Items->Add((AnsiString)"Click on 'pid' Tool button above");
   }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::Timer1Timer(TObject *)
{
   switch(i){
      case 0:{
         Image1->Picture->Bitmap->LoadFromResourceName(0,(AnsiString)"BITMAP_5");
         Image1->Refresh();
         i=1;
         return;
      }
      case 1:{
         Image1->Picture->Bitmap->LoadFromResourceName(0,(AnsiString)"BITMAP_2");
         Image1->Refresh();
         i=2;
         return;
      }
      case 2:{
         Image1->Picture->Bitmap->LoadFromResourceName(0,(AnsiString)"BITMAP_3");
         Image1->Refresh();
         i=3;
         return;
      }
      case 3:{
         Image1->Picture->Bitmap->LoadFromResourceName(0,(AnsiString)"BITMAP_4");
         Image1->Refresh();
         i=4;
         return;
      }
      case 4:{
         Image1->Picture->Bitmap->LoadFromResourceName(0,(AnsiString)"BITMAP_1");
         Image1->Refresh();
         i=0;
         return;
      }
   }//end switch
}

void __fastcall TFormMain::SpeedButton2Click(TObject *Sender)
{
   SpeedButton4Click(0);
   SpeedButton1->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpeedButton1Click(TObject *Sender)
{
   try {
   AnsiString str;
   char *tmp;
   int i;
   i= ListBox1->ItemIndex;
   if( i != -1){
      AnsiString s;
      tmp = new char[100];
      s=ListBox1->Items->Strings[i];
      strcpy(tmp,(char*)s.c_str());
      tmp=strtok(tmp,"t");
   }
   int id=atoi(tmp);
   delete[] tmp;
   HANDLE ps = OpenProcess(1,false,id);
   if(ps){
      if(!TerminateProcess(ps,-9)){
         ShowMessage((AnsiString)"Could not end process specified!");
      }
      else{
         ShowMessage((AnsiString)"Process successfully terminated!");
      }
   }
   else{
      ShowMessage((AnsiString)"Could not open process requested!");
   }
   }
   catch(Exception &e)
   {
    ShowMessage("Please select a PID");
   }
}
//---------------------------------------------------------------------

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/951917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号