diff --git a/example/DllLoader/DllLoader.cpp b/example/DllLoader/DllLoader.cpp new file mode 100644 index 0000000..e3e8c44 --- /dev/null +++ b/example/DllLoader/DllLoader.cpp @@ -0,0 +1,70 @@ +#define WIN32_LEAN_AND_MEAN + +#include +#include +#include + +#include "../../MemoryModule.h" + +typedef int (*addNumberProc)(int, int); + +#define DLL_FILE "..\\..\\SampleDLL\\Debug\\SampleDLL.dll" + +void LoadFromFile(void) +{ + addNumberProc addNumber; + HINSTANCE handle = LoadLibrary(DLL_FILE); + if (handle == INVALID_HANDLE_VALUE) + return; + + addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers"); + printf("From file: %d\n", addNumber(1, 2)); + FreeLibrary(handle); +} + +void LoadFromMemory(void) +{ + FILE *fp; + unsigned char *data=NULL; + size_t size; + HMEMORYMODULE module; + addNumberProc addNumber; + + fp = fopen(DLL_FILE, "rb"); + if (fp == NULL) + { + printf("Can't open DLL file \"%s\".", DLL_FILE); + goto exit; + } + + fseek(fp, 0, SEEK_END); + size = ftell(fp); + data = (unsigned char *)malloc(size); + fseek(fp, 0, SEEK_SET); + fread(data, 1, size, fp); + fclose(fp); + + module = MemoryLoadLibrary(data, size); + if (module == NULL) + { + printf("Can't load library from memory.\n"); + goto exit; + } + + addNumber = (addNumberProc)MemoryGetProcAddress(module, "addNumbers"); + printf("From memory: %d\n", addNumber(1, 2)); + MemoryFreeLibrary(module); + +exit: + if (data) + free(data); +} + +int main(int argc, char* argv[]) +{ + LoadFromFile(); + printf("\n\n"); + LoadFromMemory(); + return 0; +} + diff --git a/example/DllLoader/DllLoader.vcproj b/example/DllLoader/DllLoader.vcproj new file mode 100644 index 0000000..e6e24dd --- /dev/null +++ b/example/DllLoader/DllLoader.vcproj @@ -0,0 +1,138 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/DllMemory.ncb b/example/DllMemory.ncb new file mode 100644 index 0000000..260e32a Binary files /dev/null and b/example/DllMemory.ncb differ diff --git a/example/DllMemory.sln b/example/DllMemory.sln new file mode 100644 index 0000000..ef94099 --- /dev/null +++ b/example/DllMemory.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SampleDLL", "SampleDLL\SampleDLL.vcproj", "{B293DAC4-5BCA-4413-9B7B-92CB56459875}" + ProjectSection(ProjectDependencies) = postProject + {D0226BB5-3A02-4C91-893A-F36567AED5C5} = {D0226BB5-3A02-4C91-893A-F36567AED5C5} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DllLoader", "DllLoader\DllLoader.vcproj", "{D0226BB5-3A02-4C91-893A-F36567AED5C5}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {B293DAC4-5BCA-4413-9B7B-92CB56459875}.Debug.ActiveCfg = Debug|Win32 + {B293DAC4-5BCA-4413-9B7B-92CB56459875}.Debug.Build.0 = Debug|Win32 + {B293DAC4-5BCA-4413-9B7B-92CB56459875}.Release.ActiveCfg = Release|Win32 + {B293DAC4-5BCA-4413-9B7B-92CB56459875}.Release.Build.0 = Release|Win32 + {D0226BB5-3A02-4C91-893A-F36567AED5C5}.Debug.ActiveCfg = Debug|Win32 + {D0226BB5-3A02-4C91-893A-F36567AED5C5}.Debug.Build.0 = Debug|Win32 + {D0226BB5-3A02-4C91-893A-F36567AED5C5}.Release.ActiveCfg = Release|Win32 + {D0226BB5-3A02-4C91-893A-F36567AED5C5}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/example/DllMemory.suo b/example/DllMemory.suo new file mode 100644 index 0000000..3548eed Binary files /dev/null and b/example/DllMemory.suo differ diff --git a/example/SampleDLL/SampleDLL.cpp b/example/SampleDLL/SampleDLL.cpp new file mode 100644 index 0000000..662efcd --- /dev/null +++ b/example/SampleDLL/SampleDLL.cpp @@ -0,0 +1,10 @@ +#include "SampleDLL.h" + +extern "C" { + +SAMPLEDLL_API int addNumbers(int a, int b) +{ + return a + b; +} + +} \ No newline at end of file diff --git a/example/SampleDLL/SampleDLL.h b/example/SampleDLL/SampleDLL.h new file mode 100644 index 0000000..b77f135 --- /dev/null +++ b/example/SampleDLL/SampleDLL.h @@ -0,0 +1,11 @@ +extern "C" { + +#ifdef SAMPLEDLL_EXPORTS +#define SAMPLEDLL_API __declspec(dllexport) +#else +#define SAMPLEDLL_API __declspec(dllimport) +#endif + +SAMPLEDLL_API int addNumbers(int a, int b); + +} \ No newline at end of file diff --git a/example/SampleDLL/SampleDLL.vcproj b/example/SampleDLL/SampleDLL.vcproj new file mode 100644 index 0000000..9a0b722 --- /dev/null +++ b/example/SampleDLL/SampleDLL.vcproj @@ -0,0 +1,137 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +