Compdigitec Labs

« | Home | »

Console-free gtkmm applications in Visual Studio 2008

By admin | December 6, 2009

When compiling Gtkmm programs on Microsoft Windows using Visual C++, a black console box pops up when you run the program. However, setting the application subsystem to Windows will cause the program to complain about WinMain and other problems. Here is a versatile solution from the Gtkmm mailing list to get Gtkmm to work on Windows transparently without breaking interoperability with other platforms, such as Linux, though some modifications were required to make it work.

Insert the following code before your main() and after your header #includes:

#ifdef WIN32
	#include <windows.h>
#endif

using namespace std;
using namespace Gtk;

int main(int argc, char* argv[]);

#ifdef WIN32

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	// check windows version
         DWORD dwVersion = GetVersion();

         if (!(dwVersion < 0x80000000))
         {
                 MessageBox(0, _T("This application requires Windows 2000/XP or above!"), _T("Fatal Error"), MB_OK);
                 return -1;
         }

         return main(__argc, __argv);
}

#endif

Here is a sample Gtkmm application without console boxes on Windows:

#include <gtkmm.h>
#include <iostream>
#ifdef WIN32
	#include <windows.h>
#endif

using namespace std;
using namespace Gtk;

int main(int argc, char* argv[]);

#ifdef WIN32

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	// check windows version
         DWORD dwVersion = GetVersion();

         if (!(dwVersion < 0x80000000))
         {
                 MessageBox(0, _T("This application requires Windows 2000/XP or above!"), _T("Fatal Error"), MB_OK);
                 return -1;
         }

         return main(__argc, __argv);
}

#endif

int main(int argc, char* argv[])
{
	Gtk::Main kit(argc, argv);
	Gtk::Window window;
	Gtk::Main::run(window);

	return 0;
}

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Windows | 5 Comments »

5 Responses to “Console-free gtkmm applications in Visual Studio 2008”

  1. Rory Says:
    March 20th, 2010 at 11:39 am

    I am still getting a console window displayed even in the example.

    Visual studio is forcing me to use #include “stdafx.h”. Could this be the differance?

  2. admin Says:
    March 20th, 2010 at 6:18 pm

    @Rory:

    It doesn’t hurt to try commenting out the #include statement. Also, be sure you have selected the Windows subsystem, and not the Console subsystem.

  3. Rory Says:
    March 21st, 2010 at 8:28 am

    Ok thanks. With the windows subsytem all works well. Cheers

  4. Rory Says:
    March 21st, 2010 at 8:46 am

    Do you have any experiance using a glade comstructed GUI with visual studio?

  5. PK Says:
    July 9th, 2012 at 1:03 am

    Kudos!

    This is exactly what I was looking for and works perfectly for me, thank you very much.

Comments