I have an application, in which I use a console to see some of the values being output. Now some of the requirements have changed, and I do not need the console during runtime anymore.
I tried to change that, by toggling the /SUBSYSTEM parameter, found under Project Properties->Linker->System->Subsystem from Console to Windows, as I'd done the same for an earlier thing, and it had worked.
On this occasion, it gives me an Unhandled Exception, in mfc110u.dll, as the object cannot be instantiated.
Why does this exception occur, and how else can I turn off the console with the running program? I'm using VS2012 as the dev environment.
If you don't want a console, declare a winmain. This is the non-unicode version
#include <windows.h>
#include <iostream>
#include <cstdio>
int main (int, char**);
// If we just start with main, we will always get a console window
int WINAPI WinMain (
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
int argc = __argc;
char** argv = __argv;
#ifdef DEBUG
// If we are running in debug mode, open a console window
AllocConsole();
freopen("conin$", "r", stdin);
freopen("conout$", "w", stdout);
freopen("conout$", "w", stderr);
#endif
return main (argc, argv);
}
int main (
int argc,
char** argv
)
{
MessageBox(NULL, "Whoo hoo", "It Works!!!", MB_OK);
return 0;
}
Related
I need a hand because I can't get the SetConsoleCursorPosition () function to work, I made a dll project and then I allocated the console with its main functions (cout and cin), but I don't know how to make that function work as well , in the sense that I do not go to the line that I have set, as the code ignored that instruction
void consolerefresh()
{
static const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = { (SHORT)0, (SHORT)10 };
SetConsoleCursorPosition(hConsole, coord);
for (int i = 0; i < (sizeof(last) / sizeof(last[0])); i++)
{
if (last[i] != 2) {
cout << last[i] << endl;
}
}
}
My allocConsole code:
void createconsole()
{
AllocConsole();
FILE* fDummy;
freopen_s(&fDummy, "CONOUT$", "w", stdout);
freopen_s(&fDummy, "CONOUT$", "w", stderr);
freopen_s(&fDummy, "CONIN$", "r", stdin);
std::cout.clear();
std::clog.clear();
std::cerr.clear();
std::cin.clear();
}
EDIT: I fixed the error in the code but it still doesn't work.
Per SetConsoleCursorPosition, the first argument must be "a handle to the console screen buffer". Per GetStdHandle, the handle to the active console screen buffer is returned by STD_OUTPUT_HANDLE, not STD_INPUT_HANDLE which is the handle to the console input buffer.
Using the correct handle will get SetConsoleCursorPosition to work as expected.
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // instead of STD_INPUT_HANDLE
[ EDIT ] The following was verified to work in VS 2019 using a default wizard-generated Win app with the following code added around wWinMain.
//... wizard generated code
#include <stdio.h>
#include <iostream>
void consolerefresh()
{
static const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = { 5, 5 };
SetConsoleCursorPosition(hConsole, coord);
std::cout << "at (5, 5)" << std::endl;
}
void createconsole()
{
AllocConsole();
FILE* fDummy;
freopen_s(&fDummy, "CONOUT$", "w", stdout);
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
createconsole();
consolerefresh();
UNREFERENCED_PARAMETER(hPrevInstance);
//... rest of wizard generated code
Running the program creates a console window and displays the following.
I am using the wxWidget framework. When I set System/Subsystem: Console (/SUBSYSTEM:CONSOLE) then my program will start but will show both a GUI and a console. This is how it looks: https://i.stack.imgur.com/G83PR.png
When I change System/Subsystem: Windows (/SUBSYSTEM:WINDOWS) then it show errors:
LNK2019 unresolved external symbol _WinMain#16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
This is my code:
int main(int argc, char** argv) {
Gui_AutoPokemon* gui = new Gui_AutoPokemon();
wxApp::SetInstance(gui);
mainArgc = argc;
mainArgv = argv;
return wxEntry(argc, argv);
}
How can I hide the console window?
GUI Windows programs use a different entry point function than console. You're right to use /SUBSYSTEM:WINDOWS, but then you need to change to using WinMain:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR cmdLine, int nCmdShow) {
....
Then use the appropriate wxEntry overload:
return wxEntry(hInstance, NULL, cmdLine, nCmdShow);
I suggest calling FreeConsole(). It detaches the console from any window. As an example for my application:
LPCTSTR Process = ("Test game");
LPCTSTR windowName = Process(" Text");
system("color 0a");
SetConsoleTitle(windowName);
Can anyone please make this posiblle?
To change the current console title you can use the SetConsoleTitle WinAPI function:
#include <iostream>
#include <Windows.h>
int main(){
wchar_t newTitle[255] = L"My New Console Title";
SetConsoleTitle(newTitle); // set the current console title
return 0;
}
I have an winmain application that is executed from cmd.exe and prints output to it. I atach to the cmd.exe using AttachConsole(ATTACH_PARENT_PROCESS). After application is executed and output is printed to cmd.exe command line prompt is not displayed and it looks like application is stil running(while it is already closed). Before closing my application I release the console using FreeConsole().
#include <iostream>
#include <fstream>
#include <windows.h>
int wWinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nCmdShow
)
{
AttachConsole(ATTACH_PARENT_PROCESS);
std::wofstream console_out("CONOUT$");
std::wcout.rdbuf(console_out.rdbuf());
std::wcout << L"\nSome Output" << std::endl;
FreeConsole();
return 0;
}
Current result:
My goal:
How should I make prompt C:New folder> appear after myapp.exe has printed its output and is closed.
In case the question has not been answered yet (after such a long time), it is required to simulate the actual pressing of the 'Enter' key in the console window by sending (or, preferably, posting) the corresponding WM_KEYDOWN message to the console window, i.e.
after
std::wcout << L"\nSome Output" << std::endl;
and before calling
FreeConsole(),
insert the following:
HWND hWndCon_ = ::GetConsoleWindow();
if( hWndCon_ ) {
::PostMessage( hWndCon_, WM_KEYDOWN, VK_RETURN, 0 );
}
or simply
::PostMessage( ::GetConsoleWindow(), WM_KEYDOWN, VK_RETURN, 0 );
this code is compiled with -mwindows under gcc , there is no winapi error message.
#include <windows.h>
#include <stdio.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int nCmdShow) {
AllocConsole();
printf("%s\n", "sample text");
return 0;
}
Result is that console is black empty, no text, no error message.
Use:
freopen("CONOUT$", "wb", stdout);
to reopen the stdout after you have created the console. If you plan on using it for input too, then you need:
freopen("CONIN$", "rb", stdin);
and stderr may need opening too:
freopen("CONOUT$", "wb", stderr);