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 ae.sys.windows.imports; 20 mixin(importWin32!q{windef}); 21 mixin(importWin32!q{winuser}); 22 23 import ae.ui.app.application; 24 import ae.utils.exception; 25 26 extern (Windows) 27 int WinMain(HINSTANCE hInstance, 28 HINSTANCE hPrevInstance, 29 LPSTR lpCmdLine, 30 int nCmdShow) 31 { 32 int result; 33 34 try 35 { 36 Runtime.initialize(); 37 result = runApplication(getArgs()); 38 Runtime.terminate(); 39 } 40 41 catch (Throwable o) // catch any uncaught exceptions 42 { 43 MessageBoxA(null, toUTFz!LPCSTR(formatException(o)), "Error", 44 MB_OK | MB_ICONEXCLAMATION); 45 result = 1; // failed 46 } 47 48 return result; 49 } 50 51 private: 52 // Following code is adapted from D's druntime\src\rt\dmain2.d 53 54 import core.stdc.wchar_; 55 import core.stdc.stdlib; 56 57 mixin(importWin32!q{winbase}); 58 mixin(importWin32!q{shellapi}); 59 mixin(importWin32!q{winnls}); 60 61 string[] getArgs() 62 { 63 wchar_t* wcbuf = GetCommandLineW(); 64 size_t wclen = wcslen(wcbuf); 65 int wargc = 0; 66 wchar_t** wargs = CommandLineToArgvW(wcbuf, &wargc); 67 68 size_t cargl = WideCharToMultiByte(CP_UTF8, 0, wcbuf, cast(uint)wclen, null, 0, null, null); 69 70 char* cargp = cast(char*) malloc(cargl); 71 char[][] args = ((cast(char[]*) malloc(wargc * (char[]).sizeof)))[0 .. wargc]; 72 73 for (size_t i = 0, p = 0; i < wargc; i++) 74 { 75 size_t wlen = wcslen(wargs[i]); 76 size_t clen = WideCharToMultiByte(CP_UTF8, 0, &wargs[i][0], cast(uint)wlen, null, 0, null, null); 77 args[i] = cargp[p .. p+clen]; 78 p += clen; assert(p <= cargl); 79 WideCharToMultiByte(CP_UTF8, 0, &wargs[i][0], cast(uint)wlen, &args[i][0], cast(uint)clen, null, null); 80 } 81 LocalFree(cast(HLOCAL)wargs); 82 wargs = null; 83 wargc = 0; 84 85 return cast(string[])args; 86 }