mirror-ac/user/um/process.cpp

61 lines
1.5 KiB
C++
Raw Normal View History

2023-08-15 14:02:17 +02:00
#include "process.h"
#include "../common.h"
#include <iostream>
2023-08-15 14:27:21 +02:00
HANDLE usermode::process::GetHandleToProcessGivenName( std::string ProcessName )
2023-08-15 14:02:17 +02:00
{
std::wstring wide_process_name;
std::wstring target_process_name;
HANDLE process_snapshot_handle;
HANDLE process_handle;
PROCESSENTRY32 process_entry;
wide_process_name = std::wstring( ProcessName.begin(), ProcessName.end() );
process_snapshot_handle = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if ( process_snapshot_handle == INVALID_HANDLE_VALUE )
{
2023-08-15 14:27:21 +02:00
LOG_ERROR( "Failed to create snapshot of current running processes error: 0x%x", GetLastError() );
2023-08-15 14:02:17 +02:00
return INVALID_HANDLE_VALUE;
}
process_entry.dwSize = sizeof( PROCESSENTRY32 );
if ( !Process32First( process_snapshot_handle, &process_entry ) )
{
2023-08-15 14:27:21 +02:00
LOG_ERROR( "Failed to get the first process using Process32First error: 0x%x", GetLastError() );
2023-08-15 14:02:17 +02:00
CloseHandle( process_snapshot_handle );
return INVALID_HANDLE_VALUE;
}
do
{
process_handle = OpenProcess(
PROCESS_ALL_ACCESS,
FALSE,
process_entry.th32ProcessID
);
if ( process_handle == NULL )
{
2023-08-15 14:27:21 +02:00
LOG_ERROR( "OpenProcess failed with error 0x%x", GetLastError() );
2023-08-15 14:02:17 +02:00
continue;
}
target_process_name = std::wstring( process_entry.szExeFile );
if ( wide_process_name == target_process_name )
{
LOG_INFO( "Found target process" );
CloseHandle( process_snapshot_handle );
return process_handle;
}
} while ( Process32Next( process_snapshot_handle, &process_entry ) );
CloseHandle( process_snapshot_handle );
return INVALID_HANDLE_VALUE;
}