Alright, I've now tried swapping the FilterFunction to a dll file, I got it to run just to test it and it still did the exact same thing :/. So I went back to the old version. The problem is that the program for some reason seems to stop executing the code at a certain point. As soon as I set the hook to work, and hit the GetMessage line, it exits out of the loop and I can't get it back in, even by unsetting the hook. Here's the code:
main.cpp
Code:
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include "Grid.h"
using namespace std;
HHOOK hMouseHook;
HINSTANCE hInstance;
int mouseX = -1; //Keeps track of where the mouse was released at
int mouseY = -1; //Keeps track of where the mouse was released at
int maxClicks = 0;
__declspec(dllexport) LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
static int numClicks = 0;
if (pMouseStruct != NULL)
if(wParam == WM_LBUTTONDOWN)
{
numClicks++;
mouseX = pMouseStruct->pt.x;
mouseY = pMouseStruct->pt.y;
cout << mouseX;
cout << mouseY;
}
if(numClicks >= maxClicks)
{
numClicks = 0;
UnhookWindowsHookEx(hMouseHook);
cout << "Calling unhook";
return 0;
}
return CallNextHookEx(hMouseHook,
nCode,wParam,lParam);
}
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
void MainLoop()
{
bool exiting = false;
GridPlots* ResourceArea;
GridPlots* ReturnPath;
while(!exiting)
{
string input;
bool getInput = true;
bool mouseListener = false;
if(getInput)
{
cout << "Enter command or /c for a list of commands. >> ";
cin >> input;
}
if(input == "/c")
{
cout << "\nCommands: \n1) Set: The next mouse position clicked will set the grid at 0.0 to that point.\n";
cout << "\n2) SetGrid: All the next positions clicked on will be added to the grid in sets of 2 until Esc is pressed.\n";
cout << "\n3) SetReturn: Sets the return path until Esc is pressed.\n";
getInput = true;
if(mouseListener == true)
UnhookWindowsHookEx(hMouseHook);
mouseListener = false;
}
else if(input == "Set")
{
cout << "\nYou have 10 seconds to switch to browser.\n";
for(int l = 5; l >= 1; --l)
{
cout << "\n" << l << "...";
wait(1);
}
hMouseHook = SetWindowsHookEx (WH_MOUSE_LL,(HOOKPROC) MouseProc,hInstance,0);
mouseListener = true;
maxClicks = 1;
//Set the center grid spot
if(mouseX != -1 && mouseY != -1)
{
cout << "\nCenter point set at " << mouseX << " " << mouseY;
if(ResourceArea == 0)
{
ResourceArea = new GridPlots(mouseX, mouseY, 0);
getInput = true;
}
}
else
getInput = false;
}
else if(input == "SetGrid")
{
cout << "\nYou have 10 seconds to switch to browser.\n";
for(int l = 5; l >= 1; --l)
{
cout << "\n" << l << "...";
wait(1);
}
hMouseHook = SetWindowsHookEx (WH_MOUSE_LL,(HOOKPROC) MouseProc,hInstance,0);
mouseListener = true;
//Set the grid for the next few spots
getInput = false;
}
else if(input == "SetReturn")
{
cout << "\nYou have 10 seconds to switch to browser.\n";
for(int l = 5; l >= 1; --l)
{
cout << "\n" << l << "...";
wait(1);
}
hMouseHook = SetWindowsHookEx (WH_MOUSE_LL,(HOOKPROC) MouseProc,hInstance,0);
mouseListener = true;
//Set the return path for the next few spots
getInput = false;
}
else
{
getInput = true;
UnhookWindowsHookEx(hMouseHook);
mouseListener = false;
cout << "\nInvalid Entry.\n";
}
if(mouseListener == true)
{
MSG message;
/*********************************************************
ERROR IN HERE SOMEWHERE!!!!!!!!!!!!!!!!!!!!!!!!!!!
**********************************************************/
BOOL bRet;
cout << "??";
while( (bRet = GetMessage( &message, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&message);
DispatchMessage(&message);
}
}
}
}
}
DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
hInstance = GetModuleHandle(NULL);
if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
if (!hInstance) return 1;
MainLoop();
return 0;
}
int main(int argc, char** argv)
{
HANDLE hThread;
DWORD dwThread;
hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)
MyMouseLogger, (LPVOID) argv[0], 0, &dwThread);
if (hThread)
return WaitForSingleObject(hThread,INFINITE);
else return 1;
}
BTW: I have tried setting the mouseListener boolean to global, and setting it false in the filter function, but that didn't work lol.
Now, at once you type in Set , the program does a 5 second countdown, then turns on the hook. That works fine, then I wait for a single click globally. I record that information and then try to exit the hook listening process, however once I do exit (or don't idk) I don't know where the program execution is, it's still running, but not where I expect. I expected it to come back right into my main while(!exiting) loop. However it doesn't seem to do that. I have no idea where it is running at !! Please if anyone can help me to get the execution back on line after that point, I'd be soooo grateful. Thanks in advance!