forked from thintux/omxplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicDll.h
497 lines (446 loc) · 17.6 KB
/
DynamicDll.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#pragma once
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "utils/StdString.h"
///////////////////////////////////////////////////////////
//
// DECLARE_DLL_WRAPPER
//
// Declares the constructor of the wrapper class.
// This must be followed by one or more
// DEFINE_METHODX/DEFINE_METHOD_LINKAGEX and
// one BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE block.
//
// classname: name of the wrapper class to construct
// dllname: file including path of the dll to wrap
#define DECLARE_DLL_WRAPPER(classname, dllname) \
XDECLARE_DLL_WRAPPER(classname,dllname)
#define XDECLARE_DLL_WRAPPER(classname, dllname) \
public: \
classname () : DllDynamic( dllname ) {}
///////////////////////////////////////////////////////////
//
// DECLARE_DLL_WRAPPER_TEMPLATE_BEGIN
//
// Declares the constructor of the wrapper class.
// The method SetFile(strDllName) can be used to set the
// dll of this wrapper.
// This must be followed by one or more
// DEFINE_METHODX/DEFINE_METHOD_LINKAGEX and
// one BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE block.
//
// classname: name of the wrapper class to construct
//
#define DECLARE_DLL_WRAPPER_TEMPLATE(classname) \
public: \
classname () {} \
///////////////////////////////////////////////////////////
//
// LOAD_SYMBOLS
//
// Tells the dllloader to load Debug symblos when possible
#define LOAD_SYMBOLS() \
protected: \
virtual bool LoadSymbols() { return true; }
///////////////////////////////////////////////////////////
//
// DEFINE_GLOBAL
//
// Defines a global for export from the dll as well as
// a function for accessing it (Get_name).
//
// type: The variables type.
// name: Name of the variable.
//
#define DEFINE_GLOBAL_PTR(type, name) \
protected: \
union { \
type* m_##name; \
void* m_##name##_ptr; \
}; \
public: \
virtual type* Get_##name (void) \
{ \
return m_##name; \
}
#define DEFINE_GLOBAL(type, name) \
protected: \
union { \
type* m_##name; \
void* m_##name##_ptr; \
}; \
public: \
virtual type Get_##name (void) \
{ \
return *m_##name; \
}
///////////////////////////////////////////////////////////
//
// DEFINE_METHOD_LINKAGE
//
// Defines a function for an export from a dll, if the
// calling convention is not __cdecl.
// Use DEFINE_METHOD_LINKAGE for each function to be resolved.
//
// result: Result of the function
// linkage: Calling convention of the function
// name: Name of the function
// args: Arguments of the function, enclosed in parentheses
//
#define DEFINE_METHOD_LINKAGE_FP(result, linkage, name, args) \
protected: \
typedef result (linkage * name##_METHOD) args; \
public: \
union { \
name##_METHOD name; \
void* name##_ptr; \
};
#define DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, args2) \
protected: \
typedef result (linkage * name##_METHOD) args; \
union { \
name##_METHOD m_##name; \
void* m_##name##_ptr; \
}; \
public: \
virtual result name args \
{ \
return m_##name args2; \
}
#define DEFINE_METHOD_LINKAGE0(result, linkage, name) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, () , ())
#define DEFINE_METHOD_LINKAGE1(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1))
#define DEFINE_METHOD_LINKAGE2(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2))
#define DEFINE_METHOD_LINKAGE3(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3))
#define DEFINE_METHOD_LINKAGE4(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4))
#define DEFINE_METHOD_LINKAGE5(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5))
#define DEFINE_METHOD_LINKAGE6(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6))
#define DEFINE_METHOD_LINKAGE7(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7))
#define DEFINE_METHOD_LINKAGE8(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8))
#define DEFINE_METHOD_LINKAGE9(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8, p9))
#define DEFINE_METHOD_LINKAGE10(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10))
#define DEFINE_METHOD_LINKAGE11(result, linkage, name, args) \
DEFINE_METHOD_LINKAGE_BASE(result, linkage, name, args, (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11))
///////////////////////////////////////////////////////////
//
// DEFINE_METHOD_FP
//
// Defines a function for an export from a dll as a fuction pointer.
// Use DEFINE_METHOD_FP for each function to be resolved. Functions
// defined like this are not listed by IntelliSence.
//
// result: Result of the function
// name: Name of the function
// args: Arguments of the function, enclosed in parentheses
// The parameter names can be anything
//
#define DEFINE_METHOD_FP(result, name, args) DEFINE_METHOD_LINKAGE_FP(result, __cdecl, name, args)
///////////////////////////////////////////////////////////
//
// DEFINE_METHODX
//
// Defines a function for an export from a dll.
// Use DEFINE_METHODX for each function to be resolved.
// where X is the number of parameter the function has.
//
// result: Result of the function
// name: Name of the function
// args: Arguments of the function, enclosed in parentheses
// The parameter names have to be renamed to px, where
// x is the number of the parameter
//
#define DEFINE_METHOD0(result, name) DEFINE_METHOD_LINKAGE0(result, __cdecl, name)
#define DEFINE_METHOD1(result, name, args) DEFINE_METHOD_LINKAGE1(result, __cdecl, name, args)
#define DEFINE_METHOD2(result, name, args) DEFINE_METHOD_LINKAGE2(result, __cdecl, name, args)
#define DEFINE_METHOD3(result, name, args) DEFINE_METHOD_LINKAGE3(result, __cdecl, name, args)
#define DEFINE_METHOD4(result, name, args) DEFINE_METHOD_LINKAGE4(result, __cdecl, name, args)
#define DEFINE_METHOD5(result, name, args) DEFINE_METHOD_LINKAGE5(result, __cdecl, name, args)
#define DEFINE_METHOD6(result, name, args) DEFINE_METHOD_LINKAGE6(result, __cdecl, name, args)
#define DEFINE_METHOD7(result, name, args) DEFINE_METHOD_LINKAGE7(result, __cdecl, name, args)
#define DEFINE_METHOD8(result, name, args) DEFINE_METHOD_LINKAGE8(result, __cdecl, name, args)
#define DEFINE_METHOD9(result, name, args) DEFINE_METHOD_LINKAGE9(result, __cdecl, name, args)
#define DEFINE_METHOD10(result, name, args) DEFINE_METHOD_LINKAGE10(result, __cdecl, name, args)
#define DEFINE_METHOD11(result, name, args) DEFINE_METHOD_LINKAGE11(result, __cdecl, name, args)
///////////////////////////////////////////////////////////
//
// DEFINE_FUNC_ALIGNED 0-X
//
// Defines a function for an export from a dll, wich
// require a aligned stack on function call
// Use DEFINE_FUNC_ALIGNED for each function to be resolved.
//
// result: Result of the function
// linkage: Calling convention of the function
// name: Name of the function
// args: Argument types of the function
//
// Actual function call will expand to something like this
// this will align the stack (esp) at the point of function
// entry as required by gcc compiled dlls, it is abit abfuscated
// to allow for different sized variables
//
// __int64 test(__int64 p1, char p2, char p3)
// {
// int o,s = ((sizeof(p1)+3)&~3)+((sizeof(p2)+3)&~3)+((sizeof(p3)+3)&~3);
// __asm mov [o],esp;
// __asm sub esp, [s];
// __asm and esp, ~15;
// __asm add esp, [s]
// m_test(p1, p2, p3); //return value will still be correct aslong as we don't mess with it
// __asm mov esp,[o];
// };
#define ALS(a) ((sizeof(a)+3)&~3)
#define DEFINE_FUNC_PART1(result, linkage, name, args) \
private: \
typedef result (linkage * name##_type)##args; \
union { \
name##_type m_##name; \
void* m_##name##_ptr; \
}; \
public: \
virtual result name##args
#define DEFINE_FUNC_PART2(size) \
{ \
int o,s = size; \
__asm { \
__asm mov [o], esp \
__asm sub esp, [s] \
__asm and esp, ~15 \
__asm add esp, [s] \
}
#define DEFINE_FUNC_PART3(name,args) \
m_##name##args; \
__asm { \
__asm mov esp,[o] \
} \
}
#define DEFINE_FUNC_ALIGNED0(result, linkage, name) \
DEFINE_FUNC_PART1(result, linkage, name, ()) \
DEFINE_FUNC_PART2(0) \
DEFINE_FUNC_PART3(name,())
#define DEFINE_FUNC_ALIGNED1(result, linkage, name, t1) \
DEFINE_FUNC_PART1(result, linkage, name, (t1 p1)) \
DEFINE_FUNC_PART2(ALS(p1)) \
DEFINE_FUNC_PART3(name,(p1))
#define DEFINE_FUNC_ALIGNED2(result, linkage, name, t1, t2) \
DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2)) \
DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)) \
DEFINE_FUNC_PART3(name,(p1, p2))
#define DEFINE_FUNC_ALIGNED3(result, linkage, name, t1, t2, t3) \
DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3)) \
DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)) \
DEFINE_FUNC_PART3(name,(p1, p2, p3))
#define DEFINE_FUNC_ALIGNED4(result, linkage, name, t1, t2, t3, t4) \
DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4)) \
DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)) \
DEFINE_FUNC_PART3(name,(p1, p2, p3, p4))
#define DEFINE_FUNC_ALIGNED5(result, linkage, name, t1, t2, t3, t4, t5) \
DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5)) \
DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)) \
DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5))
#define DEFINE_FUNC_ALIGNED6(result, linkage, name, t1, t2, t3, t4, t5, t6) \
DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6)) \
DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)) \
DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6))
#define DEFINE_FUNC_ALIGNED7(result, linkage, name, t1, t2, t3, t4, t5, t6, t7) \
DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7)) \
DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)+ALS(p7)) \
DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6, p7))
#define DEFINE_FUNC_ALIGNED8(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8) \
DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8)) \
DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)+ALS(p7)+ALS(p8)) \
DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6, p7, p8))
#define DEFINE_FUNC_ALIGNED9(result, linkage, name, t1, t2, t3, t4, t5, t6, t7, t8, t9) \
DEFINE_FUNC_PART1(result, linkage, name, (t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9)) \
DEFINE_FUNC_PART2(ALS(p1)+ALS(p2)+ALS(p3)+ALS(p4)+ALS(p5)+ALS(p6)+ALS(p7)+ALS(p8)+ALS(p9)) \
DEFINE_FUNC_PART3(name,(p1, p2, p3, p4, p5, p6, p7, p8, p9))
///////////////////////////////////////////////////////////
//
// BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE
//
// Defines a method that resolves the exported functions
// defined with DEFINE_METHOD or DEFINE_METHOD_LINKAGE.
// There must be a RESOLVE_METHOD or RESOLVE_METHOD_RENAME
// for each DEFINE_METHOD or DEFINE_METHOD_LINKAGE within this
// block. This block must be followed by an END_METHOD_RESOLVE.
//
#define BEGIN_METHOD_RESOLVE() \
protected: \
virtual bool ResolveExports() \
{ \
return (
#define END_METHOD_RESOLVE() \
1 \
); \
}
///////////////////////////////////////////////////////////
//
// RESOLVE_METHOD
//
// Resolves a method from a dll
//
// method: Name of the method defined with DEFINE_METHOD
// or DEFINE_METHOD_LINKAGE
//
#define RESOLVE_METHOD(method) \
m_dll->ResolveExport( #method , & m_##method##_ptr ) &&
#define RESOLVE_METHOD_FP(method) \
m_dll->ResolveExport( #method , & method##_ptr ) &&
///////////////////////////////////////////////////////////
//
// RESOLVE_METHOD_RENAME
//
// Resolves a method from a dll
//
// dllmethod: Name of the function exported from the dll
// method: Name of the method defined with DEFINE_METHOD
// or DEFINE_METHOD_LINKAGE
//
#define RESOLVE_METHOD_RENAME(dllmethod, method) \
m_dll->ResolveExport( #dllmethod , & m_##method##_ptr ) &&
#define RESOLVE_METHOD_RENAME_FP(dllmethod, method) \
m_dll->ResolveExport( #dllmethod , & method##_ptr ) &&
////////////////////////////////////////////////////////////////////
//
// Example declaration of a dll wrapper class
//
// 1. Define a class with pure virtual functions with all functions
// exported from the dll. This is needed to use the IntelliSence
// feature of the Visual Studio Editor.
//
// class DllExampleInterface
// {
// public:
// virtual void foo (unsigned int type, char* szTest)=0;
// virtual void bar (char* szTest, unsigned int type)=0;
// };
//
// 2. Define a class, derived from DllDynamic and the previously defined
// interface class. Define the constructor of the class using the
// DECLARE_DLL_WRAPPER macro. Use the DEFINE_METHODX/DEFINE_METHOD_LINKAGEX
// macros to define the functions from the interface above, where X is number of
// parameters the function has. The function parameters
// have to be enclosed in parentheses. The parameter names have to be changed to px
// where x is the number on which position the parameter appears.
// Use the RESOLVE_METHOD/RESOLVE_METHOD_RENAME to do the actually resolve the functions
// from the dll when it's loaded. The RESOLVE_METHOD/RESOLVE_METHOD_RENAME have to
// be between the BEGIN_METHOD_RESOLVE/END_METHOD_RESOLVE block.
//
// class DllExample : public DllDynamic, DllExampleInterface
// {
// DECLARE_DLL_WRAPPER(DllExample, special://xbmcbin/system/Example.dll)
// LOAD_SYMBOLS() // add this if you want to load debug symbols for the dll
// DEFINE_METHOD2(void, foo, (int p1, char* p2))
// DEFINE_METHOD_LINKAGE2(void, __stdcall, bar, (char* p1, int p2))
// DEFINE_METHOD_FP(void, foobar, (int type, char* szTest)) // No need to define this function in the
// // interface class, as it's a function pointer.
// // But its not recognised by IntelliSence
// BEGIN_METHOD_RESOLVE()
// RESOLVE_METHOD(foo)
// RESOLVE_METHOD_RENAME("_bar@8", bar)
// RESOLVE_METHOD_FP(foobar)
// END_METHOD_RESOLVE()
// };
//
// The above macros will expand to a class that will look like this
//
// class DllExample : public DllDynamic, DllExampleInterface
// {
// public:
// DllExample() : DllDynamic( "special://xbmcbin/system/Example.dll" ) {}
// protected:
// virtual bool LoadSymbols() { return true; }
// protected:
// typedef void (* foo_METHOD) ( int p1, char* p2 );
// foo_METHOD m_foo;
// public:
// virtual void foo( int p1, char* p2 )
// {
// return m_foo(p1, p2);
// }
// protected:
// typedef void (__stdcall * bar_METHOD) ( char* p1, int p2 );
// bar_METHOD m_bar;
// public:
// virtual void bar( char* p1, int p2 )
// {
// return m_bar(p1, p2);
// }
// protected:
// typedef void (* foobar_METHOD) (int type, char* szTest);
// public:
// foobar_METHOD foobar;
// protected:
// virtual bool ResolveExports()
// {
// return (
// m_dll->ResolveExport( "foo", (void**)& m_foo ) &&
// m_dll->ResolveExport( "_bar@8", (void**)& m_bar ) &&
// m_dll->ResolveExport( "foobar" , (void**)& foobar ) &&
// 1
// );
// }
// };
//
// Usage of the class
//
// DllExample dll;
// dll.Load();
// if (dll.IsLoaded())
// {
// dll.foo(1, "bar");
// dll.Unload();
// }
//
///////////////////////////////////////////////////////////
//
// Baseclass for a Dynamically loaded dll
// use the above macros to create a dll wrapper
//
class DllDynamic
{
public:
DllDynamic();
DllDynamic(const CStdString& strDllName);
virtual ~DllDynamic();
virtual bool Load();
virtual void Unload();
virtual bool IsLoaded() { return m_dll!=NULL; }
bool CanLoad();
bool EnableDelayedUnload(bool bOnOff);
bool SetFile(const CStdString& strDllName);
protected:
virtual bool ResolveExports()=0;
virtual bool LoadSymbols() { return false; }
bool m_DelayUnload;
void *m_dll;
CStdString m_strDllName;
};