Use new resource loading API

This commit is contained in:
Joachim Bauch
2013-03-12 00:44:17 +01:00
parent f4d15a53ac
commit 390efc33a1
+43 -5
View File
@@ -1,6 +1,7 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <malloc.h>
@@ -13,12 +14,31 @@ typedef int (*addNumberProc)(int, int);
void LoadFromFile(void)
{
addNumberProc addNumber;
HRSRC resourceInfo;
DWORD resourceSize;
LPVOID resourceData;
_TCHAR buffer[100];
HINSTANCE handle = LoadLibrary(DLL_FILE);
if (handle == NULL)
return;
addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers");
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);
}
@@ -27,8 +47,12 @@ void LoadFromMemory(void)
FILE *fp;
unsigned char *data=NULL;
size_t size;
HMEMORYMODULE module;
HMEMORYMODULE handle;
addNumberProc addNumber;
HMEMORYRSRC resourceInfo;
DWORD resourceSize;
LPVOID resourceData;
_TCHAR buffer[100];
fp = fopen(DLL_FILE, "rb");
if (fp == NULL)
@@ -44,16 +68,30 @@ void LoadFromMemory(void)
fread(data, 1, size, fp);
fclose(fp);
module = MemoryLoadLibrary(data);
if (module == NULL)
handle = MemoryLoadLibrary(data);
if (handle == NULL)
{
printf("Can't load library from memory.\n");
goto exit;
}
addNumber = (addNumberProc)MemoryGetProcAddress(module, "addNumbers");
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
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:
if (data)