PInf *Inf;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 for(int i=0; i < ListBox1->Count; i++)
 {
  CloseHandle(((PInf *)(ListBox1->Items->Objects[i]))->H);
  delete ListBox1->Items->Objects[i];
 }
 ListBox1->Clear();
 SpeedButton1->Down = false;
 HANDLE HS = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 PROCESSENTRY32 P;
 P.dwSize = sizeof(PROCESSENTRY32);
 DWORD V;
 if(Process32First(HS, &P))
 {
  do
  {
   Inf = new PInf;
   Inf->Usage = P.cntUsage;
   Inf->Threads = P.cntThreads;
   Inf->PriClassBase = P.pcPriClassBase;
   Inf->ID = P.th32ProcessID;
   Inf->Suspended = false;
   V = GetProcessVersion(P.th32ProcessID);
   Inf->V1 = HIWORD(V);
   Inf->V2 = LOWORD(V);
   Inf->H = OpenProcess(PROCESS_ALL_ACCESS, true, P.th32ProcessID);
   ListBox1->AddItem(P.szExeFile, (TObject *)Inf);
  }
  while(Process32Next(HS, &P));
 }
 CloseHandle(HS);
 ListBox1->ItemIndex = 0;
 ListBox1Click(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
 for(int i=0; i < ListBox1->Count; i++)
 {
  CloseHandle(((PInf *)(ListBox1->Items->Objects[i]))->H);
  delete ListBox1->Items->Objects[i];
 }
 ListBox1->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
 Edit1->Clear();
 Edit2->Clear();
 Edit3->Clear();
 Edit4->Clear();
 Edit5->Clear();
 Edit6->Clear();
 Edit7->Clear();
 Inf = (PInf *)(ListBox1->Items->Objects[ListBox1->ItemIndex]);
 Edit5->Text = Inf->Usage;
 Edit4->Text = Inf->Threads;
 Edit6->Text = Inf->PriClassBase;
 Edit8->Text = IntToStr(Inf->V1) + '.' + IntToStr(Inf->V2);
 if(Inf->H)
 {
  FILETIME lpCreationTime, lpExitTime, lpKernelTime, lpUserTime;
  SYSTEMTIME T, T0;
  GetProcessTimes(Inf->H, &lpCreationTime, &lpExitTime, &lpKernelTime, &lpUserTime);
  FileTimeToSystemTime(&lpCreationTime, &T);
  TTimeZoneInformation lpTimeZoneInformation;
  System::TDateTime Tloc = SystemTimeToDateTime(T);

  if (GetTimeZoneInformation(&lpTimeZoneInformation)
                                 == TIME_ZONE_ID_STANDARD)
   Tloc = Tloc + (const double)(-lpTimeZoneInformation.Bias -
        lpTimeZoneInformation.StandardBias) / (60 * 24);
  else
   Tloc = Tloc + (const double)(-lpTimeZoneInformation.Bias -
        lpTimeZoneInformation.DaylightBias) / (60 * 24);

  Edit1->Text = DateTimeToStr(Tloc);
  Edit7->Text = TimeToStr(Now() - Tloc);

  FileTimeToSystemTime(&lpKernelTime, &T);
  Edit2->Text = TimeToStr(SystemTimeToDateTime(T));
  FileTimeToSystemTime(&lpUserTime, &T);
  Edit3->Text = TimeToStr(SystemTimeToDateTime(T));
  SpeedButton1->Down = Inf->Suspended;
  if(SpeedButton1->Down)
   SpeedButton1->Caption = "";
  else SpeedButton1->Caption = "";
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 //  
 FormCreate(Sender);
 ListBox1->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 //  
 TerminateProcess(Inf->H, 5);
 WaitForSingleObject(Inf->H, 1000);
 Button1Click(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
 //      
 DWORD ID = Inf->ID;
 Inf->Suspended = SpeedButton1->Down;
 HANDLE HS = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
 THREADENTRY32 P;
 P.dwSize = sizeof(THREADENTRY32);
 HANDLE hThread;
 if(Thread32First(HS, &P))
 {
  do
   if (P.th32OwnerProcessID == ID)
   {
    if (hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, P.th32ThreadID))
      if(SpeedButton1->Down)
      {
       SuspendThread(hThread);
       SpeedButton1->Caption = "";
       CloseHandle(hThread);
      }
      else
      {
       ResumeThread(hThread);
       SpeedButton1->Caption = "";
       CloseHandle(hThread);
      }
   }
  while(Thread32Next(HS, &P));
 }
 CloseHandle(HS);
}
//---------------------------------------------------------------------------

