首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >禁止system()输出

禁止system()输出
EN

Stack Overflow用户
提问于 2013-11-08 02:15:09
回答 3查看 10.6K关注 0票数 3

首先,我主要从事C#和.Net开发,所以如果这是一个愚蠢的问题,那就别问我了。

我正在实现一个Ericcson开源项目,将图像转换为另一种格式。问题是在转换时,输出到控制台的过程如下所示。

代码语言:javascript
复制
1 file(s) copied.

我需要抑制弹出的这个对话框。我只想在没有输出的情况下执行系统命令。我想我已经隔离了导致这个问题的代码区域。

代码语言:javascript
复制
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的引用遍历图像时,许多控制台窗口在屏幕上闪现。需要阻止这一切的发生。

非常感谢您的帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-08 02:22:57

尝试执行以下操作:

代码语言:javascript
复制
strcat( str, " > nul" )         // for Windows or
//strcat( str, " > /dev/null" ) // for Unix

system( str )

如果它没有帮助,那么这可能会有帮助:

代码语言:javascript
复制
#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()调用替换为system_no_output()调用。

票数 9
EN

Stack Overflow用户

发布于 2020-04-11 01:57:15

要完全禁止输出,请重定向stdoutstderr

代码语言:javascript
复制
system("command >nul 2>nul");
票数 4
EN

Stack Overflow用户

发布于 2013-11-08 02:34:37

如果您使用的是C#,请查看here。C#中使用的类是ProcessStartInfo。在链接中的示例中,查看OpenWithStartInfo成员函数,它将最小化控制台。

至于在C++中实现这一点,请看一下spawn系列的函数here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19843557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档