抑制system()输出 [英] Suppress system() output

查看:186
本文介绍了抑制system()输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我做的大多是C#,.Net开发,所以去容易我,如果这是一个愚蠢的问题。

First off, I do mostly C#, .Net development so go easy on me if this is a stupid question.

我正在实现一个Ericcson开源项目,将图像转换为另一种格式。问题是,在转换输出到控制台发生如下...

I am implementing an Ericcson open source project to convert an image to another format. The problem is that on conversion an output to a console happens as follows...

1 file(s) copied.

我需要禁止弹出此对话框。我只想执行没有输出的系统命令。我想我已经分离出造成这种代码的区域。

I need to suppress this dialog that pops up. I just want to execute the system command with no output. I think I have isolated the area of the code causing this.

void writeOutputFile(char *dstfile, uint8* img, uint8* alphaimg, int width, int height)

{
    char str[300];
if(format!=ETC2PACKAGE_R_NO_MIPMAPS&&format!=ETC2PACKAGE_RG_NO_MIPMAPS) 
{
    fWritePPM("tmp.ppm",width,height,img,8,false);
    //PRINTF("Saved file tmp.ppm \n\n");
}
else if(format==ETC2PACKAGE_RG_NO_MIPMAPS) 
{
    fWritePPM("tmp.ppm",width,height,img,16,false);
}
if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS)
    fWritePGM("alphaout.pgm",width,height,alphaimg,false,8);
if(format==ETC2PACKAGE_R_NO_MIPMAPS)
    fWritePGM("alphaout.pgm",width,height,alphaimg,false,16);

// Delete destination file if it exists
if(fileExist(dstfile))
{
    sprintf(str, "del %s\n",dstfile);   
    system(str);
}

int q = find_pos_of_extension(dstfile);
if(!strcmp(&dstfile[q],".ppm")&&format!=ETC2PACKAGE_R_NO_MIPMAPS) 
{
    // Already a .ppm file. Just rename. 
    sprintf(str,"move tmp.ppm %s\n",dstfile);
    //PRINTF("Renaming destination file to %s\n",dstfile);
}
else
{
    // Converting from .ppm to other file format
    // 
    // Use your favorite command line image converter program,
    // for instance Image Magick. Just make sure the syntax can
    // be written as below:
    // 
    // C:\imconv source.ppm dest.jpg
    //
    if(format==ETC2PACKAGE_RGBA_NO_MIPMAPS||format==ETC2PACKAGE_RGBA1_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA_NO_MIPMAPS||format==ETC2PACKAGE_sRGBA1_NO_MIPMAPS) 
    {
        // Somewhere after version 6.7.1-2 of ImageMagick the following command gives the wrong result due to a bug. 
        // sprintf(str,"composite -compose CopyOpacity alphaout.pgm tmp.ppm %s\n",dstfile);
        // Instead we read the file and write a tga.

        //PRINTF("Converting destination file from .ppm/.pgm to %s with alpha\n",dstfile);
        int rw, rh;
        unsigned char *pixelsRGB;
        unsigned char *pixelsA;
        fReadPPM("tmp.ppm", rw, rh, pixelsRGB, 8);
        fReadPGM("alphaout.pgm", rw, rh, pixelsA, 8);
        fWriteTGAfromRGBandA(dstfile, rw, rh, pixelsRGB, pixelsA, true);
        free(pixelsRGB);
        free(pixelsA);
        sprintf(str,""); // Nothing to execute.
    }
    else if(format==ETC2PACKAGE_R_NO_MIPMAPS) 
    {
        sprintf(str,"imconv alphaout.pgm %s\n",dstfile);
        //PRINTF("Converting destination file from .pgm to %s\n",dstfile);
    }
    else 
    {
        sprintf(str,"imconv tmp.ppm %s\n",dstfile);
        //PRINTF("Converting destination file from .ppm to %s\n",dstfile);
    }
}
// Execute system call
system(str);

free(img);
if(alphaimg!=NULL)
    free(alphaimg);

}

点关于如何抑制弹出的控制台。当我们通过dll的引用迭代图像时,许多控制台窗口在屏幕上闪烁。需要阻止此情况发生。

I am lost at this point about how to suppress the console that pops up. As we iterate through images via a reference to the dll, many many console windows flash on the screen. Need to stop this from happening.

帮助非常感谢。

推荐答案

尝试:

strcat( str, " > nul" )         // for Windows or
//strcat( str, " > /dev/null" ) // for Unix

system( str )

如果它不起作用,那么这可能会有帮助:

If it doesn't help then this may help:

#include <string>
#include <ShellAPI.h>

int system_no_output( std::string command )
{
    command.insert( 0, "/C " );

    SHELLEXECUTEINFOA ShExecInfo = {0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = NULL;
    ShExecInfo.lpFile = "cmd.exe";        
    ShExecInfo.lpParameters = command.c_str();   
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_HIDE;
    ShExecInfo.hInstApp = NULL;

    if( ShellExecuteExA( &ShExecInfo ) == FALSE )
        return -1;

    WaitForSingleObject( ShExecInfo.hProcess, INFINITE );

    DWORD rv;
    GetExitCodeProcess( ShExecInfo.hProcess, &rv );
    CloseHandle( ShExecInfo.hProcess );

    return rv;
}

并替换所有 system c $ c>调用 system_no_output()

这篇关于抑制system()输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆