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
|
// This program tests out how the various objects can be loaded
// dynamically and method calls made on them.
#include "Magazine.h"
#include "ace/DLL.h"
#include "ace/Log_Msg.h"
#include <memory>
typedef Magazine* (*Magazine_Creator) (void);
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
ACE_UNUSED_ARG (argc);
ACE_UNUSED_ARG (argv);
ACE_DLL dll;
int retval = dll.open (ACE_DLL_PREFIX ACE_TEXT("DLL_Today"));
if (retval != 0)
ACE_ERROR_RETURN ((LM_ERROR,
"%p",
"dll.open"),
-1);
Magazine_Creator mc;
// Cast the void* to non-pointer type first - it's not legal to
// cast a pointer-to-object directly to a pointer-to-function.
void *void_ptr = dll.symbol (ACE_TEXT ("create_magazine"));
ptrdiff_t tmp = reinterpret_cast<ptrdiff_t> (void_ptr);
mc = reinterpret_cast<Magazine_Creator> (tmp);
if (mc == 0)
{
ACE_ERROR_RETURN ((LM_ERROR,
"%p",
"dll.symbol"),
-1);
}
{
std::unique_ptr<Magazine> magazine (mc ());
magazine->title ();
}
dll.close ();
// The other library is now loaded on demand.
retval = dll.open (ACE_DLL_PREFIX ACE_TEXT ("DLL_Newsweek"));
if (retval != 0)
{
ACE_ERROR_RETURN ((LM_ERROR,
"%p",
"dll.open"),
-1);
}
// Cast the void* to non-pointer type first - it's not legal to
// cast a pointer-to-object directly to a pointer-to-function.
void_ptr = dll.symbol (ACE_TEXT ("create_magazine"));
tmp = reinterpret_cast<ptrdiff_t> (void_ptr);
mc = reinterpret_cast<Magazine_Creator> (tmp);
if (mc == 0)
{
ACE_ERROR_RETURN ((LM_ERROR,
"%p",
"dll.symbol"),
-1);
}
{
std::unique_ptr<Magazine> magazine (mc ());
magazine->title ();
}
dll.close ();
return 0;
}
|