Skip to content

Commit a32f925

Browse files
committed
Removed some redundant definitions
1 parent b686271 commit a32f925

4 files changed

Lines changed: 47 additions & 75 deletions

File tree

MemoryModule/LoadDllMemoryApi.cpp

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#include "stdafx.h"
2+
#include <cstdlib>
23

3-
HMEMORYMODULE WINAPI LoadLibraryMemory(_In_ PVOID BufferAddress) {
4+
HMEMORYMODULE WINAPI LoadLibraryMemoryExW(
5+
_In_ PVOID BufferAddress,
6+
_In_ size_t Reserved,
7+
_In_opt_ LPCWSTR DllBaseName,
8+
_In_opt_ LPCWSTR DllFullName,
9+
_In_ DWORD Flags) {
410
HMEMORYMODULE hMemoryModule = nullptr;
5-
NTSTATUS status = LdrLoadDllMemory(&hMemoryModule, BufferAddress, 0);
11+
NTSTATUS status = LdrLoadDllMemoryExW(&hMemoryModule, nullptr, Flags, BufferAddress, Reserved, DllBaseName, DllFullName);
612
if (!NT_SUCCESS(status) || status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH) {
713
SetLastError(RtlNtStatusToDosError(status));
814
}
@@ -15,26 +21,46 @@ HMEMORYMODULE WINAPI LoadLibraryMemoryExA(
1521
_In_opt_ LPCSTR DllBaseName,
1622
_In_opt_ LPCSTR DllFullName,
1723
_In_ DWORD Flags) {
18-
HMEMORYMODULE hMemoryModule = nullptr;
19-
NTSTATUS status = LdrLoadDllMemoryExA(&hMemoryModule, nullptr, Flags, BufferAddress, Reserved, DllBaseName, DllFullName);
20-
if (!NT_SUCCESS(status) || status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH) {
21-
SetLastError(RtlNtStatusToDosError(status));
22-
}
23-
return hMemoryModule;
24+
LPWSTR _DllName = nullptr, _DllFullName = nullptr;
25+
size_t size;
26+
HMEMORYMODULE result = nullptr;
27+
HANDLE heap = NtCurrentPeb()->ProcessHeap;
28+
29+
do {
30+
if (DllBaseName) {
31+
size = strlen(DllBaseName) + 1;
32+
_DllName = (LPWSTR)RtlAllocateHeap(heap, 0, sizeof(WCHAR) * size);
33+
34+
if (!_DllName) {
35+
RtlNtStatusToDosError(STATUS_INSUFFICIENT_RESOURCES);
36+
break;
37+
}
38+
39+
mbstowcs_s(nullptr, _DllName, size, DllBaseName, size);
40+
}
41+
42+
if (DllFullName) {
43+
size = strlen(DllFullName) + 1;
44+
_DllFullName = (LPWSTR)RtlAllocateHeap(heap, 0, sizeof(WCHAR) * size);
45+
46+
if (!_DllFullName) {
47+
RtlNtStatusToDosError(STATUS_INSUFFICIENT_RESOURCES);
48+
break;
49+
}
50+
51+
mbstowcs_s(nullptr, _DllFullName, size, DllFullName, size);
52+
}
53+
54+
result = LoadLibraryMemoryExW(BufferAddress, 0, _DllName, _DllFullName, 0);
55+
} while (false);
56+
57+
RtlFreeHeap(heap, 0, _DllName);
58+
RtlFreeHeap(heap, 0, _DllFullName);
59+
return result;
2460
}
2561

26-
HMEMORYMODULE WINAPI LoadLibraryMemoryExW(
27-
_In_ PVOID BufferAddress,
28-
_In_ size_t Reserved,
29-
_In_opt_ LPCWSTR DllBaseName,
30-
_In_opt_ LPCWSTR DllFullName,
31-
_In_ DWORD Flags) {
32-
HMEMORYMODULE hMemoryModule = nullptr;
33-
NTSTATUS status = LdrLoadDllMemoryExW(&hMemoryModule, nullptr, Flags, BufferAddress, Reserved, DllBaseName, DllFullName);
34-
if (!NT_SUCCESS(status) || status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH) {
35-
SetLastError(RtlNtStatusToDosError(status));
36-
}
37-
return hMemoryModule;
62+
HMEMORYMODULE WINAPI LoadLibraryMemory(_In_ PVOID BufferAddress) {
63+
return LoadLibraryMemoryExW(BufferAddress, 0, nullptr, nullptr, 0);
3864
}
3965

4066
BOOL WINAPI FreeLibraryMemory(_In_ HMEMORYMODULE hMemoryModule) {

MemoryModule/Loader.cpp

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "stdafx.h"
2-
#include <cstdlib>
32

43
static NTSTATUS NTAPI LdrMapDllMemory(
54
_In_ HMEMORYMODULE ViewBase,
@@ -34,13 +33,6 @@ static NTSTATUS NTAPI LdrMapDllMemory(
3433
return STATUS_SUCCESS;
3534
}
3635

37-
NTSTATUS NTAPI LdrLoadDllMemory(
38-
_Out_ HMEMORYMODULE* BaseAddress,
39-
_In_ LPVOID BufferAddress,
40-
_In_ size_t Reserved) {
41-
return LdrLoadDllMemoryExW(BaseAddress, nullptr, 0, BufferAddress, Reserved, nullptr, nullptr);
42-
}
43-
4436
NTSTATUS NTAPI LdrLoadDllMemoryExW(
4537
_Out_ HMEMORYMODULE* BaseAddress,
4638
_Out_opt_ PVOID* LdrEntry,
@@ -198,33 +190,6 @@ NTSTATUS NTAPI LdrLoadDllMemoryExW(
198190
return status;
199191
}
200192

201-
NTSTATUS NTAPI LdrLoadDllMemoryExA(
202-
_Out_ HMEMORYMODULE* BaseAddress,
203-
_Out_opt_ PVOID* LdrEntry,
204-
_In_ DWORD dwFlags,
205-
_In_ LPVOID BufferAddress,
206-
_In_ size_t BufferSize,
207-
_In_opt_ LPCSTR DllName,
208-
_In_opt_ LPCSTR DllFullName) {
209-
LPWSTR _DllName = nullptr, _DllFullName = nullptr;
210-
size_t size;
211-
NTSTATUS status;
212-
if (DllName) {
213-
size = strlen(DllName) + 1;
214-
_DllName = new wchar_t[size];
215-
mbstowcs_s(nullptr, _DllName, size, DllName, size);
216-
}
217-
if (DllFullName) {
218-
size = strlen(DllFullName) + 1;
219-
_DllFullName = new wchar_t[size];
220-
mbstowcs_s(nullptr, _DllFullName, size, DllFullName, size);
221-
}
222-
status = LdrLoadDllMemoryExW(BaseAddress, LdrEntry, dwFlags, BufferAddress, BufferSize, _DllName, _DllFullName);
223-
delete[]_DllName;
224-
delete[]_DllFullName;
225-
return status;
226-
}
227-
228193
NTSTATUS NTAPI LdrUnloadDllMemory(_In_ HMEMORYMODULE BaseAddress) {
229194
PLDR_DATA_TABLE_ENTRY CurEntry;
230195
ULONG count = 0;
@@ -291,7 +256,7 @@ NTSTATUS NTAPI LdrUnloadDllMemory(_In_ HMEMORYMODULE BaseAddress) {
291256
return status;
292257
}
293258

294-
__declspec(noreturn)
259+
DECLSPEC_NORETURN
295260
VOID NTAPI LdrUnloadDllMemoryAndExitThread(_In_ HMEMORYMODULE BaseAddress, _In_ DWORD dwExitCode) {
296261
LdrUnloadDllMemory(BaseAddress);
297262
RtlExitUserThread(dwExitCode);

MemoryModule/Loader.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
#pragma once
22

3-
//Load dll from the provided buffer.
4-
NTSTATUS NTAPI LdrLoadDllMemory(
5-
_Out_ HMEMORYMODULE* BaseAddress, // Output module base address
6-
_In_ LPVOID BufferAddress, // Pointer to the dll file data buffer
7-
_In_ size_t Reserved // Reserved parameter, must be 0
8-
);
9-
103
#define MEMORY_FEATURE_SUPPORT_VERSION 0x00000001
114
#define MEMORY_FEATURE_MODULE_BASEADDRESS_INDEX 0x00000002 /* Windows8 and greater */
125
#define MEMORY_FEATURE_LDRP_HEAP 0x00000004
@@ -65,16 +58,6 @@ NTSTATUS NTAPI LdrLoadDllMemoryExW(
6558
_In_opt_ LPCWSTR DllFullName // Module file full path
6659
);
6760

68-
NTSTATUS NTAPI LdrLoadDllMemoryExA(
69-
_Out_ HMEMORYMODULE* BaseAddress,
70-
_Out_opt_ PVOID* LdrEntry,
71-
_In_ DWORD dwFlags,
72-
_In_ LPVOID BufferAddress,
73-
_In_ size_t Reserved,
74-
_In_opt_ LPCSTR DllName,
75-
_In_opt_ LPCSTR DllFullName
76-
);
77-
7861
//Unload modules previously loaded from memory
7962
NTSTATUS NTAPI LdrUnloadDllMemory(_In_ HMEMORYMODULE BaseAddress);
8063

MemoryModule/MemoryModulePP.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LoadLibraryMemoryExA
55
LoadLibraryMemoryExW
66
FreeLibraryMemory
77

8-
LdrLoadDllMemory
9-
LdrLoadDllMemoryExA
108
LdrLoadDllMemoryExW
119
LdrUnloadDllMemory
1210
LdrUnloadDllMemoryAndExitThread

0 commit comments

Comments
 (0)