1 /** 2 * ae.ui.app.windows.main 3 * 4 * License: 5 * This Source Code Form is subject to the terms of 6 * the Mozilla Public License, v. 2.0. If a copy of 7 * the MPL was not distributed with this file, You 8 * can obtain one at http://mozilla.org/MPL/2.0/. 9 * 10 * Authors: 11 * Vladimir Panteleev <vladimir@thecybershadow.net> 12 */ 13 14 module ae.ui.app.windows.main; 15 16 import core.runtime; 17 import std.utf; 18 19 import win32.windef; 20 import win32.winuser; 21 22 import ae.ui.app.application; 23 import ae.utils.exception; 24 25 extern (Windows) 26 int WinMain(HINSTANCE hInstance, 27 HINSTANCE hPrevInstance, 28 LPSTR lpCmdLine, 29 int nCmdShow) 30 { 31 int result; 32 33 void exceptionHandler(Throwable e) 34 { 35 throw e; 36 } 37 38 try 39 { 40 Runtime.initialize(&exceptionHandler); 41 result = runApplication(getArgs()); 42 Runtime.terminate(&exceptionHandler); 43 } 44 45 catch (Throwable o) // catch any uncaught exceptions 46 { 47 MessageBoxA(null, toUTFz!LPCSTR(formatException(o)), "Error", 48 MB_OK | MB_ICONEXCLAMATION); 49 result = 1; // failed 50 } 51 52 return result; 53 } 54 55 private: 56 // Following code is adapted from D's druntime\src\rt\dmain2.d 57 58 import core.stdc.wchar_; 59 import core.stdc.stdlib; 60 61 import win32.winbase; 62 import win32.shellapi; 63 import win32.winnls; 64 65 string[] getArgs() 66 { 67 wchar_t* wcbuf = GetCommandLineW(); 68 size_t wclen = wcslen(wcbuf); 69 int wargc = 0; 70 wchar_t** wargs = CommandLineToArgvW(wcbuf, &wargc); 71 72 size_t cargl = WideCharToMultiByte(CP_UTF8, 0, wcbuf, cast(uint)wclen, null, 0, null, null); 73 74 char* cargp = cast(char*) malloc(cargl); 75 char[][] args = ((cast(char[]*) malloc(wargc * (char[]).sizeof)))[0 .. wargc]; 76 77 for (size_t i = 0, p = 0; i < wargc; i++) 78 { 79 size_t wlen = wcslen(wargs[i]); 80 size_t clen = WideCharToMultiByte(CP_UTF8, 0, &wargs[i][0], cast(uint)wlen, null, 0, null, null); 81 args[i] = cargp[p .. p+clen]; 82 p += clen; assert(p <= cargl); 83 WideCharToMultiByte(CP_UTF8, 0, &wargs[i][0], cast(uint)wlen, &args[i][0], cast(uint)clen, null, null); 84 } 85 LocalFree(cast(HLOCAL)wargs); 86 wargs = null; 87 wargc = 0; 88 89 return cast(string[])args; 90 }