VC使用TerminateProcess结束进程实例

  int KillProcess(LPCSTR pszClassName, LPCSTR

  pszWindowTitle)

  {

  HANDLE hProcessHandle;

  ULONG nProcessID;

  HWND TheWindow;

  TheWindow = ::FindWindow( NULL, pszWindowTitle );

  ::GetWindowThreadProcessId( TheWindow, &nProcessID );

  hProcessHandle = ::OpenProcess( PROCESS_TERMINATE, FALSE,

  nProcessID );

  return ::TerminateProcess( hProcessHandle, 4 );

  }

  void CZxDlg::OnOK()

  {

  // TODO: Add extra validation here

  // CDialog::OnOK();

  KillProcess("radmin.exe","Radmin Viewer")

  }

  void CZxDlg::OnButton1()

  {

  HANDLE hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

  if(hSnapShot == 0)

  return;

  PROCESSENTRY32 thePE;

  thePE.dwSize = sizeof(PROCESSENTRY32);

  //遍历正在运行的第一个系统进程

  bool Status = Process32First(hSnapShot,&thePE);

  bool bHaveFlag = false;

  DWORD ProcessID = 0;

  while(Status)

  {

  //遍历正在运行的下一个系统进程

  Status = Process32Next(hSnapShot,&thePE);

  CString jincheng ="qq.exe";

  jincheng.MakeLower();

  //找到相应的进程 **.exe

  if(0 == _tcscmp(thePE.szExeFile,_T(jincheng)))

  {

  bHaveFlag = true;

  ProcessID = thePE.th32ProcessID;

  //结束指定的进程 ProcessID

  if(!TerminateProcess(OpenProcess (PROCESS_TERMINATE||PROCESS_QUERY_INFORMATION,false,ProcessID),0))

  {

  AfxMessageBox("无法终止指定的进程!",MB_ICONWARNING||MB_OK);

  }

  break;

  }

  }

  CloseHandle(hSnapShot);

  }