mirror of
https://github.com/fancycode/MemoryModule
synced 2026-06-06 15:44:28 +00:00
Use new resource loading API
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <tchar.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
|
||||||
@@ -13,12 +14,31 @@ typedef int (*addNumberProc)(int, int);
|
|||||||
void LoadFromFile(void)
|
void LoadFromFile(void)
|
||||||
{
|
{
|
||||||
addNumberProc addNumber;
|
addNumberProc addNumber;
|
||||||
|
HRSRC resourceInfo;
|
||||||
|
DWORD resourceSize;
|
||||||
|
LPVOID resourceData;
|
||||||
|
_TCHAR buffer[100];
|
||||||
|
|
||||||
HINSTANCE handle = LoadLibrary(DLL_FILE);
|
HINSTANCE handle = LoadLibrary(DLL_FILE);
|
||||||
if (handle == NULL)
|
if (handle == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers");
|
addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers");
|
||||||
printf("From file: %d\n", addNumber(1, 2));
|
printf("From file: %d\n", addNumber(1, 2));
|
||||||
|
|
||||||
|
resourceInfo = FindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
|
||||||
|
printf("FindResource returned 0x%p\n", resourceInfo);
|
||||||
|
|
||||||
|
resourceSize = SizeofResource(handle, resourceInfo);
|
||||||
|
resourceData = LoadResource(handle, resourceInfo);
|
||||||
|
printf("Resource data: %ld bytes at 0x%p\n", resourceSize, resourceData);
|
||||||
|
|
||||||
|
LoadString(handle, 1, buffer, sizeof(buffer));
|
||||||
|
printf("String1: %s\n", buffer);
|
||||||
|
|
||||||
|
LoadString(handle, 20, buffer, sizeof(buffer));
|
||||||
|
printf("String2: %s\n", buffer);
|
||||||
|
|
||||||
FreeLibrary(handle);
|
FreeLibrary(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,8 +47,12 @@ void LoadFromMemory(void)
|
|||||||
FILE *fp;
|
FILE *fp;
|
||||||
unsigned char *data=NULL;
|
unsigned char *data=NULL;
|
||||||
size_t size;
|
size_t size;
|
||||||
HMEMORYMODULE module;
|
HMEMORYMODULE handle;
|
||||||
addNumberProc addNumber;
|
addNumberProc addNumber;
|
||||||
|
HMEMORYRSRC resourceInfo;
|
||||||
|
DWORD resourceSize;
|
||||||
|
LPVOID resourceData;
|
||||||
|
_TCHAR buffer[100];
|
||||||
|
|
||||||
fp = fopen(DLL_FILE, "rb");
|
fp = fopen(DLL_FILE, "rb");
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
@@ -44,16 +68,30 @@ void LoadFromMemory(void)
|
|||||||
fread(data, 1, size, fp);
|
fread(data, 1, size, fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
module = MemoryLoadLibrary(data);
|
handle = MemoryLoadLibrary(data);
|
||||||
if (module == NULL)
|
if (handle == NULL)
|
||||||
{
|
{
|
||||||
printf("Can't load library from memory.\n");
|
printf("Can't load library from memory.\n");
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
addNumber = (addNumberProc)MemoryGetProcAddress(module, "addNumbers");
|
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
|
||||||
printf("From memory: %d\n", addNumber(1, 2));
|
printf("From memory: %d\n", addNumber(1, 2));
|
||||||
MemoryFreeLibrary(module);
|
|
||||||
|
resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
|
||||||
|
printf("MemoryFindResource returned 0x%p\n", resourceInfo);
|
||||||
|
|
||||||
|
resourceSize = MemorySizeofResource(handle, resourceInfo);
|
||||||
|
resourceData = MemoryLoadResource(handle, resourceInfo);
|
||||||
|
printf("Memory resource data: %ld bytes at 0x%p\n", resourceSize, resourceData);
|
||||||
|
|
||||||
|
MemoryLoadString(handle, 1, buffer, sizeof(buffer));
|
||||||
|
printf("String1: %s\n", buffer);
|
||||||
|
|
||||||
|
MemoryLoadString(handle, 20, buffer, sizeof(buffer));
|
||||||
|
printf("String2: %s\n", buffer);
|
||||||
|
|
||||||
|
MemoryFreeLibrary(handle);
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
if (data)
|
if (data)
|
||||||
|
|||||||
Reference in New Issue
Block a user