取shell脚本执行的结果

bool OnShellCall(const char *shell_cmd, CString& result)
{
    FILE* pipe = ::popen(shell_cmd, "r");
    if (!pipe)
    {
        return false;
    }
 
    char buffer[256] = {0};
    result = "";
    char * s_read = NULL;
    while(!feof(pipe))
    {
        //s_read return null 或读取完毕
        s_read = fgets(buffer, sizeof(buffer), pipe);
        if (NULL!=s_read)
        {
            result += buffer;
 
        }
        else
        {
            break;
        }
    }
 
    pclose(pipe);
    s_read = NULL;
    return TRUE;
}
View Code

 

posted @ 2018-07-17 16:51  匠心十年ly  阅读(1694)  评论(0编辑  收藏  举报