How does C get the PID of a process

How does C get the PID of a process

Example of using the C program to get the windows process pid:

/* for Windows   
  how to obtain the PID of a process using C language
*/
#include <stdio.h>
#include <Windows.h>
#include <TlHelp32.h>
#include <string.h>
#include <wchar.h>

int
main(void)
{
	char processname[] = "ShellExperienceHost.exe";
	DWORD pid = 0;

	HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	PROCESSENTRY32 process;
	ZeroMemory(&process, sizeof(process));
	process.dwSize = sizeof(process);

	if (Process32First(snapshot, &process)) {
		do {
			if (wcscmp( processname, process.szExeFile) == 0) {
				pid = process.th32ProcessID;
				printf("pid: %ld",pid);
				break;
			}
		} while (Process32Next(snapshot, &process));
	}

	CloseHandle(snapshot);

    printf("pid: %ld",pid);
    printf("pid: %u",pid);
	return 0;
}
Source:‮w‬ww.lautturi.com
Created Time:2017-08-29 04:44:17  Author:lautturi