Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7000921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:44:07+00:00 2026-05-27T20:44:07+00:00

I have a DLL that I use to hook the Notepad and catch keystrokes.

  • 0

I have a DLL that I use to hook the Notepad and catch keystrokes. All works well, but I’d like to have access to the KeyboardHook Callback from within my exe. So that everytime the Callback in the .dll is triggered, it passes it’s values to the .exe Is this possible?

** HOOK.dll (hookDll.cpp) **

#include "windows.h"

#pragma data_seg (".SHARED")  
     HHOOK keyboardHook = 0;  
     HINSTANCE g_hInstance = 0;  
#pragma data_seg()  

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

extern "C" __declspec(dllexport) LRESULT CALLBACK KeyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(0 > nCode)
    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);

    {
        MessageBox(NULL, L"Got Keyboard Event !", L"Event", 0);
    }

    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}

extern "C" __declspec(dllexport) bool InstallKeyboardHook(unsigned long threadID)
{
    keyboardHook = SetWindowsHookEx(WH_KEYBOARD, &KeyboardHook, g_hInstance, threadID);
    return true;
}

extern "C" __declspec(dllexport) bool UnInstallKeyboardHook()
{
    UnhookWindowsHookEx(keyboardHook);  
    return true;
}

BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved ) 
{     
switch (ul_reason_for_call){
    case DLL_PROCESS_ATTACH:
        g_hInstance  = (HINSTANCE) hModule;
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
}     
return TRUE; 
}

** Hook.exe (hook.h) **

#pragma once

#include "afxwin.h"

typedef bool(*InstallHook)(unsigned long);
typedef bool(*UnInstallHook)();
typedef LRESULT (*KeyboardHook)(int nCode, WPARAM wParam, LPARAM lParam);

LRESULT CALLBACK MyCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
    //Id like this to get called whenever the CALLBACK in the dll is called
    MessageBox(NULL, L"test", L"test", 0);
    return NULL;
}


class CTrayMeApp : public CWinApp
{
public:
CTrayMeApp(){};
~CTrayMeApp(){};

virtual BOOL InitInstance();
};

extern CTrayMeApp theApp;

** Hook.exe (hook.cpp) **

#include "hook.h"

CTrayMeApp theApp;

BOOL CTrayMeApp::InitInstance()
{
BOOL bReturn = FALSE;

CWinApp::InitInstance();

HMODULE hModule = LoadLibrary(_T("Hook.dll"));

//Get the function address which installs the keyboard events filter.
InstallHook fpKeyboardHook = (InstallHook) GetProcAddress(hModule,"InstallKeyboardHook");

//Install the Keyboard Event Hook (Filter).
fpKeyboardHook(GetTargetThreadIdFromWindow("Notepad", "Untitled - Notepad"));

//Install the Callback
KeyboardHook cpKeyboardHook = (KeyboardHook) GetProcAddress(hModule,"_KeyboardHook@12");

//OK, are callback is here, now how do we make use of it?
KeyboardHook(MyCallback); //of course this won't work, but it gives an idea of what I'm trying to do.

FreeLibrary(hModule);

Sleep(10000); //give us 10 seconds to test it out
return bReturn;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:44:08+00:00Added an answer on May 27, 2026 at 8:44 pm

    Inside your DLL:

    LRESULT CALLBACK (*myCallbackRef)(int nCode, WPARAM wParam, LPARAM lParam);
    
    extern "C" __declspec(dllexport) void InstallMyHook(LRESULT CALLBACK (*MyCallback)(int nCode, WPARAM wParam, LPARAM lParam))
    {
       myCallbackRef = MyCallback;
    }
    

    Use myCallbackRef with parameters inside KeyboardHook.

    In your program
    Call

    InstallMyHook(MyCallBack);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an application that works with MS Office and uses Microsoft.mshtml.dll. We use
I have a dll that is working well on my computer but doesn't work
I have a DLL that I can use to pull the following information about
I have a dll that was written in c++, I need to use this
I have a DLL that I want to profile.. I tried to use Very
I have an external (.Net) dll that I have to use for an application.
I have a .NET DLL containing functions that I can use in PHP. Though
Quick question. I have an app that use a native DLL through PInvoke, this
I have been writing DLL on C++, that will be use in C#. DLL
I have a solution in VS 2008 that creates a DLL. I then use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.