Diff

Not logged in

Differences From Artifact [66565a0003521f7b]:

To Artifact [109289fec72a32d4]:


1 private import win32.ansi.windows; | 1 private import win32.windows; > 2 private import std.string; 2 3 3 class WinDLLException : Exception 4 class WinDLLException : Exception 4 { 5 { 5 private this( char[] msg ) { super(msg); } | 6 private this( string msg ) { super(msg); } 6 } 7 } 7 8 8 class WinDLL 9 class WinDLL 9 { 10 { 10 // constructor: 11 // constructor: 11 // Load a DLL with specified name 12 // Load a DLL with specified name 12 13 13 this( char[] dllname ) | 14 this( string dllname ) 14 { 15 { 15 char original_cur[MAX_PATH]; 16 char original_cur[MAX_PATH]; 16 char sys[MAX_PATH]; 17 char sys[MAX_PATH]; 17 GetCurrentDirectory(MAX_PATH, original_cur); | 18 GetCurrentDirectoryA(MAX_PATH, original_cur.ptr); 18 GetSystemDirectory(sys, MAX_PATH); | 19 GetSystemDirectoryA(sys.ptr, MAX_PATH); 19 SetCurrentDirectory(sys); | 20 SetCurrentDirectoryA(sys.ptr); 20 handle = LoadLibrary( dllname ); | 21 handle = LoadLibraryA( dllname.ptr ); 21 SetCurrentDirectory(original_cur); | 22 SetCurrentDirectoryA(original_cur.ptr); 22 if( handle is null ) 23 if( handle is null ) 23 throw new WinDLLException( dllname ~ " not found" ); 24 throw new WinDLLException( dllname ~ " not found" ); 24 } 25 } 25 26 26 static WinDLL load( char[] name ) | 27 static WinDLL load( string name ) 27 { 28 { 28 try { 29 try { 29 return new WinDLL(name); 30 return new WinDLL(name); 30 } catch(WinDLLException e) { return null; } 31 } catch(WinDLLException e) { return null; } 31 } 32 } 32 33 33 // close: 34 // close: ................................................................................................................................................................................ 44 45 45 // get_api: 46 // get_api: 46 // extract a function with specified name from the DLL. 47 // extract a function with specified name from the DLL. 47 // may return null. 48 // may return null. 48 49 49 template get_api(FnT) 50 template get_api(FnT) 50 { 51 { 51 FnT get_api( char[] apiname ) | 52 FnT get_api( string apiname ) 52 in { assert(handle); } 53 in { assert(handle); } 53 body 54 body 54 { 55 { 55 return cast(FnT) GetProcAddress( handle, apiname | 56 return cast(FnT) GetProcAddress( handle, apiname 56 } 57 } 57 } 58 } 58 59 59 // load_resource: 60 // load_resource: 60 // extract a resource 61 // extract a resource 61 62 62 void* load_resource( char[] resname, char* type ) | 63 void* load_resource( string resname, char* type ) 63 in { assert(handle); } 64 in { assert(handle); } 64 body 65 body 65 { 66 { 66 HRSRC hi = FindResource( handle, resname, type ); | 67 HRSRC hi = FindResource( handle, resname.toStringz(), ty 67 if( hi is null ) 68 if( hi is null ) 68 return null; 69 return null; 69 HGLOBAL hr = LoadResource( handle, hi ); 70 HGLOBAL hr = LoadResource( handle, hi ); 70 if( hr is null ) 71 if( hr is null ) 71 return null; 72 return null; 72 return LockResource( hr ); 73 return LockResource( hr ); 73 } 74 } 74 75 75 // load_diaglog: 76 // load_diaglog: 76 // specialized version of load_resource 77 // specialized version of load_resource 77 78 78 void* load_dialog( char[] resname ) | 79 void* load_dialog( string resname ) 79 { 80 { 80 return load_resource( resname, RT_DIALOG ); 81 return load_resource( resname, RT_DIALOG ); 81 } 82 } 82 83 83 private HINSTANCE handle; 84 private HINSTANCE handle; 84 } 85 }