使用C程序验证chrome是否正在运行

时间:2017-06-28 15:02:48

标签: c

我想在C中开发一个做一些处理的函数,但它取决于chrome Process的状态。所以如果chrome正在运行,我应该终止进程以使程序正确执行

If(chrome is running )
system("taskkill /IM chrome.exe /F");
Operation 1
operation 2
..........

那么如何在c中转换行(chrome正在运行) 我用的是windows

2 个答案:

答案 0 :(得分:1)

是的,我们可以。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
chrome_is_running() {
  char buf[BUFSIZ];
  int r = system("tasklist /FI \"IMAGENAME eq chrome.exe\" > .out");
  FILE *fp = fopen(".out", "r");
  int found = 0;
  while (fgets(buf, sizeof(buf), fp)) {
    if (strncasecmp(buf, "chrome.exe", 10) == 0) {
      found = 1;
      break;
    }
  }
  unlink(".out");
  return found;
}

int
main(int argc, char* argv[]) {
  if (chrome_is_running())
    system("taskkill /F /IM chrome.exe");
  return 0;
}

答案 1 :(得分:0)

好的,这个怎么样?

#include <windows.h>
#include <stdio.h>

int
chrome_is_running() {
  return FindWindow("Chrome_WidgetWin_1", NULL) != NULL;
}

int
main(int argc, char* argv[]) {
  if (chrome_is_running())
    system("taskkill /F /IM chrome.exe");
  return 0;
}