代码之家  ›  专栏  ›  技术社区  ›  Ravi shankar

使用system()命令隐藏上显示的Cmd窗口。

  •  0
  • Ravi shankar  · 技术社区  · 16 年前

    我在C++应用程序中创建了一个用户界面,它使用一个C DLL来操纵EXIF文件。这个C dll使用system()函数打开cmd窗口和一个记事本文件进行编辑,当我们关闭记事本文件时,输入的数据被编辑到exif头注释中。现在我必须隐藏这个cmd窗口,我使用了“start\b”,但是这会关闭cmd窗口,这会导致编辑exif头文件,而不会在记事本文件中输入数据。 此函数的代码如下。

    FILE * file;
    int a;
    char QuotedPath[PATH_MAX+10];
    
    file = fopen(TempFileName, "w");
    if (file == NULL)
     {
        fprintf(stderr, "Can't create file '%s'\n",TempFileName);
        ErrFatal("could not create temporary file");
     }
    fwrite(Comment, CommentSize, 1, file);
    
    fclose(file);
    
    
    fflush(stdout); // So logs are contiguous.
    
     {
        char * Editor;
        Editor = getenv("EDITOR");
        if (Editor == NULL)
         {
           #ifdef _WIN32
             Editor = "notepad";
           #else
             Editor = "vi";
           #endif
        }
    
        if (strlen(Editor) > PATH_MAX) ErrFatal("env too long");
    
        sprintf(QuotedPath, "%s \"%s\"",Editor, TempFileName);
    
        a = system(QuotedPath);
    
    }
    
    if (a != 0)
     {
        char message[50]= "";
        strcpy(message, "Editor failed to launch");
        MessageBoxA(hWnd,message,"Error : ",MB_ICONWARNING);
        // perror("Editor failed to launch");
        exit(-1);
     }
    
    if (hFileOpen != NULL)
     {
        file = fopen(TempFileName, "r");
        if (file == NULL)
         {
            ErrFatal("could not open temp file for read");
         }
    
        // Read the file back in.
        CommentSize = fread(Comment, 1, 999, file);
    
        fclose(file);
    
        unlink(TempFileName);
    
        return CommentSize;
    }
    
    1 回复  |  直到 11 年前
        1
  •  1
  •   Zaid Amir    16 年前

    最好的方法(以我的拙见)是使用shellexecuteEx运行指定的exe,这将返回一个句柄到执行的进程,然后您可以监视和关闭它完成后。

    注意:我认为你应该考虑重新格式化你的代码块