I have implemented a DLL (C++, VisualStudio, Windows), which extends function of an another program (further - main program
).
I don’t know internal architecture of the main program
, except it is GUI-program and doesn't use console windows itself.
DLL creates console window and uses it to produce output for debugging aims (std::cout/wcout).
The interaction of the DLL and main program
occurs through some exported functions in DLL. The interaction works good, but in several cases the main programs
crashes. In several cases it occurs when I simply close console window pressing "x".
The question is: do you see any potential risks of using console window by the DLL, and should I close the console window when DLL functions finishes working ? May be you see an obvious breach of discipline of working with the console in such scheme ?
The code for creation and closing console window:
bool CreateConsole() {
HWND ConsoleWindowHandle = GetConsoleWindow();
if (ConsoleWindowHandle != NULL)
// console ia already created
return true;
BOOL res = AllocConsole();
if (res == 0) {
DWORD LastError = GetLastError();
return false;
}
freopen_s(&ConsoleStream, "CONOUT$", "w", stdout);
setlocale(LC_ALL, "...");
return true;
}
void CloseConsole() {
BOOL ok = FreeConsole();
ConsoleWindowHandle = 0;
}
You don't need GetConsoleWindow()
, you can call AllocConsole()
unconditionally and it will fail if there is already a console present. Also, don't call FreeConsole()
if AllocConsole()
fails.
In any case, there is a better option, one that is actually designed for debugging output - DebugOutputString()
. If you run your app inside of a debugger, the messages will be displayed in the debugger's output log. If you run your app outside of a debugger, you can see the messages using SysInternals DebugView. Either way, there is no need for a console window at all.
If you need std::ostream
access for your debug output, you can easily write a class (or find a 3rd party one) that implements std::basic_streambuf
to cache the output and send it to DebugOutputString()
, and then you can attach that class to a std::ostream
object.