#include "ProcessInjector.h"
#include <stdio.h>

VOID HandleInjection(LPSTR lpszProcessName, DWORD dwProcessId)
{
	Sleep(1000);
	if (!strcmp(lpszProcessName, "notepad.exe"))// || !strcmp(lpszProcessName, "iexplore.exe"))
	{
		PINJECT_PROCESS_PARAMETERS params = malloc(sizeof(INJECT_PROCESS_PARAMETERS));
		SecureZeroMemory(params, sizeof(INJECT_PROCESS_PARAMETERS));
		params->dwProcessId = dwProcessId;
	
		CHAR* buf[1024];
		SecureZeroMemory(buf, 1024);
		_snprintf_s(buf, 1024, 1024, "[*] injecting %s %d\n", lpszProcessName, dwProcessId);
		OutputDebugString(buf);

		//CreateThread(NULL, 0, InjectProcess, params, 0, NULL);
		InjectProcess(params);
	}
	Sleep(1000);

}

VOID* WINAPI InjectProcess(LPVOID lpParams)
{
	PINJECT_PROCESS_PARAMETERS params = (PINJECT_PROCESS_PARAMETERS)lpParams;
	PRUNTIME pRuntime = params->pRuntime;
	DWORD dwProcessId = params->dwProcessId;

	PIPC_PARAMS pIPC = NULL;
	HANDLE dwThreadHandle = NULL;

	if (loader_inject_proc(dwProcessId, &pIPC))
	{
		Sleep(3000);
		HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);

		PTHREAD_PARAMS pThreadParams = (PTHREAD_PARAMS)malloc(sizeof(THREAD_PARAMS));
		pThreadParams->hProcess = hProcess;
		pThreadParams->pIPC = pIPC;
		
		dwThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)IPCWorkerThread, pThreadParams, 0, NULL);
	}

	return dwThreadHandle;
}