1// Literatur: Vorlesungsunterlagen ISE3 (FH-Hagenberg)
2
3
4/*********************************************************************/
5/* Projekt fuer dynamische Library (erzeugt dll.dll) */
6/* bei DevCpp wird libDLL.a und libDLL.def erzeugt, */
7/* bei VS sollte eine dll.def selbst angelegt werden */
8/* und dll.lib wird erzeugt */
9/*********************************************************************/
10
11// dll.h:
12//---------------------------------------------------------------------
13 #ifndef _DLL_H_
14 #define _DLL_H_
15
16 #if BUILDING_DLL
17 # define DLLIMPORT __declspec (dllexport)
18 #else
19 # define DLLIMPORT __declspec (dllimport)
20 #endif
21
22 extern DLLIMPORT int var;
23
24 DLLIMPORT int func(void);
25 // name mangling
26 DLLIMPORT int Add1(int const val1, int const val2);
27 // without name mangling
28 extern "C" DLLIMPORT int Add2(int const val1, int const val2);
29
30 class DLLIMPORT dllClass
31 {
32 public:
33 dllClass();
34 virtual ~dllClass(void);
35 };
36
37 #endif
38
39
40// dll.cpp:
41//---------------------------------------------------------------------
42 #include "dll.h"
43 #include <windows.h>
44
45 BOOL APIENTRY DllMain (HMODULE hModule,
46 DWORD ul_reason_for_call,
47 LPVOID lpReserved)
48 {
49 switch (ul_reason_for_call)
50 {
51 case DLL_PROCESS_ATTACH:
52 MessageBox(0, "DLL_PROCESS_ATTACH called", "DLL", MB_OK);
53 break;
54 case DLL_THREAD_ATTACH:
55 break;
56 case DLL_THREAD_DETACH:
57 break;
58 case DLL_PROCESS_DETACH:
59 MessageBox(0, "DLL_PROCESS_DETACH called", "DLL", MB_OK);
60 break;
61 }
62 return TRUE;
63 }
64
65
66 DLLIMPORT int var=0;
67
68 DLLIMPORT int func(void)
69 {
70 return 42;
71 }
72
73 DLLIMPORT int Add1(int const val1, int const val2)
74 {
75 return val1 + val2;
76 }
77
78 extern "C" DLLIMPORT int Add2(int const val1, int const val2)
79 {
80 return val1 + val2;
81 }
82
83 dllClass::dllClass()
84 {
85 MessageBox(0, "dllClass CTOR called", "DLL", MB_OK);
86 return;
87 }
88
89 dllClass::~dllClass()
90 {
91 MessageBox(0, "dllClass DTOR called", "DLL", MB_OK);
92 return;
93 }
94
95
96// dll.def (bei DevCpp nicht n�tig):
97//---------------------------------------------------------------------
98 LIBRARY "DLL"
99
100 DESCRIPTION 'My personal dll :-)'
101
102 EXPORTS
103 var
104 func
105 Add1
106 Add2
107
108
109
110/*********************************************************************/
111/* Projekt welches die Library verwendet */
112/* dll.h und libDLL.a/dll.lib werden ins aktuelle Projekt kopiert, */
113/* libDLL.a/dll.lib wird bei den Abhaengigkeiten des Linkers */
114/* eingetragen */
115/*********************************************************************/
116
117// testDll.h:
118//---------------------------------------------------------------------
119 #include <iostream>
120 #include <conio.h>
121 #include "dll.h"
122
123 using namespace std;
124
125 int main()
126 {
127 cout << "var = " << var << endl;
128 var = 3;
129 cout << "var = " << var << endl;
130
131 cout << "func() = " << func() << endl;
132
133 cout << "Add1(1,2) = " << Add1(1,2) << endl;
134 cout << "Add2(1,2) = " << Add2(1,2) << endl;
135
136 dllClass *cl = new dllClass();
137 delete cl;
138
139 _getch();
140 return 0;
141 }
142
143
144
145/*********************************************************************/
146/* Projekt welches die Library zur Laufzeit laedt */
147/*********************************************************************/
148
149// testDll2.h:
150//---------------------------------------------------------------------
151 #include <iostream>
152 #include <windows.h>
153 #include <conio.h>
154
155 using namespace std;
156
157 int main ()
158 {
159 // Funktionspointer gemaess der Schnittstelle
160 typedef int (* TAdd) (int const, int const);
161
162 // DLL dynamisch zur Laufzeit laden
163 HMODULE hDll = LoadLibrary("dll.dll");
164 if (hDll != 0)
165 {
166 // Funktionsadresse ermitteln
167 // via Namen
168 TAdd pAdd1 = (TAdd) GetProcAddress(hDll, "Add1");
169 // via Ordinalnummer
170 //TAdd pAdd1 = (TAdd) GetProcAddress(hDll, MAKEINTRESOURCE(3));
171 if (pAdd1 != 0)
172 cout << "Add1(1,2) = " << pAdd1(1,2) << endl;
173 else
174 cout << "Add1 not found (maybe name mangling)" << endl;
175
176 TAdd pAdd2 = (TAdd) GetProcAddress(hDll, "Add2");
177 if (pAdd2 != 0)
178 cout << "Add2(1,2) = " << pAdd2(1,2) << endl;
179 else
180 cout << "Add2 not found (maybe name mangling)" << endl;
181 }
182
183 // Clean up
184 FreeLibrary(hDll);
185 _getch();
186 return 0;
187 }