Diff

Not logged in

Differences From Artifact [bbb6d5827e2268a2]:

To Artifact [6b2f129d530b6aee]:


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