#define _WIN32_WINNT 0x0403

#include <iostream>
#include <windows.h>
#include <TlHelp32.h>


using namespace std;


void SimulateInput(unsigned VirtualKey )
{
UINT scan_code = MapVirtualKey( VirtualKey, 0 );

INPUT kbd;
             kbd.type       = INPUT_KEYBOARD;
             kbd.ki.wVk     = VirtualKey;
             kbd.ki.wScan   = scan_code;
             kbd.ki.dwFlags = 0;
             kbd.ki.time    = 0;
             kbd.ki.dwExtraInfo = 0;
             SendInput (1 , &kbd, sizeof(kbd)); // Taste drücken

             kbd.ki.dwFlags = KEYEVENTF_KEYUP;
             SendInput (1 , &kbd, sizeof(kbd)); // Taste loslassen
}



unsigned GetProcessId( string AppName )
{
bool ProcessExists = false;
HANDLE snap;
PROCESSENTRY32 lppe;
ZeroMemory(&lppe,sizeof(PROCESSENTRY32));
snap =  CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
        if ( snap == INVALID_HANDLE_VALUE )
        {

            cout<<"Konnte kein Snapshot machen Fehler code: " << GetLastError() << endl;
            system("PAUSE");
            return 0;
        }

        lppe.dwSize = sizeof(PROCESSENTRY32);
        Process32First(  snap, &lppe );

        do
        {
                 if( ! strcmp(lppe.szExeFile, AppName.c_str()  ) )
                 {
                   //cout<<lppe.szExeFile << "  Prozess ID: "  << lppe.th32ProcessID << endl;
                   ProcessExists = true;
                   break;
                }
        }
        while ( Process32Next ( snap , &lppe ) == 1 ) ;

        if ( ProcessExists == false )
        {
              return 0;
        }
return lppe.th32ProcessID;
}


void TerminateBot(string ProcessName)
{
        unsigned PID = GetProcessId (ProcessName.c_str());
        if (PID == 0)
        {
            cout<<"Der Bot wurde nicht gestartet, error code: "<<GetLastError()<< "\n";
        }

        HANDLE h = OpenProcess(PROCESS_ALL_ACCESS,false,PID);
        if (PID == 0)
        {
            cout<<"Kann Prozess nicht oeffnen, error code: "<<GetLastError()<< "\n";
        }
        TerminateProcess( h,0);
}



DWORD WINAPI FehlerWegKlicken (void*)
{
    while(1)
    {
        Sleep(100);
        HWND h = FindWindow(0,"Trial Version");
        if (h!=0)
        {
            SetForegroundWindow(h);
            Sleep(500);
            // Confirm trial message
            SimulateInput ( VK_RETURN );
        }
    }
    return 0;
}




int main(int argc, char* argv[])
{
    CreateThread(NULL, 0, FehlerWegKlicken, NULL, 0, NULL);


    while(1)
    {
        // Wait 10 Minutes
        Sleep(600000);

        TerminateBot("rofl.exe");
        Sleep(800);

        WinExec("C:\\GamingTools\\rofl.exe",0);
        Sleep(2000);

        HWND h = FindWindow(0,"Invalid license");
        SetForegroundWindow(h);
        Sleep(500);
        // Confirm invalid message
        SimulateInput ( VK_RETURN );


        h = FindWindow(0,"Trial Version");
        SetForegroundWindow(h);
        Sleep(500);
        // Confirm trial message
        SimulateInput ( VK_RETURN );
    }



return 0;
}


//---------------------------------------------------------------------------




