Diff

Not logged in

Differences From Artifact [bbb6d5827e2268a2]:

To Artifact [6b2f129d530b6aee]:


1 -import win32.windows; 1 +import core.runtime; 2 +import win32.windows; 2 3 import std.string; 3 4 4 5 class WinDLLException : Exception 5 6 { 6 7 private this( string msg ) { super(msg); } 7 8 } 8 9 9 10 class WinDLL 10 11 { 11 12 // constructor: 12 - // Load a DLL with specified name 13 + // Load a DLL of specified name. 14 + // Avoid loading the DLL from the current directory for security. 13 15 14 16 this( string dllname ) 15 17 { 16 18 char original_cur[MAX_PATH]; 17 19 char sys[MAX_PATH]; 18 - GetCurrentDirectoryA(MAX_PATH, original_cur.ptr); 19 - GetSystemDirectoryA(sys.ptr, MAX_PATH); 20 + GetCurrentDirectoryA(original_cur.length, original_cur.ptr); 21 + GetSystemDirectoryA(sys.ptr, sys.length); 20 22 SetCurrentDirectoryA(sys.ptr); 21 - handle = LoadLibraryA( dllname.ptr ); 23 + handle = cast(HINSTANCE) Runtime.loadLibrary(dllname); 22 24 SetCurrentDirectoryA(original_cur.ptr); 23 25 if( handle is null ) 24 26 throw new WinDLLException( dllname ~ " not found" ); 25 27 } 26 28 27 29 static WinDLL load( string name ) 28 30 { ................................................................................ 34 36 // close: 35 37 // free the DLL 36 38 37 39 void close() 38 40 { 39 41 if( handle ) 40 42 { 41 - FreeLibrary( handle ); 43 + Runtime.unloadLibrary(handle); 42 44 handle = null; 43 45 } 44 46 } 45 47 46 48 // get_api: 47 49 // extract a function with specified name from the DLL. 48 50 // may return null. ................................................................................ 56 58 return cast(FnT) GetProcAddress( handle, apiname.toStringz() ); 57 59 } 58 60 } 59 61 60 62 // load_resource: 61 63 // extract a resource 62 64 63 - void* load_resource( string resname, char* type ) 65 + void* load_resource( string resname, const char* type ) 64 66 in { assert(handle); } 65 67 body 66 68 { 69 + if(resname in rsrc_map) 70 + return rsrc_map[resname]; 71 + 67 72 HRSRC hi = FindResource( handle, resname.toStringz(), type ); 68 73 if( hi is null ) 69 74 return null; 70 75 HGLOBAL hr = LoadResource( handle, hi ); 71 76 if( hr is null ) 72 77 return null; 73 - return LockResource( hr ); 78 + void* v = LockResource( hr ); 79 + rsrc_map[resname] = v; 80 + return v; 74 81 } 75 82 76 83 // load_diaglog: 77 84 // specialized version of load_resource 78 85 79 86 void* load_dialog( string resname ) 80 87 { 81 88 return load_resource( resname, RT_DIALOG ); 82 89 } 83 90 84 91 private HINSTANCE handle; 92 + private void*[string] rsrc_map; 85 93 }