zoukankan      html  css  js  c++  java
  • 42.管道,cmd执行指令写到管道中

     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include <string.h>
     5 #include <Windows.h>
     6 
     7 int exeshell(char *cmd, char *result)//传递指令,返回结果
     8 {
     9     FILE *pf = _popen(cmd, "r");//打开一个管道,用管道执行cmd,结果存放在管道中
    10     if (pf == NULL)
    11     {
    12         printf("创建管道失败");
    13         return 0;
    14     }
    15     else
    16     {
    17         while (!feof(pf))//如果没有到文件末尾
    18         {
    19             char str[256] = { 0 };
    20             if (fgets(str, 256, pf))//返回值是读到多少个字符
    21             {
    22                 strcat(result, str);//拼接字符串
    23             }
    24         }
    25         _pclose(pf);//关闭管道
    26 
    27         return 1;
    28     }
    29 }
    30 
    31 void main()
    32 {
    33     char result[1024 * 10] = { 0 };//缓冲区
    34 
    35     if (exeshell("tasklist", result)!=0)
    36     {
    37         printf("%s", result);
    38     }
    39 
    40     char *p = strstr(result, "QQ.exe");
    41 
    42     int i = 0;
    43     for (char *p = strstr(result, "QQ.exe"); p != NULL; p = strstr(p+1,"QQ.exe"))
    44     {
    45         i++;
    46         printf("QQ运行了%d个
    ", i);
    47     }
    48 
    49     if (p != NULL)
    50     {
    51         MessageBoxA(0, "QQ运行中", "提示", 0);
    52     }
    53     else
    54     {
    55         MessageBoxA(0, "不在运行", "提示", 0);
    56     }
    57 
    58 }
  • 相关阅读:
    【HCIE-RS_TAC诊断5-2】
    【HCIE-RS_TAC诊断5-1】
    【HCIE-RS_TAC诊断4】
    【HCIE-RS_TAC诊断3】
    【HCIE-RS_TAC诊断2】
    华为ICT大赛辅导——双AC主备双链路备份
    shell 函数与内置变量
    CF505E Mr. Kitayuta vs. Bamboos
    CF559E Gerald and Path
    CF538H Summer Dichotomy
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8352813.html
Copyright © 2011-2022 走看看