当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++进阶与实例

如何关闭除自己程序外的所有其他应用程序

      获得当前激活的进程:
1:包含头文件tlhelp32.h
2:.cpp如下:

    ListView2->Items->Clear();
    TListItem *mItem;
    AnsiString ExeFile;
    Pointer pt,pt2;
    unsigned int s;
    DWord size,size2;
    HANDLE snapshot;
    PROCESSENTRY32 processinfo;
    processinfo.dwSize = sizeof(processinfo);
    snapshot =
    CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    if (snapshot==NULL) return;
    bool flag = Process32First (snapshot,&processinfo);
    while (flag)
      {
      mItem=ListView2->Items->Add();
      ExeFile=AnsiString(processinfo.szExeFile);
      mItem->Caption=ExeFile;
      mItem->SubItems->Add(IntToStr(int(processinfo.th32ParentProcessID)));
      mItem->SubItems->Add(IntToHex(int(processinfo.th32ProcessID),8).UpperCase());
      size=GetFileVersionInfoSize(ExeFile.c_str(),&size2);
      pt=malloc(size);
      GetFileVersionInfo(ExeFile.c_str(),NULL,size,pt);
      if(VerQueryValue(pt,"\\StringFileInfo\\040904E4\\FileVersion",&pt2,&s))
        mItem->SubItems->Add(PChar(pt2));
      if(VerQueryValue(pt,"\\StringFileInfo\\040904E4\\CompanyName",&pt2,&s))
        mItem->SubItems->Add(PChar(pt2));
      if(VerQueryValue(pt,"\\StringFileInfo\\040904E4\\FileDescription",&pt2,&s))
        mItem->SubItems->Add(PChar(pt2));
      free(pt);
      flag = Process32Next(snapshot,&processinfo);
      }

要杀除一个进程,必须获得该进程的父线成ID(避免仅仅杀除子进程)。

    if (ListView2->SelCount==0)
      {
      MessageBox(Handle,"请首先选择一个进程!","中止进程",MB_OKMB_ICONWARNING);
      return;
      }
    int pPid=StrToInt(ListView2->Selected->SubItems->Strings[0]);
    HANDLE ps = OpenProcess(1,false,pPid);
    if(ps&&TerminateProcess(ps,-9))
      {
      MessageBox(Handle,"成功中止进程!","中止进程",MB_OKMB_ICONINFORMATION);
      }
    else
      MessageBox(Handle,"中止进程失败!","中止进程",MB_OKMB_ICONWARNING);
    ViewProcess();

 

相关内容
赞助商链接