Maus Hook in DLL funzt nicht



  • Hallo !
    Ich möchte für einen 3-D Shooter einen No-Recoil-Hook schreiben.
    Das soll folgendermassen funktionieren.
    Beim drücken (und beim gedrückthalten der linken Maustaste) soll sich die Maus nach unten bewegen.
    Im Windows funktioniert das ja auch wunderbar aber im Spiel geht das mit der Maus nichtmehr ?? Hab leider keine Ahnung wieso 😕

    Hier meine DLL :

    // InputHook.cpp : Defines the initialization routines for the DLL.
    //
    
    #include "stdafx.h"
    #include <Winuser.h>
    #include <windows.h>
    #include "InputHook.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    #pragma data_seg ("Shared") 
    HWND hWindow = NULL ; 
    int oldMouseState = 0;
    long msecs = 0;
    long sleeptime = 5;
    #pragma data_seg () 
    
    // Weise den Compilern, den Abschnitt Shared als lesbar, 
    // beschreibbar und zur gemeinsamen Verwendung zu deklarieren - "RSW". 
    #pragma comment (linker, "/section:Shared,RWS")
    
    HINSTANCE hThisDLL;
    static HHOOK hHookKeyboard = NULL;
    static HHOOK hHookMouse = NULL;
    int newMouseState;
    int myTimer;
    
    BEGIN_MESSAGE_MAP(CInputHookApp, CWinApp)
    END_MESSAGE_MAP()
    ////////////////////////////////////////////////////////////////////////////////////
    LRESULT __declspec(dllexport)__stdcall CALLBACK KeyboardProc(int hook_code, WPARAM virtual_key_code, LPARAM keystroke_message_info)
    {
    	if ((hook_code == HC_ACTION) && (GetKeyEventType(keystroke_message_info) == 0)) //DOWN
    		switch(virtual_key_code) //defined in winuser.h
    		{
    		case VK_LEFT:
    			keybd_event( 0x5A, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );// Simulate a key press
    			break;
    		case VK_RIGHT:
    			keybd_event( 0x55, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );// Simulate a key press
    			break;
    		}
    	else if ((hook_code == HC_ACTION) && (GetKeyEventType(keystroke_message_info) == 1))	 //UP
    		switch(virtual_key_code) 
    		{
    		case VK_LEFT:
    			keybd_event( 0x5A, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    			break;
    		case VK_RIGHT:
    			keybd_event( 0x55, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    			break;
    		}
    	else if ((hook_code == HC_ACTION) && (GetKeyEventType(keystroke_message_info) == 2)) //REPEAT
    	{
    		switch(virtual_key_code) //defined in winuser.h
    		{
    		case VK_LEFT:
    			keybd_event( 0x5A, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );// Simulate a key press
    			break;
    		case VK_RIGHT:
    			keybd_event( 0x55, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );// Simulate a key press
    			break;
    		}
    	}
    return CallNextHookEx (hHookKeyboard,hook_code, virtual_key_code, keystroke_message_info );
    }
    
    LRESULT __declspec(dllexport)__stdcall CALLBACK LowLevelMouseProc(int hook_code, WPARAM message_identifier, LPARAM mouse_coordinates)
    {
    
    	if (hook_code == HC_ACTION)
    	{
    		newMouseState = GetAsyncKeyState(VK_LBUTTON);
    		if (newMouseState == -32767 && oldMouseState == 0) //DOWN
    		{
    			oldMouseState = -32767;
    			msecs = 0;
    		}
    		if (newMouseState == 0 && oldMouseState == -32767) //UP
    		{
    			oldMouseState = 0;
    			mouse_event(MOUSEEVENTF_MOVE,0,5,1,1);
    		}
    	}
    
    return CallNextHookEx (hHookMouse,hook_code, message_identifier, mouse_coordinates);
    }
    
    BOOL __declspec(dllexport)__stdcall InstallHookKeyboard()
    {
    	hHookKeyboard = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hThisDLL,0);
    	myTimer = SetTimer(NULL, NULL, 10, TimerProc);
    	return TRUE;
    }
    
    BOOL __declspec(dllexport)__stdcall InstallHookMouse()
    {
    	hHookMouse = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)LowLevelMouseProc,hThisDLL,0);
    	return TRUE;
    }
    
    BOOL __declspec(dllexport)__stdcall  UninstallHookKeyboard()
    {
    	KillTimer(NULL,myTimer);
         return UnhookWindowsHookEx(hHookKeyboard);
    }
    
    BOOL __declspec(dllexport)__stdcall  UninstallHookMouse()
    {
         return UnhookWindowsHookEx(hHookMouse);
    } 
    
    ////////////////////////////////////////////////////////////////////////////////////
    // CInputHookApp construction
    CInputHookApp::CInputHookApp(){}
    // The one and only CInputHookApp object
    CInputHookApp theApp;
    // CInputHookApp initialization
    
    BOOL CInputHookApp::InitInstance()
    {
    	AFX_MANAGE_STATE(AfxGetStaticModuleState());
    	hThisDLL=AfxGetInstanceHandle();
    	return TRUE;
    }
    BOOL CInputHookApp::ExitInstance()
    {
    	return TRUE;
    }
    
    int GetKeyEventType(LPARAM lParam)
    {
    	// Reference: WM_KEYDOWN on MSDN
    	if (lParam & 0x80000000) // check bit 31 for up/down
    		return 1; //UP
    	else
    	{
    		if (lParam & 0x40000000) // check bit 30 for previous up/down
    			return 2; // It was pressed down before this key-down event, so it's a key-repeat for sure
    		else
    			return 0; //DOWN
    	}
    }
    
    void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
    {
    	msecs++;
    	if(msecs >= sleeptime)
    	{
    		if (GetAsyncKeyState(VK_LBUTTON) != 0 && oldMouseState != 0)
    				mouse_event(MOUSEEVENTF_MOVE,0,5,1,1);
    		msecs = 0;
    	}
    }
    

    Und hier das wichtige aus dem Hauptprogramm:

    void CInstallhookDlg::OnOk() 
    {
    static HINSTANCE hinstDLL; 
    typedef BOOL (CALLBACK *inshook)(); 
    inshook instkbhook;
    inshook instmhook;
    hinstDLL = LoadLibrary((LPCTSTR) "InputHook.dll"); 
    instkbhook = (inshook)GetProcAddress(hinstDLL, "InstallHookKeyboard");
    instmhook = (inshook)GetProcAddress(hinstDLL, "InstallHookMouse"); 
    instkbhook();
    instmhook();
    }
    

    Kann mir da jemand weiterhelfen ??



  • Dein Subject klingt so, als würde es nur in einer DLL nicht funktionieren, sonst aber schon. Das ist wahrscheinlich nicht der Fall.

    Ich vermute mal, dass das Spiel DirectInput verwendet und dass dieses jegliche Hooks einfach ignoriert.



  • Mit dem billigsten Visual Basic Code funktioniert es auf jeden Fall.
    Ich hab gedacht ich machs halt so wie ichs überall gelesen hab in ner DLL damit es schön sauber ist und funktioniert aber Pustekuchen GEHT NET 😞
    Wenn ich die Api Funktionen direkt im Programm Aufruf und zwar mit dem guten alten VB dann klappts einwandfrei trotz Direct Input ^^

    echt rofl ^^



  • versuchs mal mit sendinput oder so anstatt mit keyboadevent
    dann musst du den fensterhandle des programms herausfinden (IS_VISIBLE)
    und so kannst du es dann direkt zum programm senden



  • Hab folgendes probiert :

    HWND TO = GetActiveWindow();
    SendMessage(TO,0,message_identifier,mouse_coordinates);
    

    Leider gehts immer noch nicht 😞

    Wenn ich im Fenstermodus bin dann funktioniert es auch im Spielfenster aber sobal das Spiel bzw. eine Map geladen ist und das Spiel losgeht funktioniert es nichtmehr.
    Aber das war ja vorher schon so

    Greetz



  • Hab jetzt nen LowLevelMouseHook genommen dann gings 🙂
    Greetz


Anmelden zum Antworten