Menu

Logging the output of 7z fails when using the command line through CreateProcess()

Help
Damian K
2016-12-02
2016-12-02
  • Damian K

    Damian K - 2016-12-02

    I am trying to use the 7z command line in order to accomplish various tasks. For one of these it's important for the program to inspect the archive's content before extracting anything from it. In order to find an interface between my program and 7z I use the '>' sign to redirect standard output into a file. Unfortunately the following code does not work:

    std::wstring archivepath = archive->getPath(); // archive path
    std::wstring logfile = L"db\\logfile.log"; // log file path
    
    SevenZipInterface::executeSevenZip(L" l \"" + archivepath + L"\" > " + logfile);
    

    In this example I want to redirect the contents of the archive into a log file and later open it to investigate the archives content. Unfortunately 7z seems to not redirect the output to the file and the operation doesn't even succeed. This is what is printed on the console:

    Obviously the error here is that the standard output is redirected to the console, not to the file specified. The second picture appears when I remove the '>' part:

    std::wstring archivepath = archive->getPath(); // archive path
    
    SevenZipInterface::executeSevenZip(L" l \"" + archivepath + L"\");
    

    Now 7z finishes the operation successfully, but there is no log file I could inspect to analyze the output.

    How do I enforce logging the output to a file?

    The interface function:

    bool executeSevenZip(std::wstring command)
    {
        DWORD dwExitCode=0;
        STARTUPINFO info= {sizeof(info)};
        PROCESS_INFORMATION processInfo;
        std::wstring commandLine = L"\"7zip\\7z.exe\" " + command;
    
        if (CreateProcessW(NULL, 
                                       &commandLine[0], 
                                       NULL, NULL, TRUE, 
                                       0, NULL, NULL, 
                                       &info, 
                                       &processInfo))
        {
            WaitForSingleObject(processInfo.hProcess, INFINITE);
    
            GetExitCodeProcess(processInfo.hProcess, &dwExitCode);
    
            CloseHandle(processInfo.hProcess);
            CloseHandle(processInfo.hThread);
    
            SetLastError(dwExitCode);
    
            return true;
        }
    
        return false;
    }
    
     
  • Igor Pavlov

    Igor Pavlov - 2016-12-02

    google / stackoverflow:
    "How do I redirect output to a file with CreateProcess?"

     
  • Damian K

    Damian K - 2016-12-02

    I see my mistake. Thanks for the quick reply!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.