GeezerVison is showing github.com — Support imports by ordinal value. · coderup2u/MemoryModule@6276f44
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: MemoryModule.c +25-15Lines changed: 25 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -584,9 +584,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, | 584 | 584 | FARPROC MemoryGetProcAddress(HMEMORYMODULE module, LPCSTR name) | 585 | 585 | { | 586 | 586 | unsigned char *codeBase = ((PMEMORYMODULE)module)->codeBase; | 587 | | - int idx=-1; | 588 | | - DWORD i, *nameRef; | 589 | | - WORD *ordinal; | | 587 | + int idx; | 590 | 588 | PIMAGE_EXPORT_DIRECTORY exports; | 591 | 589 | PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY((PMEMORYMODULE)module, IMAGE_DIRECTORY_ENTRY_EXPORT); | 592 | 590 | if (directory->Size == 0) { | @@ -602,20 +600,32 @@ FARPROC MemoryGetProcAddress(HMEMORYMODULE module, LPCSTR name) | 602 | 600 | return NULL; | 603 | 601 | } | 604 | 602 |
| 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; | 612 | 608 | } | 613 | | - } | 614 | 609 |
| 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 | + } | 619 | 629 | } | 620 | 630 |
| 621 | 631 | if ((DWORD)idx > exports->NumberOfFunctions) { | |
Collapse file Copy file name to clipboardExpand all lines: MemoryModule.h +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,7 +63,8 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *, | 63 | 63 | void *); | 64 | 64 |
| 65 | 65 | /** | 66 | | - * Get address of exported method. | | 66 | + * Get address of exported method. Supports loading both by name and by | | 67 | + * ordinal value. | 67 | 68 | */ | 68 | 69 | FARPROC MemoryGetProcAddress(HMEMORYMODULE, LPCSTR); | 69 | 70 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/LoadDll.cpp +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ BOOL LoadFromMemory(char *filename) | 16 | 16 | size_t size; | 17 | 17 | HMEMORYMODULE handle; | 18 | 18 | addNumberProc addNumber; | | 19 | + addNumberProc addNumber2; | 19 | 20 | HMEMORYRSRC resourceInfo; | 20 | 21 | DWORD resourceSize; | 21 | 22 | LPVOID resourceData; | @@ -45,9 +46,31 @@ BOOL LoadFromMemory(char *filename) | 45 | 46 | goto exit; | 46 | 47 | } | 47 | 48 |
| | 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 | + | 48 | 63 | addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers"); | 49 | 64 | _tprintf(_T("From memory: %d\n"), addNumber(1, 2)); | 50 | 65 |
| | 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 | + | 51 | 74 | resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION); | 52 | 75 | _tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo); | 53 | 76 |
| |
|