Charles Wilson wrote:
Oops. Left a debugging fragment in there. Use the attached version instead of the previous one.
/* poor man's wincap */ OSVERSIONINFO version; memset (&version, 0, sizeof version); version.dwOSVersionInfoSize = sizeof version; GetVersionEx (&version); get_console_window_avail = 0; if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) { if (version.dwMajorVersion == 5) { // w2k or wxp get_console_window_avail = 1; } } get_console_window_avail = 0;
^^^^^^ oops.
--Chuck
#include <stdio.h> #include <math.h> #define _WIN32_WINNT 0x0500 #include <windows.h> int has_console(void); HWND GetConsoleWindow2(void); int main (int argc, char * argv[]) { int console = -1; console = has_console(); if (console) { printf("This is a console application"); } else { MessageBox(NULL, "This is a GUI application", "MessageBox", MB_ICONSTOP); } return ( 0 ); } int has_console(void) { int retval = -1; int get_console_window_avail; /* poor man's wincap */ OSVERSIONINFO version; memset (&version, 0, sizeof version); version.dwOSVersionInfoSize = sizeof version; GetVersionEx (&version); get_console_window_avail = 0; if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) { if (version.dwMajorVersion == 5) { // w2k or wxp get_console_window_avail = 1; } } if (get_console_window_avail) { if (GetConsoleWindow() == NULL) { retval = 0; } else { retval = 1; } } else { if (GetConsoleWindow2() == NULL) { retval = 0; } else { retval = 1; } } return ( retval ); } HWND GetConsoleWindow2(void) { char *letdig = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; char consoleTitle[1024]; char testTitle[1024]; char randTitle[25]; HWND retval; double a, b; int index; int n; if (! GetConsoleTitle(consoleTitle, 1023)) return NULL; for (n=0; n < 20; n++) { a = (((double) rand()) / (((double) RAND_MAX)+1)); // 0 <= a < 1 index = (int) floor(36.0 * a); // 0 <= index <= 35 randTitle[n] = letdig[index]; } randTitle[20] = '\0'; // Temporarily change the title of this console to something unique if (! SetConsoleTitle( randTitle )) return NULL; while( 1 ) { // wait until the console title has really changed if (! GetConsoleTitle(testTitle, 1023)) { SetConsoleTitle(consoleTitle); // try to clean up return NULL; } if (strncmp(randTitle, testTitle, 1023) == 0) break; Sleep(50); // use Win32 Sleep() to wait 50ms } retval = FindWindow (0, randTitle); SetConsoleTitle( consoleTitle ); return retval; }