popen函数

yiyefangzhou24 2013-07-21 11:33:10
我用到这样一个函数来在c中调用cmd命令

int execmd(char* cmd,char* result) {
char buffer[128]; //定义缓冲区
FILE* pipe = _popen(cmd, "r"); //打开管道,并执行命令
if (!pipe)
return 0; //返回0表示运行失败

while(!feof(pipe)) {
if(fgets(buffer, 128, pipe)){ //将管道输出到result中
strcat(result,buffer);
}
}
_pclose(pipe); //关闭管道
return 1; //返回1表示运行成功
}

这个函数在控制台应用程序中成功执行,在MFC中FILE* pipe = _popen(cmd, "r"); 的pipe返回为NULL请问为啥?不能在MFC中使用?
...全文
344 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2013-07-22
  • 打赏
  • 举报
回复
while(!feof(pipe)) { if(fgets(buffer, 128, pipe)){ //将管道输出到result中 strcat(result,buffer); } } 典型编码错误。
yiyefangzhou24 2013-07-22
  • 打赏
  • 举报
回复
还有个问题,我在TreeControl中 m_tree.InsertItem(tmp,0,0,hRoot[0],NULL);//hRoot[0]是根节点 为什么显示的时候要点一下跟节点,根节点前面才出现+号,否则没有+ ps:这时候子节点是已经插入成功的
yiyefangzhou24 2013-07-22
  • 打赏
  • 举报
回复
我换了一种方法可以在MFC中执行,具体原因没时间探究了,大牛们指点
int CIPCexportDlg::execmd(char *cmd, char *result)
{
	SECURITY_ATTRIBUTES sa;
	HANDLE hWrite,hRead;
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	DWORD dwReadBytes;

	sa.bInheritHandle = TRUE;
	sa.lpSecurityDescriptor = NULL;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	
	if(!CreatePipe(&hRead,&hWrite,&sa,0))
	{
		MessageBox("Create Pipe Error!");
		return 0;
	}


	si.cb = sizeof(STARTUPINFO);
	GetStartupInfo(&si);
	si.hStdError = hWrite;
	si.hStdOutput = hWrite;
	si.wShowWindow = SW_HIDE;
	si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;

	char totalcmd[1024]="C:\\WINDOWS\\system32\\cmd.exe /c ";
	strcat(totalcmd,cmd);
	if(!CreateProcess(NULL,totalcmd,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
	{
		MessageBox("Create Process Error!");
		return 0;
	}
	CloseHandle(hWrite);

	ReadFile(hRead,result,4096,&dwReadBytes,NULL);

	return 1;
}
图灵狗 2013-07-22
  • 打赏
  • 举报
回复
可以先试试以下的例子,不行的话用_wpopen函数替换试试看:

// crt_popen.c
/* This program uses _popen and _pclose to receive a 
 * stream of text from a system process.
 */

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

int main( void )
{

   char   psBuffer[128];
   FILE   *pPipe;

        /* Run DIR so that it writes its output to a pipe. Open this
         * pipe with read text attribute so that we can read it 
         * like a text file. 
         */

   if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL )
      exit( 1 );

   /* Read pipe until end of file, or an error occurs. */

   while(fgets(psBuffer, 128, pPipe))
   {
      printf(psBuffer);
   }


   /* Close pipe and print return value of pPipe. */
   if (feof( pPipe))
   {
     printf( "\nProcess returned %d\n", _pclose( pPipe ) );
   }
   else
   {
     printf( "Error: Failed to read the pipe to the end.\n");
   }
}
引用 楼主 yiyefangzhou24 的回复:
我用到这样一个函数来在c中调用cmd命令

int execmd(char* cmd,char* result) {
    char buffer[128];                         //定义缓冲区                        
    FILE* pipe = _popen(cmd, "r");            //打开管道,并执行命令 
    if (!pipe)
          return 0;                      //返回0表示运行失败 
        
    while(!feof(pipe)) {
    if(fgets(buffer, 128, pipe)){             //将管道输出到result中 
            strcat(result,buffer);
        }
    }
    _pclose(pipe);                            //关闭管道 
    return 1;                                 //返回1表示运行成功 
}
这个函数在控制台应用程序中成功执行,在MFC中FILE* pipe = _popen(cmd, "r"); 的pipe返回为NULL请问为啥?不能在MFC中使用?
yiyefangzhou24 2013-07-22
  • 打赏
  • 举报
回复
引用 5 楼 qq120848369 的回复:
while(!feof(pipe)) { if(fgets(buffer, 128, pipe)){ //将管道输出到result中 strcat(result,buffer); } } 典型编码错误。
??哪里?
fthislife 2013-07-21
  • 打赏
  • 举报
回复
用GetLastError()查看返回码是啥

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧