#include "stdafx.h"
#include <Windows.h>
#include <detours.h>
#pragma comment(lib,"detours.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	if (argc < 2)
	{
		printf("Error, usage is %s + 'target application'. Just place detoured.dll into same dir as launced application (keep same name)'\r\n", argv[0]);
		return 1;
	}
	_TCHAR *prog = argv[1];
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	ZeroMemory(&si, sizeof(si));
	ZeroMemory(&pi, sizeof(pi));
	si.cb = sizeof(si);
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = SW_SHOW;
	if (!DetourCreateProcessWithDllEx(prog,
		NULL, NULL, NULL, TRUE,
		CREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED,
		NULL, NULL, &si, &pi,
		"detoured.dll", NULL))
	{
		MessageBox(0, L"failed", 0, 0);
		return 1;
	}
	else
	{
		ResumeThread(pi.hThread);
		WaitForSingleObject(pi.hProcess, INFINITE);
		CloseHandle(&si);
		CloseHandle(&pi);
	}
	return 0;
}