# GetPidFromEnumProcesses

<pre><code><strong>#include &#x3C;psapi.h>
</strong>
DWORD GetPidFromEnumProcessesW(_In_ PWCHAR ProcessNameWithExtension)
{
	HANDLE hProcess = NULL;

	DWORD ProcessIdArray[1024] = { 0 };
	DWORD ProcessIdArraySize = 0;
	DWORD NumberOfBytesReturned = 0;

	if (!K32EnumProcesses(ProcessIdArray, sizeof(ProcessIdArray), &#x26;NumberOfBytesReturned))
		return FALSE;

	ProcessIdArraySize = NumberOfBytesReturned / sizeof(DWORD);

	for (DWORD dwIndex = 0; dwIndex &#x3C; ProcessIdArraySize; dwIndex++)
	{
		HMODULE Module = NULL;
		DWORD dwProcessId = ERROR_SUCCESS;
		WCHAR ProcessStringName[MAX_PATH * sizeof(WCHAR)] = {0};

		if (ProcessIdArray[dwIndex] == 0)
			continue;

		hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ProcessIdArray[dwIndex]);
		if (hProcess == NULL)
			continue;

		if (!K32EnumProcessModules(hProcess, &#x26;Module, sizeof(Module), &#x26;NumberOfBytesReturned))
			continue;

		if (K32GetModuleBaseNameW(hProcess, Module, ProcessStringName, sizeof(ProcessStringName) / sizeof(WCHAR)) == 0)
			continue;

		if (StringCompareW(ProcessNameWithExtension, ProcessStringName) == 0)
			dwProcessId = GetProcessId(hProcess);

		CloseHandle(hProcess);

		if (dwProcessId != 0)
			return dwProcessId;
	}

	return 0;
}

DWORD GetPidFromEnumProcessesA(_In_ PCHAR ProcessNameWithExtension)
{
	HANDLE hProcess = NULL;

	DWORD ProcessIdArray[1024] = { 0 };
	DWORD ProcessIdArraySize = 0;
	DWORD NumberOfBytesReturned = 0;

	if (!K32EnumProcesses(ProcessIdArray, sizeof(ProcessIdArray), &#x26;NumberOfBytesReturned))
		return FALSE;

	ProcessIdArraySize = NumberOfBytesReturned / sizeof(DWORD);

	for (DWORD dwIndex = 0; dwIndex &#x3C; ProcessIdArraySize; dwIndex++)
	{
		HMODULE Module = NULL;
		DWORD dwProcessId = ERROR_SUCCESS;
		CHAR ProcessStringName[MAX_PATH] = { 0 };

		if (ProcessIdArray[dwIndex] == 0)
			continue;

		hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ProcessIdArray[dwIndex]);
		if (hProcess == NULL)
			continue;

		if (!K32EnumProcessModules(hProcess, &#x26;Module, sizeof(Module), &#x26;NumberOfBytesReturned))
			continue;

		if (K32GetModuleBaseNameA(hProcess, Module, ProcessStringName, sizeof(ProcessStringName) / sizeof(CHAR)) == 0)
			continue;

		if (StringCompareA(ProcessNameWithExtension, ProcessStringName) == 0)
			dwProcessId = GetProcessId(hProcess);

		CloseHandle(hProcess);

		if (dwProcessId != 0)
			return dwProcessId;
	}

	return 0;
}
</code></pre>


---

# 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/fingerprinting/getpidfromenumprocesses.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.
