1 /** 2 * Windows exceptions. 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.sys.windows.exception; 15 16 import core.sys.windows.windows; 17 18 import std.string; 19 20 import ae.sys.windows.text; 21 22 class WindowsException : Exception 23 { 24 DWORD code; 25 26 this(DWORD code, string str=null) 27 { 28 this.code = code; 29 30 wchar *lpMsgBuf = null; 31 FormatMessageW( 32 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 33 null, 34 code, 35 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 36 cast(LPWSTR)&lpMsgBuf, 37 0, 38 null); 39 40 auto message = lpMsgBuf.fromWString(); 41 if (lpMsgBuf) 42 LocalFree(lpMsgBuf); 43 44 message = strip(message); 45 message ~= format(" (error %d)", code); 46 if (str) 47 message = str ~ ": " ~ message; 48 49 super(message); 50 } 51 } 52 53 T wenforce(T)(T cond, string str=null) 54 { 55 if (cond) 56 return cond; 57 58 throw new WindowsException(GetLastError(), str); 59 }