Thread: Problem with invoke of hooks DLL

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    Problem with invoke of hooks DLL

    Hi!

    I know we've had dozens of discussions on Win32 hooks, but I couldn't get any of them to work for me simply because I couldn't compile the file that references the DLL.

    In the following case, I got an:
    error LNK2019: unresolved external symbol "__declspec(dllimport) int __stdcall InstallHook (struct HINSTANCE __ *)" (__imp_?InstallHook@@YGHP

    For now, here's what I came up with after copy-pasting from a few sources:

    the one thats supposed to compile to .EXE:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    extern __declspec (dllimport) LRESULT CALLBACK KbProc(int, 
    WPARAM, LPARAM);
    extern __declspec (dllimport) BOOL WINAPI InstallHook(HMODULE ahmod);
    extern __declspec (dllimport) void WINAPI RemoveHook(void);
    
    
    int main(int argc, char *argv[])
    {
      HMODULE hmod = LoadLibrary("Hooky.dll");
      InstallHook(hmod);
    
      system("pause");
      return 0;
    };
    The one that compiles to DLL:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    __declspec (dllexport) LRESULT CALLBACK KbProc(int, WPARAM, LPARAM);
    __declspec (dllexport) BOOL WINAPI InstallHook(HMODULE ahmod);
    __declspec (dllexport) void WINAPI RemoveHook(void);
    
    #pragma data_seg("shared_data")
    // Superglobals
    HHOOK hookdata = 0;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:shared_data,S")
    
    HMODULE hmodule = 0;
    HWND hwindow = 0;
    BOOL recycle = FALSE;
    BOOL running = FALSE;
    UINT timerid = 0;
    void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime);
    
    __declspec (dllexport) LRESULT CALLBACK KbProc(int code, WPARAM w, LPARAM l)
    {
      if ( code < 0 || code != HC_ACTION )
        return CallNextHookEx(hookdata,code,w,l);
    
      char buff[100];
    
      sprintf(buff,"Values are: %d %u %u\n", code, w, l);
    
      MessageBox(0, buff, "ok", 0);
    
      switch(w)
      {
      case NULL: break;
        default: return CallNextHookEx(hookdata,code,w,l);
      }
      return TRUE;
    }
    
    __declspec (dllexport) BOOL APIENTRY DllMain (HMODULE hmod,
                           DWORD reason,
                           LPVOID na)
    {
        switch (reason)
        {
          case DLL_PROCESS_ATTACH:
               hmodule = hmod;
            break;
    
          case DLL_PROCESS_DETACH:
            break;
    
          case DLL_THREAD_ATTACH:
            break;
    
          case DLL_THREAD_DETACH:
            break;
        }
    
        /* Returns TRUE on success, FALSE on failure */
        return TRUE;
    }
    
    __declspec (dllexport) BOOL WINAPI InstallHook(HMODULE ahmod)
    {
      if ( hookdata != 0 )
         return FALSE;
      hookdata = SetWindowsHookEx(WH_KEYBOARD, KbProc, (HINSTANCE) ahmod, 0);
      if ( !hookdata )
         MessageBox(0, TEXT("Hook failed."), TEXT("Error"), MB_ICONEXCLAMATION); 
      return hookdata != 0;
    }
    
    __declspec (dllexport) void WINAPI RemoveHook(void)
    {
      if ( hookdata != 0 )
      {
        UnhookWindowsHookEx(hookdata);
      }
    }
    
    void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
    {
    }
    Finally, the module definition file:
    Code:
    LIBRARY	Hooky
    
    EXPORTS
    KbProc
    InstallHook
    RemoveHook

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Remove the def file: it's not needed with __declspec (dllexport).

    Also, try remove the "extern" keyword in your import declaration

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. DLL Function / Load Library Problem
    By cboard_member in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2005, 10:11 AM
  3. C++ .NET - "inconsistent dll linkage" - What's that?
    By BrianK in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2004, 10:16 AM
  4. std::string vs char* DLL problem
    By aker_y3k in forum C++ Programming
    Replies: 13
    Last Post: 10-02-2002, 09:05 AM
  5. VCL and DLL class problem
    By borland_man in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 11:07 AM