Qt, Visual Studio and the Windows Subsystem

By | 11/03/2009

Note: This is an entry I posted to my old blog, I am just keeping it here for history’s sake. I am also not sure how much this is still relevant or if there are better ways to do this nowadays.

As commonly known, when building windows applications one usually uses WinMain() instead of main() since the win32 subsystem requires it as entry point (for non-unicode apps). Also commonly known is that the QApplication object requires the argc and argv parameters which are passed to main(). That’s perfectly fine, after all you can always use main() as entry point. The downside of using main() and the console subsystem on windows is that there’s always a console window opened which then opens your applications main window. To avoid this, one must use the previously mentioned win32 subsystem and the WinMain() entry point – which has neither argc nor argv as parameters. As a solution, I came up with the following code (thanks to chip for improvement suggestions):

// QT includes
#ifdef Q_WS_WIN
#include "windows.h"
int main(int argc, char **argv);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                                 LPSTR lpCmdLine, int nCmdShow)
  return main(__argc, __argv);

int main(int argc, char** argv)
  QApplication theApp(argc, argv);

  return theApp.exec();

Alternate solution (thanks to Theory for pointing me at it): Add the following post-build step:

editbin /subsystem:windows $(OutDir)\$(TargetFileName)

Leave a Reply

Your email address will not be published. Required fields are marked *