1 #define WIN32_LEAN_AND_MEAN
2 #include <windows.h>
3 #include "loadfuncs.h"
4
5 //
6 // UnloadFuncs:
7 //
8 // This function will reset all the function pointers of a function loaded
9 // by LaodFuncs and will free the DLL instance provided.
10 //
11
12 void
UnloadFuncs(FUNC_INFO fi[],HINSTANCE h)13 UnloadFuncs(
14 FUNC_INFO fi[],
15 HINSTANCE h
16 )
17 {
18 int n;
19 if (fi)
20 for (n = 0; fi[n].func_ptr_var; n++)
21 *(fi[n].func_ptr_var) = NULL;
22 if (h) FreeLibrary(h);
23 }
24
25
26 //
27 // LoadFuncs:
28 //
29 // This function try to load the functions for a DLL. It returns 0 on failure
30 // and non-zero on success. The parameters are described below.
31 //
32
33 int
LoadFuncs(const char * dll_name,FUNC_INFO fi[],HINSTANCE * ph,int * pindex,int cleanup,int go_on,int silent)34 LoadFuncs(
35 const char* dll_name,
36 FUNC_INFO fi[],
37 HINSTANCE* ph, // [out, optional] - DLL handle
38 int* pindex, // [out, optional] - index of last func loaded (-1 if none)
39 int cleanup, // cleanup function pointers and unload on error
40 int go_on, // continue loading even if some functions cannot be loaded
41 int silent // do not pop-up a system dialog if DLL cannot be loaded
42
43 )
44 {
45 HINSTANCE h;
46 int i, n, last_i;
47 int error = 0;
48 UINT em;
49
50 if (ph) *ph = 0;
51 if (pindex) *pindex = -1;
52
53 for (n = 0; fi[n].func_ptr_var; n++)
54 *(fi[n].func_ptr_var) = NULL;
55
56 if (silent)
57 em = SetErrorMode(SEM_FAILCRITICALERRORS);
58 h = LoadLibrary(dll_name);
59 if (silent)
60 SetErrorMode(em);
61
62 if (!h)
63 return 0;
64
65 last_i = -1;
66 for (i = 0; (go_on || !error) && (i < n); i++)
67 {
68 void* p = (void*)GetProcAddress(h, fi[i].func_name);
69 if (!p)
70 error = 1;
71 else
72 {
73 last_i = i;
74 *(fi[i].func_ptr_var) = p;
75 }
76 }
77 if (pindex) *pindex = last_i;
78 if (error && cleanup && !go_on) {
79 for (i = 0; i < n; i++) {
80 *(fi[i].func_ptr_var) = NULL;
81 }
82 FreeLibrary(h);
83 return 0;
84 }
85 if (ph) *ph = h;
86 if (error) return 0;
87 return 1;
88 }
89