본문 바로가기

Windows Programming/Windows API

윈도우 프로세스 목록 가져오기

CreateToolhelp32Snapshot API MSDN 주소

- https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot

 

CreateToolhelp32Snapshot function (tlhelp32.h) - Win32 apps

Takes a snapshot of the specified processes, as well as the heaps, modules, and threads used by these processes.

docs.microsoft.com

Process32First API MSDN 주소

- https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32first

 

Process32First function (tlhelp32.h) - Win32 apps

Retrieves information about the first process encountered in a system snapshot.

docs.microsoft.com

 

Process32Next API MSDN 주소

- https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32next

 

Process32Next function (tlhelp32.h) - Win32 apps

Retrieves information about the next process recorded in a system snapshot.

docs.microsoft.com

 

 

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <TlHelp32.h>

int main(void)
{
	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnapshot != INVALID_HANDLE_VALUE)
	{
		PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) };
		if (Process32First(hSnapshot, &pe32))
		{
			do
			{
				printf("PID : %6d, Process Name : %s\n", pe32.th32ProcessID, pe32.szExeFile);
			} while (Process32Next(hSnapshot, &pe32));
		}
		CloseHandle(hSnapshot);
	}

	return 0;
}