├── README.md ├── mingw-unicode-gui.c └── mingw-unicode.c /README.md: -------------------------------------------------------------------------------- 1 | mingw-unicode-main 2 | ================== 3 | 4 | Note: This should no longer be used as MinGW now has a built-in solution. Add -municode to the command line (and possibly extern "C" to the wmain function). 5 | 6 | Simple wrappers to add wmain and wWinMain support in MinGW 7 | 8 | These wrappers allow for use of wmain / wWinMain in MinGW seamlessly with Unicode (WCHAR), regular (CHAR), or the ability to choose (TCHAR). 9 | 10 | The instructions for using them are in the files. Also take a look at other programs that use them. 11 | -------------------------------------------------------------------------------- /mingw-unicode-gui.c: -------------------------------------------------------------------------------- 1 | // This is for the MinGW compiler which does not support wWinMain. 2 | // It is a wrapper for _tWinMain when _UNICODE is defined (wWinMain). 3 | // 4 | // !! Do not compile this file, but instead include it right before your _tWinMain function like: 5 | // #include "mingw-unicode-gui.c" 6 | // int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { 7 | // 8 | // This wrapper adds ~400 bytes to the program and negligible overhead 9 | 10 | #undef _tWinMain 11 | #ifdef _UNICODE 12 | #define _tWinMain wWinMain 13 | #else 14 | #define _tWinMain WinMain 15 | #endif 16 | 17 | #if defined(__GNUC__) && defined(_UNICODE) 18 | 19 | #define WIN32_LEAN_AND_MEAN 20 | #define WIN32_EXTRA_LEAN 21 | #define NOGDICAPMASKS 22 | #define NOVIRTUALKEYCODES 23 | #define NOWINMESSAGES 24 | #define NOWINSTYLES 25 | #define NOSYSMETRICS 26 | #define NOMENUS 27 | #define NOICONS 28 | #define NOKEYSTATES 29 | #define NOSYSCOMMANDS 30 | #define NORASTEROPS 31 | #define NOSHOWWINDOW 32 | #define OEMRESOURCE 33 | #define NOATOM 34 | #define NOCLIPBOARD 35 | #define NOCOLOR 36 | #define NOCTLMGR 37 | #define NODRAWTEXT 38 | #define NOGDI 39 | #define NOKERNEL 40 | #define NOUSER 41 | #define NONLS 42 | #define NOMB 43 | #define NOMEMMGR 44 | #define NOMETAFILE 45 | #define NOMINMAX 46 | #define NOMSG 47 | #define NOOPENFILE 48 | #define NOSCROLL 49 | #define NOSERVICE 50 | #define NOSOUND 51 | #define NOTEXTMETRIC 52 | #define NOWH 53 | #define NOWINOFFSETS 54 | #define NOCOMM 55 | #define NOKANJI 56 | #define NOHELP 57 | #define NOPROFILER 58 | #define NODEFERWINDOWPOS 59 | #define NOMCX 60 | #include 61 | #include 62 | 63 | int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow); 64 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR _lpCmdLine, int nCmdShow) { 65 | WCHAR *lpCmdLine = GetCommandLineW(); 66 | if (__argc == 1) { // avoids GetCommandLineW bug that does not always quote the program name if no arguments 67 | do { ++lpCmdLine; } while (*lpCmdLine); 68 | } else { 69 | BOOL quoted = lpCmdLine[0] == L'"'; 70 | ++lpCmdLine; // skips the " or the first letter (all paths are at least 1 letter) 71 | while (*lpCmdLine) { 72 | if (quoted && lpCmdLine[0] == L'"') { quoted = FALSE; } // found end quote 73 | else if (!quoted && lpCmdLine[0] == L' ') { 74 | // found an unquoted space, now skip all spaces 75 | do { ++lpCmdLine; } while (lpCmdLine[0] == L' '); 76 | break; 77 | } 78 | ++lpCmdLine; 79 | } 80 | } 81 | return wWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); 82 | } 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /mingw-unicode.c: -------------------------------------------------------------------------------- 1 | // This document is released into the public domain. Absolutely no warranty is provided. 2 | // See http://www.coderforlife.com/projects/utilities. 3 | // 4 | // This is for the MinGW compiler which does not support wmain. 5 | // It is a wrapper for _tmain when _UNICODE is defined (wmain). 6 | // 7 | // !! Do not compile this file, but instead include it right before your _tmain function like: 8 | // #include "mingw-unicode.c" 9 | // int _tmain(int argc, _TCHAR *argv[]) { 10 | // 11 | // If you wish to have enpv in your main, then define the following before including this file: 12 | // #define MAIN_USE_ENVP 13 | // 14 | // This wrapper adds ~300 bytes to the program and negligible overhead 15 | 16 | #undef _tmain 17 | #ifdef _UNICODE 18 | #define _tmain wmain 19 | #else 20 | #define _tmain main 21 | #endif 22 | 23 | #if defined(__GNUC__) && defined(_UNICODE) 24 | 25 | #ifndef __MSVCRT__ 26 | #error Unicode main function requires linking to MSVCRT 27 | #endif 28 | 29 | #include 30 | #include 31 | 32 | extern int _CRT_glob; 33 | extern 34 | #ifdef __cplusplus 35 | "C" 36 | #endif 37 | void __wgetmainargs(int*,wchar_t***,wchar_t***,int,int*); 38 | 39 | #ifdef MAIN_USE_ENVP 40 | int wmain(int argc, wchar_t *argv[], wchar_t *envp[]); 41 | #else 42 | int wmain(int argc, wchar_t *argv[]); 43 | #endif 44 | 45 | int main() { 46 | wchar_t **enpv, **argv; 47 | int argc, si = 0; 48 | __wgetmainargs(&argc, &argv, &enpv, _CRT_glob, &si); // this also creates the global variable __wargv 49 | #ifdef MAIN_USE_ENVP 50 | return wmain(argc, argv, enpv); 51 | #else 52 | return wmain(argc, argv); 53 | #endif 54 | } 55 | 56 | #endif //defined(__GNUC__) && defined(_UNICODE) 57 | --------------------------------------------------------------------------------