-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathtestlib.cpp
More file actions
357 lines (301 loc) · 8.16 KB
/
testlib.cpp
File metadata and controls
357 lines (301 loc) · 8.16 KB
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
// File: testlib.cpp
// Brief: Sample Windows shared library / DLL implementation
// Author: Caio Rodrigues
//---------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <sstream>
#include <functional>
#include <windows.h>
#include "testlib.hpp"
#define WindbgTrace(text) \
{ std::stringstream ss; \
ss << __FILE__ << ":" << __LINE__ << ": <" << __FUNCTION__ << "> " \
<< text << std::endl; \
OutputDebugString(ss.str().c_str()); \
}
#define DbgTrace(text) \
{ std::cerr << __FILE__ << ":" << __LINE__ << ": <" << __FUNCTION__ << "> " \
<< text << std::endl; }
#define DbgDisp(expr) \
{ std::cerr << __FILE__ << ":" << __LINE__ << ": <" << __FUNCTION__ << "> " \
<< #expr << " = " << (expr) << std::endl; }
/** - DLL Entry point - main function of DLL which is executed when
the DLL is loaded by some process.
*/
extern "C" __declspec(dllexport)
BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID lpReserved)
{
std::string text =
std::string("DLL Loaded into the process => PID = ")
+ std::to_string(::GetCurrentProcessId());
WindbgTrace(text);
DbgTrace(text);
switch (reason)
{
case DLL_PROCESS_ATTACH:
WindbgTrace("DLL attached to process");
DbgTrace("DLL attached to process.");
break;
case DLL_PROCESS_DETACH:
WindbgTrace("=> DLL detached.");
DbgTrace("=> DLL attached");
break;
case DLL_THREAD_ATTACH:
WindbgTrace("DLL attached to thread");
DbgTrace("DLL detached to thread.");
break;
case DLL_THREAD_DETACH:
WindbgTrace("DLL detached from thread");
DbgTrace("DLL detached from thread.");
break;
}
return TRUE;
}
//================================================================//
// Linear algebra tools
namespace Linalg{
__declspec(dllexport)
double norm(const std::vector<double>& xs){
double sum = 0.0;
for(const auto& x : xs) sum += x * x;
return std::sqrt(sum);
}
__declspec(dllexport)
std::vector<double>
linTransform(double a, double b, std::vector<double>& xs){
std::vector<double> out(xs.size());
for(size_t i = 0; i < xs.size(); i++){
out[i] = a * xs[i] + b;
}
return out;
}
__declspec(dllexport)
std::ostream&
printVector(std::ostream& os, std::vector<double>& xs){
os << "[" << xs.size() << "]( ";
for(const auto& x: xs)
os << x << ", ";
return os << " )";
}
}
//=========== C-wrappers ---------------///
// Handler for double vector
using hVectorD = void*;
using pVectorD = std::vector<double>*;
/** C-wrapper for vector<double> constructor */
extern "C" __declspec(dllexport)
hVectorD
testlib_vectorD_make0(size_t n, double x){
return new std::vector<double>(n, x);
}
/** C-wrapper for range constructor */
extern "C" __declspec(dllexport)
hVectorD
testlib_vectorD_make1(size_t n, double array []){
return new std::vector<double>(array, array + n);
}
/** C-wrapper for setting elements of vector<double> */
extern "C" __declspec(dllexport)
void
testlib_vectorD_set(hVectorD hv, size_t n, double x){
reinterpret_cast<pVectorD>(hv)->operator[](n) = x;
}
/** C-wrapper for vector<double> destructor */
extern "C" __declspec(dllexport)
void
testlib_vectorD_delete(hVectorD hv){
delete reinterpret_cast<pVectorD>(hv);
}
/** C-wrapepr for Linalg::norm function */
extern "C" __declspec(dllexport)
double
testlib_vectorD_Linalg_norm(hVectorD hv){
return Linalg::norm(*reinterpret_cast<pVectorD>(hv));
}
extern "C" __declspec(dllexport)
void
testlib_vectorD_Linalg_printVector(const char* name, hVectorD hv){
std::cout << name << " = ";
Linalg::printVector(std::cout, *reinterpret_cast<pVectorD>(hv));
std::cout << std::endl;;
}
//==========>>> class SampleClass <<===========
SampleClass::SampleClass(const std::string& name)
: m_name(name), m_counter(0)
{
std::cout << " Instance created with name = " << m_name << std::endl;
}
/** Delegated constructor on right-hand-side */
SampleClass::SampleClass(): SampleClass("unnamed"){}
SampleClass::~SampleClass(){
std::string text = std::string("SampleClass => name = ") + m_name + " deleted";
DbgTrace(text);
}
std::string SampleClass::getName() const {
return m_name;
}
int SampleClass::SampleClass::get(){
return m_counter;
}
void SampleClass::set(int n){
std::cout << " Counter set to value = " << n << std::endl;
m_counter = n;
}
//======= C-interface of SampleClass ===========//
using hSampleClass = void*;
/** Nullable constructor zero-arg constructor */
extern "C" __declspec(dllexport)
hSampleClass
testlib_SampleClass_make0(){
return new SampleClass();
}
/** Other constructor */
extern "C" __declspec(dllexport)
hSampleClass
testlib_SampleClass_make1(const char* name){
return new SampleClass(name);
}
/** Destructor */
extern "C" __declspec(dllexport)
void
testlib_SampleClass_delete(hSampleClass hnd){
delete reinterpret_cast<SampleClass*>(hnd);
}
/** Wrapper for get method */
extern "C" __declspec(dllexport)
int
testlib_SampleClass_get(hSampleClass hnd){
return reinterpret_cast<SampleClass*>(hnd)->get();
}
/** Wrapper for set method */
extern "C" __declspec(dllexport)
void
testlib_SampleClass_set(hSampleClass hnd, int n){
return reinterpret_cast<SampleClass*>(hnd)->set(n);
}
extern "C" __declspec(dllexport)
const char*
testlib_SampleClass_getName(hSampleClass hnd){
return reinterpret_cast<SampleClass*>(hnd)->getName().c_str();
}
//========= Implementations of the interface class ===//
// Destructor
// InterfaceClass::~InterfaceClass(){}
class ImplementationA: public InterfaceClass
{
private:
std::string m_name;
public:
static constexpr const char* class_id = "ImplementationA";
ImplementationA(): m_name("Unammed-A"){ }
ImplementationA(const std::string& name)
: m_name(name){}
~ImplementationA(){
std::cout << " [INFO] ImplementationA deleted => name = "
<< m_name
<< " ; type = " << class_id
<< std::endl;
}
const char* getID() const {
return class_id;
}
void setName(const char* name) {
m_name = name;
}
const char* getName() {
return m_name.c_str();
}
};
class ImplementationB: public InterfaceClass
{
private:
std::string m_name;
public:
static constexpr const char* class_id = "ImplementationB";
ImplementationB(): m_name("Unammed-B"){ }
ImplementationB(const std::string& name)
: m_name(name){}
~ImplementationB(){
std::cout << " [INFO] ImplementationB deleted => name = "
<< m_name
<< " ; type = " << class_id
<< std::endl;
}
const char* getID() const {
return class_id;
}
void setName(const char* name) {
m_name = name;
}
const char* getName() {
return m_name.c_str();
}
};
EXPORT_C InterfaceClass*
teslib_InterfaceClass_factory(const char* class_id)
{
auto s = std::string(class_id);
if(s == "ImplementationA")
return new ImplementationA();
if(s == "ImplementationB")
return new ImplementationB();
return nullptr;
}
EXPORT_C void testlib_InterfaceClass_delete(InterfaceClass* hinst)
{
delete hinst;
}
EXPORT_C
const char* testlib_InterfaceClass_getID(InterfaceClass* hinst)
{
return hinst->getID();
}
EXPORT_C
void testlib_InterfaceClass_setName(InterfaceClass* hinst, const char* name)
{
hinst->setName(name);
}
EXPORT_C
const char* testlib_InterfaceClass_getName(InterfaceClass* hinst){
return hinst->getName();
}
///================================================//
// Class private to this compilation unit -
// cannot be accessed from any other file
namespace {
class _StaticObject{
public:
using Action = std::function<void ()>;
Action m_end;
_StaticObject(Action init, Action end)
: m_end(end)
{
init();
}
~_StaticObject(){ m_end(); }
};
auto initDLL = _StaticObject(
[]{
std::cout << " [StaticObject] => Initialize DLL"
<< std::endl;
},
[]{
std::cout << " [StaticObject] => Shutdown DLL"
<< std::endl;
});
}
/*** ===========>>>> run32dll Entry Points ================= */
extern "C" __declspec(dllexport)
void entryPoint1(HWND hwn, HINSTANCE hinst, LPSTR cmdLine, int nCmdShow){
DbgDisp(cmdLine);
OutputDebugString("Rudll32 called entryPoint1()");
MessageBoxA(NULL, "DLL ENTRY POINT", "Entry point 1", 0);
}
extern "C" __declspec(dllexport)
int main(){
std::cout << "Running program OK." << std::endl;
return 0;
}