Hook installieren



  • Hallo.
    Ich möchte einen Keyboard-Hook installieren (um ein paar tastenkombinationen wegen eines spieles umzuleiten.)
    leider scheitere ich schon am anfang:

    das programm an sich:

    /* main.cpp */
    #include <iostream>
    #include <cstdlib>
    
    #include <Windows.h>
    
    int main()
    {
    	const HINSTANCE dll_addr = LoadLibrary(TEXT("key_hook_dll.dll"));
    	const HOOKPROC proc_addr = (HOOKPROC)GetProcAddress(dll_addr, "keyboard_hook"); 
    	if(proc_addr == NULL)
    		std::cout << "fail: " << GetLastError() << std::endl;
    
    	const HHOOK function_addr = SetWindowsHookEx(WH_KEYBOARD_LL, proc_addr, dll_addr, 0);
    	if(function_addr == NULL)
    		std::cout << "fail: " << GetLastError() << std::endl;
    
    	system("PAUSE");
    	system("PAUSE");
    	UnhookWindowsHookEx(function_addr);
    	FreeLibrary(dll_addr);
    	system("PAUSE");
    }
    

    erzeugt:

    fail: 127
    fail: 1427
    [...]
    

    <a href= schrieb:

    http://msdn.microsoft.com/en-us/library/ms681382(v=vs.85).aspx">The specified procedure could not be found.

    falls es an der dll liegen sollte, die entsteht so:

    /*dllmain.h*/
    
    #include <Windows.h>
    
    #if defined(DLL_BUILD)
    #	define DLL_FUNCTION __declspec( dllexport )
    #else
    #	define DLL_FUNCTION __declspec( dllimport )
    #endif
    
    #if defined(__cplusplus)
    extern "C"
    {
    #endif
    
    DLL_FUNCTION 
    LRESULT CALLBACK keyboard_hook(
    	int nCode,
    	WPARAM wParam,
    	LPARAM lParam
    );
    
    #if defined(__cplusplus)
    }
    #endif
    
    /*dllmain.cpp*/
    #include <iostream>
    
    #define DLL_BUILD
    
    #include "main.h"
    
    DLL_FUNCTION 
    LRESULT CALLBACK keyboard_hook(
    	int nCode,
    	WPARAM wParam,
    	LPARAM lParam
    )
    {
    	std::cout << "hey!!!" << std::endl;
    	return LRESULT();
    }
    

    da das projekt der dll in der gleichen solution hinzugefügt wurde, wie der rest, liegt die dll auch im gleichen ordner wie die exe.
    der name enthälgt auch keine tippfehler, so weit ich das sehen kann 😉

    danke im voraus 🙂
    bb



  • Nimm mal das CALLBACK weg - ansonsten hast du nämlich diese @-Conventions (_keyboard_hook@12)...



  • danke - das war es 🙂
    wieso auch immer in den ganzen msdn-bsp. das callback mit drin steht...

    bye


Anmelden zum Antworten