+/* Direct from the horse's mouth: Microsoft KB article Q124103 */
+static HWND
+GetConsoleHwnd (void)
+{
+ HWND hwndFound; /* this is what is returned to the caller */
+ char pszNewWindowTitle[KLUDGE_BUFSIZE]; /* contains fabricated WindowTitle */
+ char pszOldWindowTitle[KLUDGE_BUFSIZE]; /* contains original WindowTitle */
+
+ /* fetch current window title */
+
+ GetConsoleTitle(pszOldWindowTitle, KLUDGE_BUFSIZE);
+
+ /* format a "unique" NewWindowTitle */
+
+ wsprintf(pszNewWindowTitle,"%d/%d",
+ GetTickCount(),
+ GetCurrentProcessId());
+
+ /* change current window title */
+
+ SetConsoleTitle(pszNewWindowTitle);
+
+ /* ensure window title has been updated */
+
+ Sleep(40);
+
+ /* look for NewWindowTitle */
+
+ hwndFound=FindWindow(NULL, pszNewWindowTitle);
+
+ /* restore original window title */
+
+ SetConsoleTitle(pszOldWindowTitle);
+
+ return(hwndFound);
+}
+
+HWND
+msw_get_console_hwnd (void)
+{
+ if (!msw_console_hwnd)
+ msw_console_hwnd = GetConsoleHwnd ();
+ return msw_console_hwnd;
+}
+
+int
+msw_ensure_console_allocated (void)
+{
+ HWND fgwin = GetForegroundWindow ();
+ /* stupid mswin api won't let you create the console window
+ hidden! creating it changes the focus! fuck me! */
+ if (AllocConsole ())
+ {
+ SetForegroundWindow (fgwin);
+ return 1;
+ }
+ return 0;
+}
+
+static Lisp_Object
+mswindows_canonicalize_console_connection (Lisp_Object connection,
+ Error_behavior errb)
+{
+ /* Do not allow more than one mswindows device, by explicitly
+ requiring that CONNECTION is nil, the only allowed connection in
+ Windows. */
+ if (!NILP (connection))
+ {
+ if (ERRB_EQ (errb, ERROR_ME))
+ signal_simple_error
+ ("Invalid (non-nil) connection for mswindows device/console",
+ connection);
+ else
+ return Qunbound;
+ }
+
+ return Qnil;
+}
+
+static Lisp_Object
+mswindows_canonicalize_device_connection (Lisp_Object connection,
+ Error_behavior errb)
+{
+ return mswindows_canonicalize_console_connection (connection, errb);
+}