Jump to content
  • Advertisement
Sign in to follow this  
dustydoodoo

SetWindowHookEx()? (Having trouble with a lot of things)

This topic is 3414 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to use and understand how to use a keyboard hook. I need to use the hook just for one thread. But the big thing that I'm not able to get right is exporting functions from a DLL file. I'm trying to do this using the DEF file. But I just cant find any full clear example of writing and using a DEF file to export functions from a DLL. I will list a bunch of steps, which are from my knowledge, what is needed for setting a keyboard hook. (I won't make the hook do anything) -Build a dll containing the CALLBACK function for the keyboard hook. --Function declaration in header file being...
LRESULT CALLBACK keyboardProc(int code, WPARAM wParam, LPARAM lParam);

--Function definition in main file being...
LRESULT CALLBACK keyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
    return CallNextHookEx(NULL, code, wParam, lParam);
}


--When I build the DLL the DEF file comes out with this...
EXPORTS
    _ZSt3minIjERKT_S2_S2_ @1
    keyboardProc@12 @2


-Write a main.cpp file to call these functions --Include the DEF file in the linker? --main.cpp file containing...
#include <windows.h>
#include <iostream>

using namespace std;

void setHook();

HHOOK hhook;
int main()
{
    setHook();

    cin.get();

    UnhookWindowsHookEx(hhook);
    return 0;
}

void setHook()
{
    typedef LRESULT (CALLBACK* KEYPROCPTR)(int, WPARAM, LPARAM);

    KEYPROCPTR keyProcPtr = NULL;
    HMODULE hMod = NULL;
    
    hMod = LoadLibrary("Hook.dll");
    if(hMod == NULL)
        printf("hMod is NULL");

    keyProcPtr = (KEYPROCPTR)GetProcAddress(hMod, "keyboardProc @12");
    if(keyProcPtr == NULL)
        printf("keyProcPtr is NULL \n");

    hhook = SetWindowsHookEx(WH_KEYBOARD, keyProcPtr, hMod, 0);
    if(hhook == NULL)
        printf("hook is NULL \n");
}



EDITED SOURCE: Once I know that I can set a hook, I will set the hook to a specific window. I don't really have any idea what I am suppose to put in the second parameter of 'GetProcAddress' so I just put in the line from the DEF file. I'm sorry if everything is incredibly wrong. I know I don't have a perfect knowledge of window hooks and dynamic libraries. But any help would be very much appreciated. I'm lost on where to go to understand how to do this. [Edited by - dustydoodoo on January 15, 2009 3:11:34 AM]

Share this post


Link to post
Share on other sites
Advertisement
I was trying out a bunch of things in the DEF file, and I finally got something to happen. But once I ran the main file that uses it, a window came up and said "SetHook.exe is not a valid Win32 application." So is this a security type thing, and does it mean that I'm going in the right direction? Or is it because I horribly disfigured some code or something. After I took the DEF file out of the linker and hid the function in a comment, I was able to run it. Now what?

EDIT:

I guess it's not because of a security problem. As soon as I took the "LIBRARY" line out of the DEF file I could run it (But the hook still doesn't work)

LIBRARY Hook
EXPORTS
_ZSt3minIjERKT_S2_S2_

I'm not sure exactly what to put beside the "LIBRARY" line, is it suppose to be the full file name of the DLL (with extension type)? And when I build the DLL, it always gives that fist garbled up function, and two of them for some reason(there is only one function in my DLL). When I try and build the main.cpp file it always complains about the second function so I just take it out of the DEF file(even thought it looks like the right function to be exporting). I just have no idea what to do!


[Edited by - dustydoodoo on January 14, 2009 11:28:54 PM]

Share this post


Link to post
Share on other sites
keyProcPtr = (KEYPROCPTR)GetProcAddress(hMod, "keyboardProc @12");

Mangled function names don't work with GetProcAddress. That's where the DEF file comes into play. It tells the linker to insert the unmangled function name into the header of the dll.

keyProcPtr = (KEYPROCPTR)GetProcAddress(hMod, "keyboardProc");

If you're not familiar with dlls, you should familiarize yourself with them first before jumping into the deep end of the pool with hooks so to speak.

Jeff Richter's book "Programming Windows" (aka "Advanced Windows") is a good reference to get started with this kind of system programming.

Tutorials and samples can also be found on the www using google. The topic of windows hooks has also come up on these boards many times before. Try searching on SetWindowsHookEx, CallNextHookEx and so on.

If you're running Windows XP and just want to have the toy without understanding how it works, here's some code: keylog.zip. YMMV.

Share this post


Link to post
Share on other sites
So the DEF file is for building the DLL, not for putting in the library of the main file that is going to call the function in the DLL?

EDIT:
Actually never mind. You probably just want me to go find out myself.
EDIT2:
Excellent, I just found a great tutorial that explains everything about building DLLs. It's great that I tried to do this, it has made me learn a lot of new things.

[Edited by - dustydoodoo on January 15, 2009 9:54:06 PM]

Share this post


Link to post
Share on other sites
Yes, I would like you to investigate the subject for yourself. I think you'll learn more that way.

Here's a utility that should help you learn about dlls and exes: Dependency Walker 2.2.

And if you want to hook other processes, here's a utility that exposes more about them than the task manager: process explorer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!