I'm trying to write to stdout from a DLL compiled using Visual C++ 6.0.
This line works correctly:
printf("testing\n");
This line produces an access violation:
fprintf(stdout,"testing\n");
This DLL is being called from a Windows application over which I have very little control. The original application was *not* compiled as a console application, but I have forced it to activate a console window by using editbin with the /SUBSYSTEM:CONSOLE option to modify appropriate bytes in the exeheader.
As evidenced by successful execution of printf, the console window does in fact exist and is able to receive output written to its standard output file stream. However, it appears that the stdout FILE* pointer is probably not being properly initialized in this scenario since fprintf to stdout fails.
I actually need to use fwrite instead of fprintf to write something substantially more complex, but fwrite fails in exactly the same manner and almost certainly for the same underlying reason as fprintf.
How can I correct this issue? Do I need to somehow initialize stdout? Or is there someway I can create a different FILE* pointer to the same stream that is being used by printf?
By the way, due to restrictions imposed by the executable that this DLL is linked to, this DLL can't use C++ code. It is limited to C.