LDFLAGS = /nologo /subsystem:console /incremental:no /machine:$(ARCH)
 
 # Debugging flags
-#CFLAGS = /nologo /MDd /W3 /GX /Od /Gm /Zi /I "." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /c
+#CFLAGS = /nologo /MDd /W3 /EHsc /Od /Gm /Zi /I "." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /c
 #LDFLAGS = /nologo /subsystem:console /incremental:yes /debug /machine:$(ARCH)
 
 LD = link
 
                return;
        }
 #ifdef WIN32
-       SetConsoleOutputCP(65001);
-    _setmode(_fileno(stdout),_O_U8TEXT);
-    _setmode(_fileno(stderr),_O_U8TEXT);
-       if (isatty(fileno(stdin))) 
-               _setmode(_fileno(stdin),_O_U8TEXT);
        icharset("UTF-8",0);
        
 #else 
 
                                        char dir[_MAX_DIR];     \
                                        char fname[_MAX_FNAME]; \
                                        char ext[_MAX_EXT];     \
-                                       long handle;
+                                       intptr_t handle;
 
 #else
 #if MSDOS_COMPILER==WIN32C && !defined(_MSC_VER) /* Borland C for Windows */
 
 
 #ifdef WIN32
 static char consoleTitle[256];
-static int  savedConsoleCodePage;
 #endif
 
 extern int     less_is_more;
                }
        }
        GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
-       savedConsoleCodePage = GetConsoleOutputCP();    
 #endif /* WIN32 */
 
        /*
 #endif
 #ifdef WIN32
        SetConsoleTitle(consoleTitle);
-       SetConsoleOutputCP(savedConsoleCodePage);
 #endif
        close_getchr();
        exit(status);
 
 {
 #if MSDOS_COMPILER==WIN32C
        DWORD written;
-       WriteConsole(con_out, text, len, &written, NULL);
+       wchar_t widebuf[1024];
+       wchar_t *wb=widebuf;
+       if (len >= 1024) 
+               wb = (wchar_t *) malloc(len*sizeof(wchar_t));
+       written=MultiByteToWideChar(CP_UTF8,0,text,len,wb,len>1024?len:1024);
+       WriteConsoleW(con_out, wb, written, &written, NULL);
+       if (wb != widebuf) free(wb);
 #else
        char c = text[len];
        text[len] = '\0';