4 * Copyright (C) 2005-2013 Team XBMC
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
23 #include "cores/DllLoader/LibraryLoader.h"
24 #include "utils/StdString.h"
27 ///////////////////////////////////////////////////////////
29 // DECLARE_DLL_WRAPPER
31 // Declares the constructor of the wrapper class.
32 // This must be followed by one or more
33 // DEFINE_METHODX/DEFINE_METHOD_LINKAGEX and
34 // one BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE block.
36 // classname: name of the wrapper class to construct
37 // dllname: file including path of the dll to wrap
39 #define DECLARE_DLL_WRAPPER(classname, dllname) \
40 XDECLARE_DLL_WRAPPER(classname,dllname)
42 #define XDECLARE_DLL_WRAPPER(classname, dllname) \
44 classname () : DllDynamic( dllname ) {}
46 ///////////////////////////////////////////////////////////
48 // DECLARE_DLL_WRAPPER_TEMPLATE_BEGIN
50 // Declares the constructor of the wrapper class.
51 // The method SetFile(strDllName) can be used to set the
52 // dll of this wrapper.
53 // This must be followed by one or more
54 // DEFINE_METHODX/DEFINE_METHOD_LINKAGEX and
55 // one BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE block.
57 // classname: name of the wrapper class to construct
59 #define DECLARE_DLL_WRAPPER_TEMPLATE(classname) \
64 ///////////////////////////////////////////////////////////
68 // Tells the dllloader to load Debug symblos when possible
69 #define LOAD_SYMBOLS() \
71 virtual bool LoadSymbols() { return true; }
73 ///////////////////////////////////////////////////////////
77 // Defines a global for export from the dll as well as
78 // a function for accessing it (Get_name).
80 // type: The variables type.
81 // name: Name of the variable.
84 #define DEFINE_GLOBAL_PTR(type, name) \
88 void* m_##name##_ptr; \
91 virtual type* Get_##name (void) \
96 #define DEFINE_GLOBAL(type, name) \
100 void* m_##name##_ptr; \
103 virtual type Get_##name (void) \
108 ///////////////////////////////////////////////////////////
110 // DEFINE_METHOD_LINKAGE
112 // Defines a function for an export from a dll, if the
113 // calling convention is not __cdecl.
114 // Use DEFINE_METHOD_LINKAGE for each function to be resolved.
116 // result: Result of the function
117 // linkage: Calling convention of the function
118 // name: Name of the function
119 // args: Arguments of the function, enclosed in parentheses
121 #define DEFINE_METHOD_LINKAGE_FP(result, linkage, name, args) \
123 typedef result (linkage * name##_METHOD) args; \
126 name##_METHOD name; \
130 #define DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, args2) \
132 typedef result (linkage * name##_METHOD) args; \
134 name##_METHOD m_##name; \
135 void* m_##name##_ptr; \
138 virtual result name args \
140 return m_##name args2; \
143 #define DEFINE_METHOD_LINKAGE0(result, linkage, name) \
144 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, () , ())
146 #define DEFINE_METHOD_LINKAGE1(result, linkage, name, args) \
147 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1))
149 #define DEFINE_METHOD_LINKAGE2(result, linkage, name, args) \
150 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2))
152 #define DEFINE_METHOD_LINKAGE3(result, linkage, name, args) \
153 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3))
155 #define DEFINE_METHOD_LINKAGE4(result, linkage, name, args) \
156 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4))
158 #define DEFINE_METHOD_LINKAGE5(result, linkage, name, args) \
159 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5))
161 #define DEFINE_METHOD_LINKAGE6(result, linkage, name, args) \
162 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6))
164 #define DEFINE_METHOD_LINKAGE7(result, linkage, name, args) \
165 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7))
167 #define DEFINE_METHOD_LINKAGE8(result, linkage, name, args) \
168 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8))
170 #define DEFINE_METHOD_LINKAGE9(result, linkage, name, args) \
171 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8, p9))
173 #define DEFINE_METHOD_LINKAGE10(result, linkage, name, args) \
174 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10))
176 #define DEFINE_METHOD_LINKAGE11(result, linkage, name, args) \
177 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11))
179 ///////////////////////////////////////////////////////////
183 // Defines a function for an export from a dll as a fuction pointer.
184 // Use DEFINE_METHOD_FP for each function to be resolved. Functions
185 // defined like this are not listed by IntelliSence.
187 // result: Result of the function
188 // name: Name of the function
189 // args: Arguments of the function, enclosed in parentheses
190 // The parameter names can be anything
192 #define DEFINE_METHOD_FP(result, name, args) DEFINE_METHOD_LINKAGE_FP(result, __cdecl, name, args)
194 ///////////////////////////////////////////////////////////
198 // Defines a function for an export from a dll.
199 // Use DEFINE_METHODX for each function to be resolved.
200 // where X is the number of parameter the function has.
202 // result: Result of the function
203 // name: Name of the function
204 // args: Arguments of the function, enclosed in parentheses
205 // The parameter names have to be renamed to px, where
206 // x is the number of the parameter
208 #define DEFINE_METHOD0(result, name) DEFINE_METHOD_LINKAGE0(result, __cdecl, name)
209 #define DEFINE_METHOD1(result, name, args) DEFINE_METHOD_LINKAGE1(result, __cdecl, name, args)
210 #define DEFINE_METHOD2(result, name, args) DEFINE_METHOD_LINKAGE2(result, __cdecl, name, args)
211 #define DEFINE_METHOD3(result, name, args) DEFINE_METHOD_LINKAGE3(result, __cdecl, name, args)
212 #define DEFINE_METHOD4(result, name, args) DEFINE_METHOD_LINKAGE4(result, __cdecl, name, args)
213 #define DEFINE_METHOD5(result, name, args) DEFINE_METHOD_LINKAGE5(result, __cdecl, name, args)
214 #define DEFINE_METHOD6(result, name, args) DEFINE_METHOD_LINKAGE6(result, __cdecl, name, args)
215 #define DEFINE_METHOD7(result, name, args) DEFINE_METHOD_LINKAGE7(result, __cdecl, name, args)
216 #define DEFINE_METHOD8(result, name, args) DEFINE_METHOD_LINKAGE8(result, __cdecl, name, args)
217 #define DEFINE_METHOD9(result, name, args) DEFINE_METHOD_LINKAGE9(result, __cdecl, name, args)
218 #define DEFINE_METHOD10(result, name, args) DEFINE_METHOD_LINKAGE10(result, __cdecl, name, args)
219 #define DEFINE_METHOD11(result, name, args) DEFINE_METHOD_LINKAGE11(result, __cdecl, name, args)
222 ///////////////////////////////////////////////////////////
224 // DEFINE_FUNC_ALIGNED 0-X
226 // Defines a function for an export from a dll, wich
227 // require a aligned stack on function call
228 // Use DEFINE_FUNC_ALIGNED for each function to be resolved.
230 // result: Result of the function
231 // linkage: Calling convention of the function
232 // name: Name of the function
233 // args: Argument types of the function
235 // Actual function call will expand to something like this
236 // this will align the stack (esp) at the point of function
237 // entry as required by gcc compiled dlls, it is abit abfuscated
238 // to allow for different sized variables
240 // int64_t test(int64_t p1, char p2, char p3)
242 // int o,s = ((sizeof(p1)+3)&~3)+((sizeof(p2)+3)&~3)+((sizeof(p3)+3)&~3);
243 // __asm mov [o],esp;
244 // __asm sub esp, [s];
245 // __asm and esp, ~15;
246 // __asm add esp, [s]
247 // m_test(p1, p2, p3); //return value will still be correct aslong as we don't mess with it
248 // __asm mov esp,[o];
251 #define ALS(a) ((sizeof(a)+3)&~3)
252 #define DEFINE_FUNC_PART1(result, linkage, name, args) \
254 typedef result (linkage * name##_type)##args; \
256 name##_type m_##name; \
257 void* m_##name##_ptr; \
260 virtual result name##args
262 #define DEFINE_FUNC_PART2(size) \
272 #define DEFINE_FUNC_PART3(name,args) \
279 #define DEFINE_FUNC_ALIGNED0(result, linkage, name) \
280 DEFINE_FUNC_PART1(result, linkage, name, ()) \
281 DEFINE_FUNC_PART2(0) \
282 DEFINE_FUNC_PART3(name,())
284 #define DEFINE_FUNC_ALIGNED1(result, linkage, name, t1) \
285 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1)) \
286 DEFINE_FUNC_PART2(ALS(p1)) \
287 DEFINE_FUNC_PART3(name,(p1))
289 #define DEFINE_FUNC_ALIGNED2(result, linkage, name, t1, t2) \
290 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2)) \
291 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)) \
292 DEFINE_FUNC_PART3(name,(p1, p2))
294 #define DEFINE_FUNC_ALIGNED3(result, linkage, name, t1, t2, t3) \
295 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3)) \
296 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)) \
297 DEFINE_FUNC_PART3(name,(p1, p2, p3))
299 #define DEFINE_FUNC_ALIGNED4(result, linkage, name, t1, t2, t3, t4) \
300 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4)) \
301 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)) \
302 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4))
304 #define DEFINE_FUNC_ALIGNED5(result, linkage, name, t1, t2, t3, t4, t5) \
305 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5)) \
306 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)) \
307 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5))
309 #define DEFINE_FUNC_ALIGNED6(result, linkage, name, t1, t2, t3, t4, t5, t6) \
310 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6)) \
311 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)) \
312 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6))
314 #define DEFINE_FUNC_ALIGNED7(result, linkage, name, t1, t2, t3, t4, t5, t6, t7) \
315 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7)) \
316 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)+ALS(p7)) \
317 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6, p7))
319 #define DEFINE_FUNC_ALIGNED8(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8) \
320 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8)) \
321 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)+ALS(p7)+ALS(p8)) \
322 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6, p7, p8))
324 #define DEFINE_FUNC_ALIGNED9(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8, t9) \
325 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9)) \
326 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)+ALS(p7)+ALS(p8)+ALS(p9)) \
327 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6, p7, p8, p9))
331 #define DEFINE_FUNC_ALIGNED0(result, linkage, name) DEFINE_METHOD_LINKAGE0 (result, linkage, name)
332 #define DEFINE_FUNC_ALIGNED1(result, linkage, name, t1) DEFINE_METHOD_LINKAGE1 (result, linkage, name, (t1 p1) )
333 #define DEFINE_FUNC_ALIGNED2(result, linkage, name, t1, t2) DEFINE_METHOD_LINKAGE2 (result, linkage, name, (t1 p1, t2 p2) )
334 #define DEFINE_FUNC_ALIGNED3(result, linkage, name, t1, t2, t3) DEFINE_METHOD_LINKAGE3 (result, linkage, name, (t1 p1, t2 p2, t3 p3) )
335 #define DEFINE_FUNC_ALIGNED4(result, linkage, name, t1, t2, t3, t4) DEFINE_METHOD_LINKAGE4 (result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4) )
336 #define DEFINE_FUNC_ALIGNED5(result, linkage, name, t1, t2, t3, t4, t5) DEFINE_METHOD_LINKAGE5 (result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5) )
337 #define DEFINE_FUNC_ALIGNED6(result, linkage, name, t1, t2, t3, t4, t5, t6) DEFINE_METHOD_LINKAGE6 (result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6) )
338 #define DEFINE_FUNC_ALIGNED7(result, linkage, name, t1, t2, t3, t4, t5, t6, t7) DEFINE_METHOD_LINKAGE7 (result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7) )
339 #define DEFINE_FUNC_ALIGNED8(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8) DEFINE_METHOD_LINKAGE8 (result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8) )
340 #define DEFINE_FUNC_ALIGNED9(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8, t9) DEFINE_METHOD_LINKAGE9 (result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9) )
341 #define DEFINE_FUNC_ALIGNED10(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8, t10) DEFINE_METHOD_LINKAGE10(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9, t10 p10) )
342 #define DEFINE_FUNC_ALIGNED11(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8, t10, t11) DEFINE_METHOD_LINKAGE11(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9, t10 p10, t11 p11) )
346 ///////////////////////////////////////////////////////////
348 // BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE
350 // Defines a method that resolves the exported functions
351 // defined with DEFINE_METHOD or DEFINE_METHOD_LINKAGE.
352 // There must be a RESOLVE_METHOD or RESOLVE_METHOD_RENAME
353 // for each DEFINE_METHOD or DEFINE_METHOD_LINKAGE within this
354 // block. This block must be followed by an END_METHOD_RESOLVE.
356 #define BEGIN_METHOD_RESOLVE() \
358 virtual bool ResolveExports() \
362 #define END_METHOD_RESOLVE() \
367 ///////////////////////////////////////////////////////////
371 // Resolves a method from a dll
373 // method: Name of the method defined with DEFINE_METHOD
374 // or DEFINE_METHOD_LINKAGE
376 #define RESOLVE_METHOD(method) \
377 m_dll->ResolveExport( #method , & m_##method##_ptr ) &&
379 #define RESOLVE_METHOD_FP(method) \
380 m_dll->ResolveExport( #method , & method##_ptr ) &&
382 ///////////////////////////////////////////////////////////
384 // RESOLVE_METHOD_RENAME
386 // Resolves a method from a dll
388 // dllmethod: Name of the function exported from the dll
389 // method: Name of the method defined with DEFINE_METHOD
390 // or DEFINE_METHOD_LINKAGE
392 #define RESOLVE_METHOD_RENAME(dllmethod, method) \
393 m_dll->ResolveExport( #dllmethod , & m_##method##_ptr ) &&
395 #define RESOLVE_METHOD_RENAME_FP(dllmethod, method) \
396 m_dll->ResolveExport( #dllmethod , & method##_ptr ) &&
399 ////////////////////////////////////////////////////////////////////
401 // Example declaration of a dll wrapper class
403 // 1. Define a class with pure virtual functions with all functions
404 // exported from the dll. This is needed to use the IntelliSence
405 // feature of the Visual Studio Editor.
407 // class DllExampleInterface
410 // virtual void foo (unsigned int type, char* szTest)=0;
411 // virtual void bar (char* szTest, unsigned int type)=0;
414 // 2. Define a class, derived from DllDynamic and the previously defined
415 // interface class. Define the constructor of the class using the
416 // DECLARE_DLL_WRAPPER macro. Use the DEFINE_METHODX/DEFINE_METHOD_LINKAGEX
417 // macros to define the functions from the interface above, where X is number of
418 // parameters the function has. The function parameters
419 // have to be enclosed in parentheses. The parameter names have to be changed to px
420 // where x is the number on which position the parameter appears.
421 // Use the RESOLVE_METHOD/RESOLVE_METHOD_RENAME to do the actually resolve the functions
422 // from the dll when it's loaded. The RESOLVE_METHOD/RESOLVE_METHOD_RENAME have to
423 // be between the BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE block.
425 // class DllExample : public DllDynamic, DllExampleInterface
427 // DECLARE_DLL_WRAPPER(DllExample, special://xbmcbin/system/Example.dll)
428 // LOAD_SYMBOLS() // add this if you want to load debug symbols for the dll
429 // DEFINE_METHOD2(void, foo, (int p1, char* p2))
430 // DEFINE_METHOD_LINKAGE2(void, __stdcall, bar, (char* p1, int p2))
431 // DEFINE_METHOD_FP(void, foobar, (int type, char* szTest)) // No need to define this function in the
432 // // interface class, as it's a function pointer.
433 // // But its not recognised by IntelliSence
434 // BEGIN_METHOD_RESOLVE()
435 // RESOLVE_METHOD(foo)
436 // RESOLVE_METHOD_RENAME("_bar@8", bar)
437 // RESOLVE_METHOD_FP(foobar)
438 // END_METHOD_RESOLVE()
441 // The above macros will expand to a class that will look like this
443 // class DllExample : public DllDynamic, DllExampleInterface
446 // DllExample() : DllDynamic( "special://xbmcbin/system/Example.dll" ) {}
448 // virtual bool LoadSymbols() { return true; }
450 // typedef void (* foo_METHOD) ( int p1, char* p2 );
453 // virtual void foo( int p1, char* p2 )
455 // return m_foo(p1, p2);
458 // typedef void (__stdcall * bar_METHOD) ( char* p1, int p2 );
461 // virtual void bar( char* p1, int p2 )
463 // return m_bar(p1, p2);
466 // typedef void (* foobar_METHOD) (int type, char* szTest);
468 // foobar_METHOD foobar;
470 // virtual bool ResolveExports()
473 // m_dll->ResolveExport( "foo", (void**)& m_foo ) &&
474 // m_dll->ResolveExport( "_bar@8", (void**)& m_bar ) &&
475 // m_dll->ResolveExport( "foobar" , (void**)& foobar ) &&
481 // Usage of the class
485 // if (dll.IsLoaded())
487 // dll.foo(1, "bar");
492 ///////////////////////////////////////////////////////////
494 // Baseclass for a Dynamically loaded dll
495 // use the above macros to create a dll wrapper
501 DllDynamic(const CStdString& strDllName);
502 virtual ~DllDynamic();
504 virtual void Unload();
505 virtual bool IsLoaded() const { return m_dll!=NULL; }
507 bool EnableDelayedUnload(bool bOnOff);
508 bool SetFile(const CStdString& strDllName);
509 const CStdString &GetFile() const { return m_strDllName; }
512 virtual bool ResolveExports()=0;
513 virtual bool LoadSymbols() { return false; }
515 LibraryLoader* m_dll;
516 CStdString m_strDllName;