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