# ImplGetModuleHandle

A custom implementation of GetModuleHandle

```
HMODULE ImplGetModuleHandleA(_In_ LPCSTR lpModuleName)
{
	PPEB Peb = GetPeb();
	PLDR_MODULE Module = NULL;
	CHAR wDllName[64] = { 0 };
	PLIST_ENTRY Head = &Peb->LoaderData->InMemoryOrderModuleList;
	PLIST_ENTRY Next = Head->Flink;
	Module = (PLDR_MODULE)((PBYTE)Next - 16);

	while (Next != Head)
	{
		Module = (PLDR_MODULE)((PBYTE)Next - 16);
		if (Module->BaseDllName.Buffer != NULL)
		{
			ZeroMemory(wDllName, sizeof(wDllName));
			WCharStringToCharString(wDllName, Module->BaseDllName.Buffer, 64);

			CHAR InitialModuleName[256] = { 0 };
			CHAR IdentifiedModuleName[256] = { 0 };

			if (StringCopyA(InitialModuleName, (PCHAR)lpModuleName) == NULL)
				return NULL;

			if (StringCopyA(IdentifiedModuleName, wDllName) == NULL)
				return NULL;

			PCHAR ComparisonObject1 = CaplockStringA(InitialModuleName);
			PCHAR ComparisonObject2 = CaplockStringA(IdentifiedModuleName);


			if (StringCompareA(ComparisonObject1, ComparisonObject2) == 0)
				return (HMODULE)Module->BaseAddress;
		}

		Next = Next->Flink;
	}

	return NULL;
}

HMODULE ImplGetModuleHandleW(_In_ LPCWSTR lpModuleName)
{
	PPEB Peb = GetPeb();
	PLDR_MODULE Module = NULL;

	PLIST_ENTRY Head = &Peb->LoaderData->InMemoryOrderModuleList;
	PLIST_ENTRY Next = Head->Flink;
	Module = (PLDR_MODULE)((PBYTE)Next - 16);

	while (Next != Head)
	{
		Module = (PLDR_MODULE)((PBYTE)Next - 16);
		if (Module->BaseDllName.Buffer != NULL)
		{
			WCHAR InitialModuleName[256] = { 0 };
			WCHAR IdentifiedModuleName[256] = { 0 };

			if (StringCopyW(InitialModuleName, (PWCHAR)lpModuleName) == NULL)
				return NULL;

			if (StringCopyW(IdentifiedModuleName, Module->BaseDllName.Buffer) == NULL)
				return NULL;

			PWCHAR ComparisonObject1 = CaplockStringW(InitialModuleName);
			PWCHAR ComparisonObject2 = CaplockStringW(IdentifiedModuleName);

			if (StringCompareW(ComparisonObject1, ComparisonObject2) == 0)
				return (HMODULE)Module->BaseAddress;
		}

		Next = Next->Flink;
	}

	return NULL;
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://malwaresourcecode.com/home/wrappers-and-helpers/implgetmodulehandle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
