mirror of
https://github.com/fancycode/MemoryModule
synced 2026-06-06 15:44:28 +00:00
Add exception handling code to example.
This commit is contained in:
@@ -12,12 +12,18 @@
|
|||||||
#include "../../MemoryModule.h"
|
#include "../../MemoryModule.h"
|
||||||
|
|
||||||
typedef int (*addNumberProc)(int, int);
|
typedef int (*addNumberProc)(int, int);
|
||||||
|
#ifdef _WIN64
|
||||||
|
typedef void (*throwExceptionProc)(void);
|
||||||
|
#endif
|
||||||
|
|
||||||
#define DLL_FILE TEXT("..\\SampleDLL\\SampleDLL.dll")
|
#define DLL_FILE TEXT("..\\SampleDLL\\SampleDLL.dll")
|
||||||
|
|
||||||
void LoadFromFile(void)
|
void LoadFromFile(void)
|
||||||
{
|
{
|
||||||
addNumberProc addNumber;
|
addNumberProc addNumber;
|
||||||
|
#ifdef _WIN64
|
||||||
|
throwExceptionProc throwException;
|
||||||
|
#endif
|
||||||
HRSRC resourceInfo;
|
HRSRC resourceInfo;
|
||||||
DWORD resourceSize;
|
DWORD resourceSize;
|
||||||
LPVOID resourceData;
|
LPVOID resourceData;
|
||||||
@@ -43,6 +49,24 @@ void LoadFromFile(void)
|
|||||||
LoadString(handle, 20, buffer, sizeof(buffer));
|
LoadString(handle, 20, buffer, sizeof(buffer));
|
||||||
_tprintf(_T("String2: %s\n"), buffer);
|
_tprintf(_T("String2: %s\n"), buffer);
|
||||||
|
|
||||||
|
#ifdef _WIN64
|
||||||
|
throwException = (throwExceptionProc)GetProcAddress(handle, "throwException");
|
||||||
|
if (!throwException) {
|
||||||
|
_tprintf(_T("GetProcAddress(\"throwException\") returned NULL\n"));
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
throwException();
|
||||||
|
_tprintf(_T("Should have caught exception.\n"));
|
||||||
|
} catch (int e) {
|
||||||
|
if (e != 42) {
|
||||||
|
_tprintf(_T("Should have caught exception 42, got %d instead\n"), e);
|
||||||
|
} else {
|
||||||
|
_tprintf(_T("Caught exception %d\n"), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
FreeLibrary(handle);
|
FreeLibrary(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,6 +114,9 @@ void LoadFromMemory(void)
|
|||||||
size_t size;
|
size_t size;
|
||||||
HMEMORYMODULE handle;
|
HMEMORYMODULE handle;
|
||||||
addNumberProc addNumber;
|
addNumberProc addNumber;
|
||||||
|
#ifdef _WIN64
|
||||||
|
throwExceptionProc throwException;
|
||||||
|
#endif
|
||||||
HMEMORYRSRC resourceInfo;
|
HMEMORYRSRC resourceInfo;
|
||||||
DWORD resourceSize;
|
DWORD resourceSize;
|
||||||
LPVOID resourceData;
|
LPVOID resourceData;
|
||||||
@@ -124,6 +151,24 @@ void LoadFromMemory(void)
|
|||||||
MemoryLoadString(handle, 20, buffer, sizeof(buffer));
|
MemoryLoadString(handle, 20, buffer, sizeof(buffer));
|
||||||
_tprintf(_T("String2: %s\n"), buffer);
|
_tprintf(_T("String2: %s\n"), buffer);
|
||||||
|
|
||||||
|
#ifdef _WIN64
|
||||||
|
throwException = (throwExceptionProc)MemoryGetProcAddress(handle, "throwException");
|
||||||
|
if (!throwException) {
|
||||||
|
_tprintf(_T("MemoryGetProcAddress(\"throwException\") returned NULL\n"));
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
throwException();
|
||||||
|
_tprintf(_T("Should have caught exception.\n"));
|
||||||
|
} catch (int e) {
|
||||||
|
if (e != 42) {
|
||||||
|
_tprintf(_T("Should have caught exception 42, got %d instead\n"), e);
|
||||||
|
} else {
|
||||||
|
_tprintf(_T("Caught exception %d\n"), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
MemoryFreeLibrary(handle);
|
MemoryFreeLibrary(handle);
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ set (sources
|
|||||||
add_definitions (-DSAMPLEDLL_EXPORTS)
|
add_definitions (-DSAMPLEDLL_EXPORTS)
|
||||||
add_library (SampleDLL MODULE ${sources})
|
add_library (SampleDLL MODULE ${sources})
|
||||||
if (NOT MSVC)
|
if (NOT MSVC)
|
||||||
|
set_target_properties ("SampleDLL" PROPERTIES LINK_FLAGS "-static -lstdc++ -dynamic")
|
||||||
set_target_properties ("SampleDLL" PROPERTIES PREFIX "")
|
set_target_properties ("SampleDLL" PROPERTIES PREFIX "")
|
||||||
set_target_properties ("SampleDLL" PROPERTIES SUFFIX ".dll")
|
set_target_properties ("SampleDLL" PROPERTIES SUFFIX ".dll")
|
||||||
|
else ()
|
||||||
|
set (CMAKE_CXX_FLAGS "/EHs")
|
||||||
endif ()
|
endif ()
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ endif
|
|||||||
OBJ = SampleDLL.o SampleDLL.res
|
OBJ = SampleDLL.o SampleDLL.res
|
||||||
|
|
||||||
SampleDLL.dll: $(OBJ)
|
SampleDLL.dll: $(OBJ)
|
||||||
$(LINK) $(LDFLAGS) -o SampleDLL.dll $(OBJ)
|
$(LINK) $(LDFLAGS) -static -lstdc++ -dynamic -o SampleDLL.dll $(OBJ)
|
||||||
|
|
||||||
%.o: %.cpp
|
%.o: %.cpp
|
||||||
$(CXX) $(CFLAGS) -c $<
|
$(CXX) $(CFLAGS) -c $<
|
||||||
|
|||||||
@@ -7,4 +7,11 @@ SAMPLEDLL_API int addNumbers(int a, int b)
|
|||||||
return a + b;
|
return a + b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN64
|
||||||
|
SAMPLEDLL_API void throwException(void)
|
||||||
|
{
|
||||||
|
throw 42;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user