mirror of
https://github.com/fancycode/MemoryModule
synced 2026-06-06 15:44:28 +00:00
Use "SetLastError" to flag errors to caller while loading.
This commit is contained in:
+8
-18
@@ -253,9 +253,7 @@ BuildImportTable(PMEMORYMODULE module)
|
|||||||
FARPROC *funcRef;
|
FARPROC *funcRef;
|
||||||
HCUSTOMMODULE handle = module->loadLibrary((LPCSTR) (codeBase + importDesc->Name), module->userdata);
|
HCUSTOMMODULE handle = module->loadLibrary((LPCSTR) (codeBase + importDesc->Name), module->userdata);
|
||||||
if (handle == NULL) {
|
if (handle == NULL) {
|
||||||
#if DEBUG_OUTPUT
|
SetLastError(ERROR_MOD_NOT_FOUND);
|
||||||
OutputLastError("Can't load library");
|
|
||||||
#endif
|
|
||||||
result = 0;
|
result = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -263,6 +261,7 @@ BuildImportTable(PMEMORYMODULE module)
|
|||||||
tmp = (HCUSTOMMODULE *) realloc(module->modules, (module->numModules+1)*(sizeof(HCUSTOMMODULE)));
|
tmp = (HCUSTOMMODULE *) realloc(module->modules, (module->numModules+1)*(sizeof(HCUSTOMMODULE)));
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
module->freeLibrary(handle, module->userdata);
|
module->freeLibrary(handle, module->userdata);
|
||||||
|
SetLastError(ERROR_OUTOFMEMORY);
|
||||||
result = 0;
|
result = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -292,6 +291,7 @@ BuildImportTable(PMEMORYMODULE module)
|
|||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
module->freeLibrary(handle, module->userdata);
|
module->freeLibrary(handle, module->userdata);
|
||||||
|
SetLastError(ERROR_PROC_NOT_FOUND);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -341,17 +341,13 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
|
|||||||
|
|
||||||
dos_header = (PIMAGE_DOS_HEADER)data;
|
dos_header = (PIMAGE_DOS_HEADER)data;
|
||||||
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) {
|
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) {
|
||||||
#if DEBUG_OUTPUT
|
SetLastError(ERROR_BAD_EXE_FORMAT);
|
||||||
OutputDebugString("Not a valid executable file.\n");
|
|
||||||
#endif
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
old_header = (PIMAGE_NT_HEADERS)&((const unsigned char *)(data))[dos_header->e_lfanew];
|
old_header = (PIMAGE_NT_HEADERS)&((const unsigned char *)(data))[dos_header->e_lfanew];
|
||||||
if (old_header->Signature != IMAGE_NT_SIGNATURE) {
|
if (old_header->Signature != IMAGE_NT_SIGNATURE) {
|
||||||
#if DEBUG_OUTPUT
|
SetLastError(ERROR_BAD_EXE_FORMAT);
|
||||||
OutputDebugString("No PE header found.\n");
|
|
||||||
#endif
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -370,18 +366,14 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
|
|||||||
MEM_RESERVE | MEM_COMMIT,
|
MEM_RESERVE | MEM_COMMIT,
|
||||||
PAGE_READWRITE);
|
PAGE_READWRITE);
|
||||||
if (code == NULL) {
|
if (code == NULL) {
|
||||||
#if DEBUG_OUTPUT
|
SetLastError(ERROR_OUTOFMEMORY);
|
||||||
OutputLastError("Can't reserve memory");
|
|
||||||
#endif
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result = (PMEMORYMODULE)HeapAlloc(GetProcessHeap(), 0, sizeof(MEMORYMODULE));
|
result = (PMEMORYMODULE)HeapAlloc(GetProcessHeap(), 0, sizeof(MEMORYMODULE));
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
#if DEBUG_OUTPUT
|
SetLastError(ERROR_OUTOFMEMORY);
|
||||||
OutputLastError("Can't reserve memory");
|
|
||||||
#endif
|
|
||||||
VirtualFree(code, 0, MEM_RELEASE);
|
VirtualFree(code, 0, MEM_RELEASE);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -432,9 +424,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data,
|
|||||||
// notify library about attaching to process
|
// notify library about attaching to process
|
||||||
successfull = (*DllEntry)((HINSTANCE)code, DLL_PROCESS_ATTACH, 0);
|
successfull = (*DllEntry)((HINSTANCE)code, DLL_PROCESS_ATTACH, 0);
|
||||||
if (!successfull) {
|
if (!successfull) {
|
||||||
#if DEBUG_OUTPUT
|
SetLastError(ERROR_DLL_INIT_FAILED);
|
||||||
OutputDebugString("Can't attach library.\n");
|
|
||||||
#endif
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
result->initialized = 1;
|
result->initialized = 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user