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个\n", i);
47     }
48 
49     if (p != NULL)
50     {
51         MessageBoxA(0, "QQ运行中", "提示", 0);
52     }
53     else
54     {
55         MessageBoxA(0, "不在运行", "提示", 0);
56     }
57 
58 }

 

posted @ 2018-01-25 19:21  喵小喵~  阅读(988)  评论(0编辑  收藏  举报