Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dialog hang

Dialog hang

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++tutorial
4 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 6831394
    wrote on last edited by
    #1

    Hi all i am get guide from codeproject and codeguru for create dll WH_KEYBOARD. Code is here

    // Testdll.cpp : Defines the entry point for the DLL application.
    //

    #include "stdafx.h"
    #include "Testdll.h"

    #pragma data_seg(".HOOKDATA")//Shared data among all instances.
    HHOOK hook = NULL;
    HWND hwnd = NULL;
    #pragma data_seg()

    #pragma comment(linker, "/SECTION:.HOOKDATA,RWS")//linker directive

    HINSTANCE hinstance = NULL;

    BOOL APIENTRY DllMain( HANDLE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserved
    )
    {
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
    break;
    }

    hinstance = (HINSTANCE)hModule;
    hook = NULL;
    
    return TRUE;
    

    }

    Testdll_API void installhook(HWND h)
    {
    hook = NULL;
    hwnd = h;
    hook = SetWindowsHookEx(WH_KEYBOARD,hookproc,hinstance,NULL);
    if(hook==NULL)
    MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
    }
    Testdll_API void removehook()
    {
    UnhookWindowsHookEx(hook);
    }

    Testdll_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
    {
    if(ncode>=0)
    {
    if((lparam & 0x80000000) == 0x00000000)//Check whether key was pressed(not released).
    {
    hwnd = FindWindow("#32770","TestDialog");//Find application window handle
    PostMessage(hwnd,WM_USER+755,wparam,lparam);//Send info to app Window.
    }
    }
    return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
    }

    // Testdll.h : Defines the entry point for the DLL application.
    //

    #ifdef TESTDLL_EXPORTS
    #define TESTDLL_API __declspec(dllexport)
    #else
    #define TESTDLL_API __declspec(dllimport)
    #endif

    TESTDLL_API void installhook(HWND h);//This function installs the Keyboard hook.
    TESTDLL_API void removehook();//This function removes the previously installed hook.
    TESTDLL_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam);//This function is called when the keyboard is operated.

    It is compile successful. Now i am using this dll in testdlg.Then dialog hang.

    installhook(this->GetSafeHwnd());

    Can any one tell me where i am wromg. Plz help me

    M 1 Reply Last reply
    0
    • U User 6831394

      Hi all i am get guide from codeproject and codeguru for create dll WH_KEYBOARD. Code is here

      // Testdll.cpp : Defines the entry point for the DLL application.
      //

      #include "stdafx.h"
      #include "Testdll.h"

      #pragma data_seg(".HOOKDATA")//Shared data among all instances.
      HHOOK hook = NULL;
      HWND hwnd = NULL;
      #pragma data_seg()

      #pragma comment(linker, "/SECTION:.HOOKDATA,RWS")//linker directive

      HINSTANCE hinstance = NULL;

      BOOL APIENTRY DllMain( HANDLE hModule,
      DWORD ul_reason_for_call,
      LPVOID lpReserved
      )
      {
      switch (ul_reason_for_call)
      {
      case DLL_PROCESS_ATTACH:
      case DLL_THREAD_ATTACH:
      case DLL_THREAD_DETACH:
      case DLL_PROCESS_DETACH:
      break;
      }

      hinstance = (HINSTANCE)hModule;
      hook = NULL;
      
      return TRUE;
      

      }

      Testdll_API void installhook(HWND h)
      {
      hook = NULL;
      hwnd = h;
      hook = SetWindowsHookEx(WH_KEYBOARD,hookproc,hinstance,NULL);
      if(hook==NULL)
      MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
      }
      Testdll_API void removehook()
      {
      UnhookWindowsHookEx(hook);
      }

      Testdll_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
      {
      if(ncode>=0)
      {
      if((lparam & 0x80000000) == 0x00000000)//Check whether key was pressed(not released).
      {
      hwnd = FindWindow("#32770","TestDialog");//Find application window handle
      PostMessage(hwnd,WM_USER+755,wparam,lparam);//Send info to app Window.
      }
      }
      return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
      }

      // Testdll.h : Defines the entry point for the DLL application.
      //

      #ifdef TESTDLL_EXPORTS
      #define TESTDLL_API __declspec(dllexport)
      #else
      #define TESTDLL_API __declspec(dllimport)
      #endif

      TESTDLL_API void installhook(HWND h);//This function installs the Keyboard hook.
      TESTDLL_API void removehook();//This function removes the previously installed hook.
      TESTDLL_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam);//This function is called when the keyboard is operated.

      It is compile successful. Now i am using this dll in testdlg.Then dialog hang.

      installhook(this->GetSafeHwnd());

      Can any one tell me where i am wromg. Plz help me

      M Offline
      M Offline
      Mattias G
      wrote on last edited by
      #2

      Where does your application hang? At what function call? Have you tried setting a breakpoint and stepping through the code?

      U 1 Reply Last reply
      0
      • M Mattias G

        Where does your application hang? At what function call? Have you tried setting a breakpoint and stepping through the code?

        U Offline
        U Offline
        User 6831394
        wrote on last edited by
        #3

        Thanks for response and sorry for late from my side. I have try to debug and i found where my application is going to hang.

        installhook(this->GetSafeHwnd());

        M 1 Reply Last reply
        0
        • U User 6831394

          Thanks for response and sorry for late from my side. I have try to debug and i found where my application is going to hang.

          installhook(this->GetSafeHwnd());

          M Offline
          M Offline
          Mattias G
          wrote on last edited by
          #4

          It crashes when doing the actual function call? Hmmm ... Can you call anything in the DLL? Try calling a function returning void with no arguments, to eliminate problems with calling conventions and stuff like that. Does DllMain get called properly? /M

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups