How to call popen () with a path name containing spaces under Windows in C / C ++?

I am trying to call popen () using mingw as follows:

#define RUN_COMMAND "\"C:\\Program Files\\CA\\BrightStor ARCserve Backup\\ca_qmgr.exe\" \"-list\""
int main() {
    outputPointer = popen(RUN_COMMAND, "r");
    ...
}

But I can’t make it work. I think this is a nightmare of a nightmare ...

+3
source share
5 answers

Take a look at What is equivalent to posix popen in win32 api . I gave up trying to use _popen()under Windows for a long time. A clean WIN32 solution is much more reliable.

If mingw passes your parameters in sh -cinstead cmd.exe /c, then you probably want to change these backslashes to slashes. Something like the following should do the trick.

"\"C:/Program Files/CA/BrightStor ARCserve Backup/ca_qmgr.exe\" -list"
+4
source

, popen , . "C:/Program Files" C:/Program Files, "Ab cd.exe" "ef gh" ab cd" "ef gh. , , . ""ab cd.exe" "ef gh"", "Ab cd.exe" "ef gh" .

+2

. (, Windows, 100%).

popen , , cmd.exe /c <here>.

cmd.exe /c Windows .

, nope:

C:\>cmd /c "foo bar"
'foo' is not recognized as an internal or external command,
operable program or batch file.

circumflex?

C:\>cmd /c ^"foo bar^"
'foo' is not recognized [...]

?

C:\>cmd /c foo^ bar
'foo' is not recognized [...]

?

C:\>cmd /c foo" "bar
'foo" "bar' is not recognized as an internal or external command,
operable program or batch file.

! ! foo bar? :

C:\>copy con "foo bar.bat"
echo i am foo bar
^Z
        1 file(s) copied.

C:\>cmd /c foo" "bar

C:\>echo i am foo bar
i am foo bar

! !

, Visual Studio 2008:

#include <stdio.h>

int main()
{
    FILE *in = _popen("C:\\Program\" \"Files\\Internet\" \"Explorer\\iexplore.exe", "r");

    if (in) {
        char line[200];
        printf("successfully opened!\n");
        if (fgets(line, 200, in))
            fputs(line, stdout);
        _pclose(in);
    }

    return 0;
}

Internet Explorer, fgets ( , ).

MinGW, Microsoft C; , , , Windows MinGW.

:

. , . ; , , .

, , . , , - C:\Program Files\Bob so-called "editor"\bedit.exe.

+1

Kaz. , , , _popen , .

char cwd[MAX_PATH];
_getcwd(cwd, MAX_PATH);

if (_chdir("C:\\Program Files\\CA\\BrightStor ARCserve Backup") >= 0)
{
    outputPointer = _popen("ca_qmgr.exe \"-list\"", "r");
    _chdir(cwd);
}
+1

man popen:

The command argument is a pointer to a null-terminated string containing the shell command line. This command is passed to / bin / sh using the -c flag; interpretation, if any, performed by the shell.

So you want to avoid the line shfor parsing. You do not need to avoid the argument -list, and you can try to avoid spaces instead of quoting the entire program name. I do not have a Windows system for testing, but I would suggest

#define RUN_COMMAND "C:\\Program Files\\CA\\BrightStor\\ ARCserve\\ Backup\\ca_qmgr.exe -list"
int main() {
    outputPointer = popen(RUN_COMMAND, "r");
    ...
}
-1
source

Source: https://habr.com/ru/post/1719927/


All Articles