Skip to content

Commit 6276f44

Browse files
committed
Support imports by ordinal value.
Code based on pull-request #28.
1 parent e5d4285 commit 6276f44

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

MemoryModule.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
584584
FARPROC MemoryGetProcAddress(HMEMORYMODULE module, LPCSTR name)
585585
{
586586
unsigned char *codeBase = ((PMEMORYMODULE)module)->codeBase;
587-
int idx=-1;
588-
DWORD i, *nameRef;
589-
WORD *ordinal;
587+
int idx;
590588
PIMAGE_EXPORT_DIRECTORY exports;
591589
PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY((PMEMORYMODULE)module, IMAGE_DIRECTORY_ENTRY_EXPORT);
592590
if (directory->Size == 0) {
@@ -602,20 +600,32 @@ FARPROC MemoryGetProcAddress(HMEMORYMODULE module, LPCSTR name)
602600
return NULL;
603601
}
604602

605-
// search function name in list of exported names
606-
nameRef = (DWORD *) (codeBase + exports->AddressOfNames);
607-
ordinal = (WORD *) (codeBase + exports->AddressOfNameOrdinals);
608-
for (i=0; i<exports->NumberOfNames; i++, nameRef++, ordinal++) {
609-
if (_stricmp(name, (const char *) (codeBase + (*nameRef))) == 0) {
610-
idx = *ordinal;
611-
break;
603+
if (HIWORD(name) == 0) {
604+
// load function by ordinal value
605+
if (LOWORD(name) < exports->Base) {
606+
SetLastError(ERROR_PROC_NOT_FOUND);
607+
return NULL;
612608
}
613-
}
614609

615-
if (idx == -1) {
616-
// exported symbol not found
617-
SetLastError(ERROR_PROC_NOT_FOUND);
618-
return NULL;
610+
idx = LOWORD(name) - exports->Base;
611+
} else {
612+
// search function name in list of exported names
613+
DWORD i;
614+
DWORD *nameRef = (DWORD *) (codeBase + exports->AddressOfNames);
615+
WORD *ordinal = (WORD *) (codeBase + exports->AddressOfNameOrdinals);
616+
idx = -1;
617+
for (i=0; i<exports->NumberOfNames; i++, nameRef++, ordinal++) {
618+
if (_stricmp(name, (const char *) (codeBase + (*nameRef))) == 0) {
619+
idx = *ordinal;
620+
break;
621+
}
622+
}
623+
624+
if (idx == -1) {
625+
// exported symbol not found
626+
SetLastError(ERROR_PROC_NOT_FOUND);
627+
return NULL;
628+
}
619629
}
620630

621631
if ((DWORD)idx > exports->NumberOfFunctions) {

MemoryModule.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *,
6363
void *);
6464

6565
/**
66-
* Get address of exported method.
66+
* Get address of exported method. Supports loading both by name and by
67+
* ordinal value.
6768
*/
6869
FARPROC MemoryGetProcAddress(HMEMORYMODULE, LPCSTR);
6970

tests/LoadDll.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ BOOL LoadFromMemory(char *filename)
1616
size_t size;
1717
HMEMORYMODULE handle;
1818
addNumberProc addNumber;
19+
addNumberProc addNumber2;
1920
HMEMORYRSRC resourceInfo;
2021
DWORD resourceSize;
2122
LPVOID resourceData;
@@ -45,9 +46,31 @@ BOOL LoadFromMemory(char *filename)
4546
goto exit;
4647
}
4748

49+
addNumber = (addNumberProc)MemoryGetProcAddress(handle, NULL);
50+
if (addNumber != NULL) {
51+
_tprintf(_T("MemoryGetProcAddress(NULL) returned %p\n"), addNumber);
52+
result = FALSE;
53+
goto exit;
54+
}
55+
56+
addNumber = (addNumberProc)MemoryGetProcAddress(handle, reinterpret_cast<LPCTSTR>(0xff));
57+
if (addNumber != NULL) {
58+
_tprintf(_T("MemoryGetProcAddress(0xff) returned %p\n"), addNumber);
59+
result = FALSE;
60+
goto exit;
61+
}
62+
4863
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
4964
_tprintf(_T("From memory: %d\n"), addNumber(1, 2));
5065

66+
// the DLL only exports one function, try to load by ordinal value
67+
addNumber2 = (addNumberProc)MemoryGetProcAddress(handle, reinterpret_cast<LPCTSTR>(0x01));
68+
if (addNumber != addNumber2) {
69+
_tprintf(_T("MemoryGetProcAddress(0x01) returned %p (expected %p)\n"), addNumber2, addNumber);
70+
result = FALSE;
71+
goto exit;
72+
}
73+
5174
resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
5275
_tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo);
5376

0 commit comments

Comments
 (0)