Fixes typos and function call errors on GetProcessFromArgv.c

This commit is contained in:
Lsec
2024-08-16 13:03:16 +03:00
committed by GitHub
parent 6d6226a020
commit 6c95d803d8
+34 -25
View File
@@ -4,37 +4,46 @@
#include <windows.h>
#include <tlhelp32.h>
DWORD GetProcessIdByName(const char * name)
DWORD GetProcessIdByName(IN const char* name, OUT HANDLE& hProcess)
{
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
char buf[MAX_PATH] = { 0 };
size_t charsConverted = 0;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
char buf[MAX_PATH] = { 0 };
size_t charsConverted = 0;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (snapshot == INVALID_HANDLE_VALUE)
{
printf("[-] Could not create snapshot. Error: %lu\n", GetLastError());
return NULL;
}
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
wcstombs_s(&charsConverted, buf, entry.szExeFile, MAX_PATH);
if (_stricmp(buf, name) == 0)
{
HANDLE ph = OpenProcess(PROCESS_ALL_ACCESS, FALSE, DWORD(entry.th32ProcessID));
if (ph != NULL)
{
printf("[+] Got handle on %d\n", entry.th32ProcessID);
return entry.th32ProcessID;
}
printf("[-] Could not obtain handle on %d. Continuing!\n", entry.th32ProcessID);
}
}
}
return NULL;
if (Process32First(snapshot, &entry) == TRUE)
{
do
{
wcstombs_s(&charsConverted, buf, MAX_PATH, entry.szExeFile, MAX_PATH - 1);
if (_stricmp(buf, name) == 0)
{
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
if (hProcess != NULL)
{
printf("[+] Got handle on %d\n", entry.th32ProcessID);
CloseHandle(snapshot); // Always close handles you no longer need
return entry.th32ProcessID;
}
printf("[-] Could not obtain handle on %d. Continuing!\n", entry.th32ProcessID);
}
} while (Process32Next(snapshot, &entry) == TRUE);
}
printf("[-] Process not found.\n");
CloseHandle(snapshot);
return NULL;
}
int main(int argc, char* argv[])
{
int pid = GetProcessIdByName(argv[1]);
printf("%d\n", pid);
}
}