简体   繁体   中英

DLL output to stderr doesn't work when redirected from parent

I've redirected stdout and stderr in a dll with:

dup2(fileno(myLogHandle), fileno(stderr));
dup2(fileno(myLogHandle), fileno(stdout));

which works fine when tested from the dll.

std::cout << "cout\n";
std::cerr << "cerr\n";
fprintf(stdout,"stdout\n");
fprintf(stderr,"stderr\n");
fputs("fputs stdout\n",stdout);
fputs("fputs stderr\n",stderr);
system("echo system stdout");
system("echo system stderr 1>&2");

Gives the following output in the log file:

cout
cerr
stdout
stderr
fputs stdout
fputs stderr
system stdout
system stderr

But when another DLL (opened with LoadLibrary) is using stderr I don't get anything in the log file.

Do I miss something obvious?

That's entirely reasonable. You are not redirecting the standard handles of the process, only the standard output and error devices of your DLL's runtime. The other DLL would appear to be using its own instance of the runtime.

Fundamentally you are doing this the wrong way. You should redirect the standard output and standard error at the process level, when you create the process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM