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

  • SEARCH
  • Home
  • 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 7594567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:27:02+00:00 2026-05-30T21:27:02+00:00

In a Delphi XE application I am trying to set up a global hook

  • 0

In a Delphi XE application I am trying to set up a global hook to monitor focus changes. The hook is created in a dll:

focusHook := SetWindowsHookEx( WH_CBT, @FocusHookProc, HInstance, 0 );
// dwThreadId (the last argument) set to 0 should create a global hook

In the same dll I have the hook procedure that posts a message to the host app window:

function FocusHookProc( code : integer; wParam: WPARAM; lParam: LPARAM ) : LResult; stdcall;
begin
  if ( code < 0 ) then
  begin
    result := CallNextHookEx( focusHook, code, wParam, lParam );
    exit;
  end;

  result := 0;

  if ( code = HCBT_SETFOCUS ) then
  begin
    if ( hostHWND <> INVALID_HANDLE_VALUE ) then
      PostMessage( hostHWND, cFOCUSMSGID, wParam, lParam );
  end;
end;

This works, but the host only receives notifications on focus changes within the application itself. There is a memo and a few TButtons on the main form, and switching focus between them produces the expected message. However, any focus changes outside the application itself are never reported.

I suppose it has something to do with multiple instances of the DLL getting injected into other processes. There is a similar question with an accepted reply here, but it is for C, and I can’t quite see how I can do the same in a Delphi dll (e.g. the pragma statements to set up shared memory).

(This is mostly a proof of concept, but I’d still like to get it to work. I need to know what window was active just before my app got activated by way of clicking, alt+tab, activation hotkey etc. The problem is that if the mouse or alt+tab is used, GetForegroundWindow always returns my own app’s window handle, no matter how early I put it, such as by hooking the application’s main message queue. So the hook seems like the only viable solution, though I don’t really like the idea.)

  • 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-30T21:27:04+00:00Added an answer on May 30, 2026 at 9:27 pm

    Since the DLL is injected into another process, you’re not going to get any breakpoints hit for anything other than the process you’re debugging. Also, each instance of the DLL in the other process also get their own global/static data. If hostHWND is a global, it won’t be the same value in the other process as it is in this one. In fact it won’t even get initialized. You need to use a shared memory block to share values among the processes. Shared mutexes and other synchronization objects may need to be used to ensure any shared memory writes are protected. Finally, if you’re using Windows Vista+, only processes with the same access level and below will get the DLL injected. IOW, if you’re running the process as the logged-in user, only processed running as the logged-in user will get that DLL injected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Delphi application similar to Taskbar Shuffle that includes a hook dll.
I am trying to call LoadLibrary and load Delphi dll in ASP.NET application. On
I am trying to get the window handles to a Delphi application from an
I'm trying to write a Delphi console application that creates a TStream for its
Description I'm trying to test application coded in Delphi (VCL components by DevEx) with
I'm trying to squeeze every bit of performance in my Delphi application and now
im trying to send a Windows message to my Delphi application, but im having
While trying to modernize the appearance of an old C++Builder / Delphi application, I
I'm was trying to write a dll library in Delphi wih a function that
I am trying to debug an application in Delphi 2007 on a Vista machine.

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.