0

I am not very familiar with c++. When I compile, and run the below code, a console window is also opened with the message box. I need to know why, and how to avoid that console window. I've checked for duplicate questions, but haven't yet found a proper answer.

#include <Windows.h>
int main()
{
    char* title = "Message box title";
    char* message = "I am working";

    MessageBox(NULL, message, title, MB_OK | MB_ICONEXCLAMATION );
    return 0;
}
IInspectable
  • 46,945
  • 8
  • 85
  • 181
Scevola
  • 45
  • 9
  • Chose to create a WinForms application in 1st place. – πάντα ῥεῖ Aug 15 '15 at 12:04
  • 1
    You are creating a console application, instead you should create a win32 application. – john Aug 15 '15 at 12:07
  • i was hoping for better comments like "Throw these header files and call these methods instead of that". And whoever down voted my question, please suggest a better way to ask the question instead of down voting. – Scevola Aug 15 '15 at 12:29
  • @Scevola Ask [better questions](http://stackoverflow.com/help/how-to-ask) then. – πάντα ῥεῖ Aug 15 '15 at 12:41
  • I voted to reopen this question, as the answers to the proposed duplicate do not really address the question. The answer to **this** question is: Write an application that runs in the *WINDOWS* subsystem, not the *CONSOLE* subsystem. – IInspectable Aug 15 '15 at 20:01
  • @πάνταῥεῖ: You cannot create a WinForms application using C++ (C++/CLI is **not** C++, and .NET is not the Windows API). – IInspectable Aug 15 '15 at 20:03
  • @πάνταῥεῖ: You were the **first** to vote to close this question, and provided the proposed duplicate (which isn't). Regardless, I wasn't even addressing this in my previous comment, just saying, that maybe you should not vote on questions that are outside your areas of expertise (proposing a .NET solution to a question that is clearly using the Windows API is quite revealing). – IInspectable Aug 15 '15 at 20:14
  • @IInspectable I didn't close as a duplicate, community did (since I have a dupe hammer available, this would look different in case). The question is certainly something already answered here though. Question is reopened now, close for the correct dupe or answer please. – πάντα ῥεῖ Aug 15 '15 at 20:15
  • @IInspectable _You were the first to vote to close this question, and provided the proposed duplicate_ No, either I would have closed as dupe immediately, or giving a different close reason. – πάντα ῥεῖ Aug 15 '15 at 20:19
  • @IInspectable: and by a not-so-strange coincidence, that's also the answer to the duplicate. :-) Did you read Rob's existing answer? "configure your project so that it is not a console program". Same as yours. – Harry Johnston Aug 16 '15 at 02:38
  • Here's yet another duplicate: http://stackoverflow.com/questions/6342935/start-a-program-without-a-console-window-in-background – Harry Johnston Aug 16 '15 at 02:53
  • @HarryJohnston: Does the answer also provide instructions on changing the entry point signature? Or the respective linker switches? Or even the correct IDE setting to change (there is not setting called *"GUI"*)? If you still feel, that the existing answer is sufficient, or the existing question is better, go ahead, and close this one. – IInspectable Aug 16 '15 at 02:53
  • @HarryJohnston: That other answer is fairly misleading as well (come on, setting the entry point manually? Are you proposing to screw up the CRT's initialization?). – IInspectable Aug 16 '15 at 02:55
  • @IInspectable: if you didn't like the existing answers to the older question, the proper action to take would have been to post your answer there, not to reopen this question and post your answer here. Oh, well. What's done is done, I don't have the energy to fight about it. (There's probably a dozen duplicates out there, I suppose ideally I'd track them all down and get a moderator to merge them. Right now I can't be bothered.) – Harry Johnston Aug 16 '15 at 03:16

1 Answers1

5

When you create an application for the CONSOLE subsystem (using the /SUBSYSTEM:CONSOLE linker switch), the system allocates a console automatically.

If you don't want the system to allocate a console for your process, specify the /SUBSYSTEM:WINDOWS linker switch. This also requires a different application entry point signature:

int APIENTRY wWinMain( HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPWSTR    lpCmdLine,
                       int       nCmdShow ) {

    const wchar_t* title = L"Message box title";
    const wchar_t* message = L"I am working";

    MessageBoxW( NULL, message, title, MB_OK | MB_ICONEXCLAMATION );
    return 0;
}

When using Visual Studio, you can change the subsytem through the project settings GUI:

enter image description here


Note, that I changed the character encoding to use Unicode. This is the default encoding Windows uses internally. To make the compiler pick up the wWinMain entry point, you need to set the character encoding in the project settings accordingly (General -> Character Set: Use Unicode Character Set).
IInspectable
  • 46,945
  • 8
  • 85
  • 181