DllLoaderLoader.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #define WIN32_LEAN_AND_MEAN
  2. #ifndef _CRT_SECURE_NO_WARNINGS
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #endif
  5. #include <assert.h>
  6. #include <windows.h>
  7. #include <tchar.h>
  8. #include <stdio.h>
  9. #include <malloc.h>
  10. #include "../../MemoryModule.h"
  11. #define EXE_FILE TEXT("DllLoader.exe")
  12. int RunFromMemory(void)
  13. {
  14. FILE *fp;
  15. unsigned char *data=NULL;
  16. long size;
  17. size_t read;
  18. HMEMORYMODULE handle;
  19. int result = -1;
  20. fp = _tfopen(EXE_FILE, _T("rb"));
  21. if (fp == NULL)
  22. {
  23. _tprintf(_T("Can't open executable \"%s\"."), EXE_FILE);
  24. goto exit;
  25. }
  26. fseek(fp, 0, SEEK_END);
  27. size = ftell(fp);
  28. assert(size >= 0);
  29. data = (unsigned char *)malloc(size);
  30. assert(data != NULL);
  31. fseek(fp, 0, SEEK_SET);
  32. read = fread(data, 1, size, fp);
  33. assert(read == static_cast<size_t>(size));
  34. fclose(fp);
  35. handle = MemoryLoadLibrary(data, size);
  36. if (handle == NULL)
  37. {
  38. _tprintf(_T("Can't load library from memory.\n"));
  39. goto exit;
  40. }
  41. result = MemoryCallEntryPoint(handle);
  42. if (result < 0) {
  43. _tprintf(_T("Could not execute entry point: %d\n"), result);
  44. }
  45. MemoryFreeLibrary(handle);
  46. exit:
  47. free(data);
  48. return result;
  49. }
  50. int main()
  51. {
  52. return RunFromMemory();
  53. }