Skip to content

Commit 1adaf0e

Browse files
committed
Add exception handling code to example.
1 parent bf23ebf commit 1adaf0e

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

example/DllLoader/DllLoader.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@
1212
#include "../../MemoryModule.h"
1313

1414
typedef int (*addNumberProc)(int, int);
15+
#ifdef _WIN64
16+
typedef void (*throwExceptionProc)(void);
17+
#endif
1518

1619
#define DLL_FILE TEXT("..\\SampleDLL\\SampleDLL.dll")
1720

1821
void LoadFromFile(void)
1922
{
2023
addNumberProc addNumber;
24+
#ifdef _WIN64
25+
throwExceptionProc throwException;
26+
#endif
2127
HRSRC resourceInfo;
2228
DWORD resourceSize;
2329
LPVOID resourceData;
@@ -43,6 +49,24 @@ void LoadFromFile(void)
4349
LoadString(handle, 20, buffer, sizeof(buffer));
4450
_tprintf(_T("String2: %s\n"), buffer);
4551

52+
#ifdef _WIN64
53+
throwException = (throwExceptionProc)GetProcAddress(handle, "throwException");
54+
if (!throwException) {
55+
_tprintf(_T("GetProcAddress(\"throwException\") returned NULL\n"));
56+
} else {
57+
try {
58+
throwException();
59+
_tprintf(_T("Should have caught exception.\n"));
60+
} catch (int e) {
61+
if (e != 42) {
62+
_tprintf(_T("Should have caught exception 42, got %d instead\n"), e);
63+
} else {
64+
_tprintf(_T("Caught exception %d\n"), e);
65+
}
66+
}
67+
}
68+
#endif
69+
4670
FreeLibrary(handle);
4771
}
4872

@@ -90,6 +114,9 @@ void LoadFromMemory(void)
90114
size_t size;
91115
HMEMORYMODULE handle;
92116
addNumberProc addNumber;
117+
#ifdef _WIN64
118+
throwExceptionProc throwException;
119+
#endif
93120
HMEMORYRSRC resourceInfo;
94121
DWORD resourceSize;
95122
LPVOID resourceData;
@@ -124,6 +151,24 @@ void LoadFromMemory(void)
124151
MemoryLoadString(handle, 20, buffer, sizeof(buffer));
125152
_tprintf(_T("String2: %s\n"), buffer);
126153

154+
#ifdef _WIN64
155+
throwException = (throwExceptionProc)MemoryGetProcAddress(handle, "throwException");
156+
if (!throwException) {
157+
_tprintf(_T("MemoryGetProcAddress(\"throwException\") returned NULL\n"));
158+
} else {
159+
try {
160+
throwException();
161+
_tprintf(_T("Should have caught exception.\n"));
162+
} catch (int e) {
163+
if (e != 42) {
164+
_tprintf(_T("Should have caught exception 42, got %d instead\n"), e);
165+
} else {
166+
_tprintf(_T("Caught exception %d\n"), e);
167+
}
168+
}
169+
}
170+
#endif
171+
127172
MemoryFreeLibrary(handle);
128173

129174
exit:

example/SampleDLL/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ set (sources
77
add_definitions (-DSAMPLEDLL_EXPORTS)
88
add_library (SampleDLL MODULE ${sources})
99
if (NOT MSVC)
10+
set_target_properties ("SampleDLL" PROPERTIES LINK_FLAGS "-static -lstdc++ -dynamic")
1011
set_target_properties ("SampleDLL" PROPERTIES PREFIX "")
1112
set_target_properties ("SampleDLL" PROPERTIES SUFFIX ".dll")
13+
else ()
14+
set (CMAKE_CXX_FLAGS "/EHs")
1215
endif ()

example/SampleDLL/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ endif
2727
OBJ = SampleDLL.o SampleDLL.res
2828

2929
SampleDLL.dll: $(OBJ)
30-
$(LINK) $(LDFLAGS) -o SampleDLL.dll $(OBJ)
30+
$(LINK) $(LDFLAGS) -static -lstdc++ -dynamic -o SampleDLL.dll $(OBJ)
3131

3232
%.o: %.cpp
3333
$(CXX) $(CFLAGS) -c $<

example/SampleDLL/SampleDLL.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ SAMPLEDLL_API int addNumbers(int a, int b)
77
return a + b;
88
}
99

10+
#ifdef _WIN64
11+
SAMPLEDLL_API void throwException(void)
12+
{
13+
throw 42;
14+
}
15+
#endif
16+
1017
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy