1 /* AbiSource Application Framework
2 * Copyright (C) 2001 Mike Nordell
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #define WIN32_LEAN_AND_MEAN
21 #define NOUSER
22 #define NOGDI
23 #define NOSERVICE
24 #include <windows.h>
25
26 #include "xap_Win32Module.h"
27 #include "ut_types.h"
28 #include "ut_string.h"
29 #include "ut_assert.h"
30
31
32 static const char szErrBadParam[] = "Bad parameter";
33 static const char szErrNoDllFound[] = "Could not load library";
34 static const char szErrNoDllLoaded[] = "No library loaded";
35 static const char szErrAlreadyLoaded[] = "Library already loaded";
36 static const char szErrCouldNotUnload[] = "Could not unload library";
37
38 class ABI_EXPORT XAP_Win32ModuleImpl
39 {
40 public:
XAP_Win32ModuleImpl()41 XAP_Win32ModuleImpl() : m_hMod(0), m_pszErr(0), m_pszModuleName(0) { }
~XAP_Win32ModuleImpl()42 ~XAP_Win32ModuleImpl()
43 {
44 }
45
load(const char * name)46 bool load(const char* name)
47 {
48 if (m_hMod)
49 {
50 m_pszErr = szErrAlreadyLoaded;
51 return false;
52 }
53
54 WCHAR szName[512];
55
56 MultiByteToWideChar(CP_UTF8,0,name,-1,szName,512);
57
58 m_hMod = LoadLibraryW(szName);
59 if (!m_hMod)
60 {
61 m_pszErr = szErrNoDllFound;
62 return false;
63 }
64 m_pszErr = 0;
65 m_pszModuleName = new char[strlen(name) + 1];
66 if (m_pszModuleName)
67 {
68 strcpy(m_pszModuleName, name);
69 }
70 return true;
71 }
72
unload()73 bool unload()
74 {
75 if (m_hMod)
76 {
77 if (FreeLibrary(m_hMod))
78 {
79 m_hMod = 0;
80 delete [] m_pszModuleName;
81 m_pszModuleName = 0;
82 m_pszErr = 0;
83 return true;
84 }
85 m_pszErr = szErrCouldNotUnload;
86 return false;
87 }
88 m_pszErr = szErrNoDllLoaded;
89 return false;
90 }
91
92
resolveSymbol(const char * symbol_name,void ** symbol)93 bool resolveSymbol(const char* symbol_name, void** symbol)
94 {
95 if (!symbol_name || !*symbol_name || !symbol ) // TODO Fix this so it works || IsBadWritePtr(*symbol, sizeof(*symbol)))
96 {
97 m_pszErr = szErrBadParam;
98 return false;
99 }
100
101 FARPROC pProc = GetProcAddress(m_hMod, symbol_name);
102 if (pProc)
103 {
104 #ifdef __MINGW32__
105 *symbol = (void*)(pProc);
106 #else
107 *symbol = reinterpret_cast<void*>(pProc);
108 #endif
109 m_pszErr = 0;
110 return true;
111 }
112 return false;
113 }
114
getModuleName(char ** dest) const115 bool getModuleName(char** dest) const
116 {
117 if (!m_hMod)
118 {
119 m_pszErr = szErrNoDllLoaded;
120 return false;
121 }
122 if (!dest) // TODO Fix this so it works || IsBadWritePtr(*dest, sizeof(*dest)))
123 {
124 m_pszErr = szErrBadParam;
125 return false;
126 }
127 if (m_pszModuleName)
128 {
129 *dest = g_strdup(m_pszModuleName);
130 m_pszErr = 0;
131 return true;
132 }
133 return false;
134 }
135
getErrorMsg(char ** dest) const136 bool getErrorMsg(char** dest) const
137 {
138 if (!dest) // TODO Fix this so it works || IsBadWritePtr(*dest, sizeof(*dest)))
139 {
140 m_pszErr = szErrBadParam;
141 return false;
142 }
143 if (m_pszErr)
144 {
145 *dest = g_strdup(m_pszErr);
146 return true;
147 }
148 return false;
149 }
150
151 HMODULE m_hMod;
152 mutable const char* m_pszErr;
153 mutable char* m_pszModuleName;
154 };
155
XAP_Win32Module()156 XAP_Win32Module::XAP_Win32Module()
157 : pimpl(new XAP_Win32ModuleImpl)
158 {
159 UT_ASSERT_HARMLESS(pimpl);
160 }
161
~XAP_Win32Module()162 XAP_Win32Module::~XAP_Win32Module()
163 {
164 delete pimpl;
165 }
166
load(const char * name)167 bool XAP_Win32Module::load(const char* name)
168 {
169 return pimpl->load(name);
170 }
171
unload()172 bool XAP_Win32Module::unload()
173 {
174 return pimpl->unload();
175 }
176
resolveSymbol(const char * symbol_name,void ** symbol)177 bool XAP_Win32Module::resolveSymbol(const char* symbol_name, void** symbol)
178 {
179 return pimpl->resolveSymbol(symbol_name, symbol);
180 }
181
getModuleName(char ** dest) const182 bool XAP_Win32Module::getModuleName(char** dest) const
183 {
184 return pimpl->getModuleName(dest);
185 }
186
getErrorMsg(char ** dest) const187 bool XAP_Win32Module::getErrorMsg(char** dest) const
188 {
189 return pimpl->getErrorMsg(dest);
190 }
191
192