3 * Copyright (C) 2005-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
22 #include "cores/DllLoader/LibraryLoader.h"
23 #include "utils/StdString.h"
26 ///////////////////////////////////////////////////////////
28 // DECLARE_DLL_WRAPPER
30 // Declares the constructor of the wrapper class.
31 // This must be followed by one or more
32 // DEFINE_METHODX/DEFINE_METHOD_LINKAGEX and
33 // one BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE block.
35 // classname: name of the wrapper class to construct
36 // dllname: file including path of the dll to wrap
38 #define DECLARE_DLL_WRAPPER(classname, dllname) \
39 XDECLARE_DLL_WRAPPER(classname,dllname)
41 #define XDECLARE_DLL_WRAPPER(classname, dllname) \
43 classname () : DllDynamic( dllname ) {}
45 ///////////////////////////////////////////////////////////
47 // DECLARE_DLL_WRAPPER_TEMPLATE_BEGIN
49 // Declares the constructor of the wrapper class.
50 // The method SetFile(strDllName) can be used to set the
51 // dll of this wrapper.
52 // This must be followed by one or more
53 // DEFINE_METHODX/DEFINE_METHOD_LINKAGEX and
54 // one BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE block.
56 // classname: name of the wrapper class to construct
58 #define DECLARE_DLL_WRAPPER_TEMPLATE(classname) \
63 ///////////////////////////////////////////////////////////
67 // Tells the dllloader to load Debug symblos when possible
68 #define LOAD_SYMBOLS() \
70 virtual bool LoadSymbols() { return true; }
72 ///////////////////////////////////////////////////////////
76 // Defines a global for export from the dll as well as
77 // a function for accessing it (Get_name).
79 // type: The variables type.
80 // name: Name of the variable.
83 #define DEFINE_GLOBAL_PTR(type, name) \
87 void* m_##name##_ptr; \
90 virtual type* Get_##name (void) \
95 #define DEFINE_GLOBAL(type, name) \
99 void* m_##name##_ptr; \
102 virtual type Get_##name (void) \
107 ///////////////////////////////////////////////////////////
109 // DEFINE_METHOD_LINKAGE
111 // Defines a function for an export from a dll, if the
112 // calling convention is not __cdecl.
113 // Use DEFINE_METHOD_LINKAGE for each function to be resolved.
115 // result: Result of the function
116 // linkage: Calling convention of the function
117 // name: Name of the function
118 // args: Arguments of the function, enclosed in parentheses
120 #define DEFINE_METHOD_LINKAGE_FP(result, linkage, name, args) \
122 typedef result (linkage * name##_METHOD) args; \
125 name##_METHOD name; \
129 #define DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, args2) \
131 typedef result (linkage * name##_METHOD) args; \
133 name##_METHOD m_##name; \
134 void* m_##name##_ptr; \
137 virtual result name args \
139 return m_##name args2; \
142 #define DEFINE_METHOD_LINKAGE0(result, linkage, name) \
143 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, () , ())
145 #define DEFINE_METHOD_LINKAGE1(result, linkage, name, args) \
146 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1))
148 #define DEFINE_METHOD_LINKAGE2(result, linkage, name, args) \
149 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2))
151 #define DEFINE_METHOD_LINKAGE3(result, linkage, name, args) \
152 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3))
154 #define DEFINE_METHOD_LINKAGE4(result, linkage, name, args) \
155 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4))
157 #define DEFINE_METHOD_LINKAGE5(result, linkage, name, args) \
158 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5))
160 #define DEFINE_METHOD_LINKAGE6(result, linkage, name, args) \
161 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6))
163 #define DEFINE_METHOD_LINKAGE7(result, linkage, name, args) \
164 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7))
166 #define DEFINE_METHOD_LINKAGE8(result, linkage, name, args) \
167 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8))
169 #define DEFINE_METHOD_LINKAGE9(result, linkage, name, args) \
170 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8, p9))
172 #define DEFINE_METHOD_LINKAGE10(result, linkage, name, args) \
173 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10))
175 #define DEFINE_METHOD_LINKAGE11(result, linkage, name, args) \
176 DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11))
178 ///////////////////////////////////////////////////////////
182 // Defines a function for an export from a dll as a fuction pointer.
183 // Use DEFINE_METHOD_FP for each function to be resolved. Functions
184 // defined like this are not listed by IntelliSence.
186 // result: Result of the function
187 // name: Name of the function
188 // args: Arguments of the function, enclosed in parentheses
189 // The parameter names can be anything
191 #define DEFINE_METHOD_FP(result, name, args) DEFINE_METHOD_LINKAGE_FP(result, __cdecl, name, args)
193 ///////////////////////////////////////////////////////////
197 // Defines a function for an export from a dll.
198 // Use DEFINE_METHODX for each function to be resolved.
199 // where X is the number of parameter the function has.
201 // result: Result of the function
202 // name: Name of the function
203 // args: Arguments of the function, enclosed in parentheses
204 // The parameter names have to be renamed to px, where
205 // x is the number of the parameter
207 #define DEFINE_METHOD0(result, name) DEFINE_METHOD_LINKAGE0(result, __cdecl, name)
208 #define DEFINE_METHOD1(result, name, args) DEFINE_METHOD_LINKAGE1(result, __cdecl, name, args)
209 #define DEFINE_METHOD2(result, name, args) DEFINE_METHOD_LINKAGE2(result, __cdecl, name, args)
210 #define DEFINE_METHOD3(result, name, args) DEFINE_METHOD_LINKAGE3(result, __cdecl, name, args)
211 #define DEFINE_METHOD4(result, name, args) DEFINE_METHOD_LINKAGE4(result, __cdecl, name, args)
212 #define DEFINE_METHOD5(result, name, args) DEFINE_METHOD_LINKAGE5(result, __cdecl, name, args)
213 #define DEFINE_METHOD6(result, name, args) DEFINE_METHOD_LINKAGE6(result, __cdecl, name, args)
214 #define DEFINE_METHOD7(result, name, args) DEFINE_METHOD_LINKAGE7(result, __cdecl, name, args)
215 #define DEFINE_METHOD8(result, name, args) DEFINE_METHOD_LINKAGE8(result, __cdecl, name, args)
216 #define DEFINE_METHOD9(result, name, args) DEFINE_METHOD_LINKAGE9(result, __cdecl, name, args)
217 #define DEFINE_METHOD10(result, name, args) DEFINE_METHOD_LINKAGE10(result, __cdecl, name, args)
218 #define DEFINE_METHOD11(result, name, args) DEFINE_METHOD_LINKAGE11(result, __cdecl, name, args)
220 #ifdef TARGET_WINDOWS
221 ///////////////////////////////////////////////////////////
223 // DEFINE_FUNC_ALIGNED 0-X
225 // Defines a function for an export from a dll, wich
226 // require a aligned stack on function call
227 // Use DEFINE_FUNC_ALIGNED for each function to be resolved.
229 // result: Result of the function
230 // linkage: Calling convention of the function
231 // name: Name of the function
232 // args: Argument types of the function
234 // Actual function call will expand to something like this
235 // this will align the stack (esp) at the point of function
236 // entry as required by gcc compiled dlls, it is abit abfuscated
237 // to allow for different sized variables
239 // int64_t test(int64_t p1, char p2, char p3)
241 // int o,s = ((sizeof(p1)+3)&~3)+((sizeof(p2)+3)&~3)+((sizeof(p3)+3)&~3);
242 // __asm mov [o],esp;
243 // __asm sub esp, [s];
244 // __asm and esp, ~15;
245 // __asm add esp, [s]
246 // m_test(p1, p2, p3); //return value will still be correct aslong as we don't mess with it
247 // __asm mov esp,[o];
250 #define ALS(a) ((sizeof(a)+3)&~3)
251 #define DEFINE_FUNC_PART1(result, linkage, name, args) \
253 typedef result (linkage * name##_type)##args; \
255 name##_type m_##name; \
256 void* m_##name##_ptr; \
259 virtual result name##args
261 #define DEFINE_FUNC_PART2(size) \
271 #define DEFINE_FUNC_PART3(name,args) \
278 #define DEFINE_FUNC_ALIGNED0(result, linkage, name) \
279 DEFINE_FUNC_PART1(result, linkage, name, ()) \
280 DEFINE_FUNC_PART2(0) \
281 DEFINE_FUNC_PART3(name,())
283 #define DEFINE_FUNC_ALIGNED1(result, linkage, name, t1) \
284 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1)) \
285 DEFINE_FUNC_PART2(ALS(p1)) \
286 DEFINE_FUNC_PART3(name,(p1))
288 #define DEFINE_FUNC_ALIGNED2(result, linkage, name, t1, t2) \
289 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2)) \
290 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)) \
291 DEFINE_FUNC_PART3(name,(p1, p2))
293 #define DEFINE_FUNC_ALIGNED3(result, linkage, name, t1, t2, t3) \
294 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3)) \
295 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)) \
296 DEFINE_FUNC_PART3(name,(p1, p2, p3))
298 #define DEFINE_FUNC_ALIGNED4(result, linkage, name, t1, t2, t3, t4) \
299 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4)) \
300 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)) \
301 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4))
303 #define DEFINE_FUNC_ALIGNED5(result, linkage, name, t1, t2, t3, t4, t5) \
304 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5)) \
305 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)) \
306 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5))
308 #define DEFINE_FUNC_ALIGNED6(result, linkage, name, t1, t2, t3, t4, t5, t6) \
309 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6)) \
310 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)) \
311 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6))
313 #define DEFINE_FUNC_ALIGNED7(result, linkage, name, t1, t2, t3, t4, t5, t6, t7) \
314 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7)) \
315 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)+ALS(p7)) \
316 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6, p7))
318 #define DEFINE_FUNC_ALIGNED8(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8) \
319 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8)) \
320 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)+ALS(p7)+ALS(p8)) \
321 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6, p7, p8))
323 #define DEFINE_FUNC_ALIGNED9(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8, t9) \
324 DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9)) \
325 DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)+ALS(p7)+ALS(p8)+ALS(p9)) \
326 DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6, p7, p8, p9))
330 #define DEFINE_FUNC_ALIGNED0(result, linkage, name) DEFINE_METHOD_LINKAGE0 (result, linkage, name)
331 #define DEFINE_FUNC_ALIGNED1(result, linkage, name, t1) DEFINE_METHOD_LINKAGE1 (result, linkage, name, (t1 p1) )
332 #define DEFINE_FUNC_ALIGNED2(result, linkage, name, t1, t2) DEFINE_METHOD_LINKAGE2 (result, linkage, name, (t1 p1, t2 p2) )
333 #define DEFINE_FUNC_ALIGNED3(result, linkage, name, t1, t2, t3) DEFINE_METHOD_LINKAGE3 (result, linkage, name, (t1 p1, t2 p2, t3 p3) )
334 #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) )
335 #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) )
336 #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) )
337 #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) )
338 #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) )
339 #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) )
340 #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) )
341 #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) )
345 ///////////////////////////////////////////////////////////
347 // BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE
349 // Defines a method that resolves the exported functions
350 // defined with DEFINE_METHOD or DEFINE_METHOD_LINKAGE.
351 // There must be a RESOLVE_METHOD or RESOLVE_METHOD_RENAME
352 // for each DEFINE_METHOD or DEFINE_METHOD_LINKAGE within this
353 // block. This block must be followed by an END_METHOD_RESOLVE.
355 #define BEGIN_METHOD_RESOLVE() \
357 virtual bool ResolveExports() \
360 #define END_METHOD_RESOLVE() \
364 ///////////////////////////////////////////////////////////
368 // Resolves a method from a dll
370 // method: Name of the method defined with DEFINE_METHOD
371 // or DEFINE_METHOD_LINKAGE
373 #define RESOLVE_METHOD(method) \
374 if (!m_dll->ResolveExport( #method , & m_##method##_ptr )) \
377 #define RESOLVE_METHOD_FP(method) \
378 if (!m_dll->ResolveExport( #method , & method##_ptr )) \
382 ///////////////////////////////////////////////////////////
384 // RESOLVE_METHOD_OPTIONAL
386 // Resolves a method from a dll. does not abort if the
389 // method: Name of the method defined with DEFINE_METHOD
390 // or DEFINE_METHOD_LINKAGE
393 #define RESOLVE_METHOD_OPTIONAL(method) \
394 m_dll->ResolveExport( #method , & m_##method##_ptr );
396 #define RESOLVE_METHOD_OPTIONAL_FP(method) \
397 method##_ptr = NULL; \
398 m_dll->ResolveExport( #method , & method##_ptr );
402 ///////////////////////////////////////////////////////////
404 // RESOLVE_METHOD_RENAME
406 // Resolves a method from a dll
408 // dllmethod: Name of the function exported from the dll
409 // method: Name of the method defined with DEFINE_METHOD
410 // or DEFINE_METHOD_LINKAGE
412 #define RESOLVE_METHOD_RENAME(dllmethod, method) \
413 if (!m_dll->ResolveExport( #dllmethod , & m_##method##_ptr )) \
416 #define RESOLVE_METHOD_RENAME_FP(dllmethod, method) \
417 if (!m_dll->ResolveExport( #dllmethod , & method##_ptr )) \
421 ////////////////////////////////////////////////////////////////////
423 // Example declaration of a dll wrapper class
425 // 1. Define a class with pure virtual functions with all functions
426 // exported from the dll. This is needed to use the IntelliSence
427 // feature of the Visual Studio Editor.
429 // class DllExampleInterface
432 // virtual void foo (unsigned int type, char* szTest)=0;
433 // virtual void bar (char* szTest, unsigned int type)=0;
436 // 2. Define a class, derived from DllDynamic and the previously defined
437 // interface class. Define the constructor of the class using the
438 // DECLARE_DLL_WRAPPER macro. Use the DEFINE_METHODX/DEFINE_METHOD_LINKAGEX
439 // macros to define the functions from the interface above, where X is number of
440 // parameters the function has. The function parameters
441 // have to be enclosed in parentheses. The parameter names have to be changed to px
442 // where x is the number on which position the parameter appears.
443 // Use the RESOLVE_METHOD/RESOLVE_METHOD_RENAME to do the actually resolve the functions
444 // from the dll when it's loaded. The RESOLVE_METHOD/RESOLVE_METHOD_RENAME have to
445 // be between the BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE block.
447 // class DllExample : public DllDynamic, DllExampleInterface
449 // DECLARE_DLL_WRAPPER(DllExample, special://xbmcbin/system/Example.dll)
450 // LOAD_SYMBOLS() // add this if you want to load debug symbols for the dll
451 // DEFINE_METHOD2(void, foo, (int p1, char* p2))
452 // DEFINE_METHOD_LINKAGE2(void, __stdcall, bar, (char* p1, int p2))
453 // DEFINE_METHOD_FP(void, foobar, (int type, char* szTest)) // No need to define this function in the
454 // // interface class, as it's a function pointer.
455 // // But its not recognised by IntelliSence
456 // BEGIN_METHOD_RESOLVE()
457 // RESOLVE_METHOD(foo)
458 // RESOLVE_METHOD_RENAME("_bar@8", bar)
459 // RESOLVE_METHOD_FP(foobar)
460 // END_METHOD_RESOLVE()
463 // The above macros will expand to a class that will look like this
465 // class DllExample : public DllDynamic, DllExampleInterface
468 // DllExample() : DllDynamic( "special://xbmcbin/system/Example.dll" ) {}
470 // virtual bool LoadSymbols() { return true; }
472 // typedef void (* foo_METHOD) ( int p1, char* p2 );
475 // virtual void foo( int p1, char* p2 )
477 // return m_foo(p1, p2);
480 // typedef void (__stdcall * bar_METHOD) ( char* p1, int p2 );
483 // virtual void bar( char* p1, int p2 )
485 // return m_bar(p1, p2);
488 // typedef void (* foobar_METHOD) (int type, char* szTest);
490 // foobar_METHOD foobar;
492 // virtual bool ResolveExports()
495 // m_dll->ResolveExport( "foo", (void**)& m_foo ) &&
496 // m_dll->ResolveExport( "_bar@8", (void**)& m_bar ) &&
497 // m_dll->ResolveExport( "foobar" , (void**)& foobar ) &&
503 // Usage of the class
507 // if (dll.IsLoaded())
509 // dll.foo(1, "bar");
514 ///////////////////////////////////////////////////////////
516 // Baseclass for a Dynamically loaded dll
517 // use the above macros to create a dll wrapper
523 DllDynamic(const CStdString
& strDllName
);
524 virtual ~DllDynamic();
526 virtual void Unload();
527 virtual bool IsLoaded() const { return m_dll
!=NULL
; }
529 bool EnableDelayedUnload(bool bOnOff
);
530 bool SetFile(const CStdString
& strDllName
);
531 const CStdString
&GetFile() const { return m_strDllName
; }
534 virtual bool ResolveExports()=0;
535 virtual bool LoadSymbols() { return false; }
537 LibraryLoader
* m_dll
;
538 CStdString m_strDllName
;