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 8964551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:36:57+00:00 2026-06-15T16:36:57+00:00

For an application I’m making I want to intercept window messages from an external

  • 0

For an application I’m making I want to intercept window messages from an external process (much like the way spy++ does it). I figured out that I can use SetWinEventHook to do this.
This is the code I have.

    class Program
    {
    internal delegate void WinEventProc(IntPtr hWinEventHook, int iEvent, IntPtr hWnd, int      idObject, int idChild, int dwEventThread, int dwmsEventTime);
        [DllImport("user32.dll", SetLastError = true)]
        internal static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, SetWinEventHookFlags dwflags);
        //[DllImport("user32.dll", SetLastError = true)]
        internal static extern int UnhookWinEvent(IntPtr hWinEventHook);
      internal enum SetWinEventHookFlags
      {
          WINEVENT_INCONTEXT = 4,
          WINEVENT_OUTOFCONTEXT = 0,
          WINEVENT_SKIPOWNPROCESS = 2,
          WINEVENT_SKIPOWNTHREAD = 1
      } 
        static void Main(string[] args)
        {
            int threadId = 0x000306D4;
            int processId = 0x000306BC;
            WinEventProc listener = new WinEventProc(EventCallback);
   //setting the window hook and writing the result to the console
            Console.WriteLine(SetWinEventHook(1, 0x7fffffff, IntPtr.Zero, listener, processId, threadId, SetWinEventHookFlags.WINEVENT_INCONTEXT).ToString());
            Console.WriteLine("done");
            Console.ReadKey(); 
        }
        private static void EventCallback(IntPtr hWinEventHook, int iEvent, IntPtr hWnd, int idObject, int idChild, int dwEventThread, int dwmsEventTime)
        {
   //callback function, called when message is intercepted
            Console.WriteLine(hWnd.ToString());
        }
    }
   }

As far as I know I am using the correct P/Invoke declaration, but when I run this code the output is:

0
done

If successful, it should return an HWINEVENTHOOK value that identifies this event hook instance. If unsuccesful it returns 0. (according to microsoft’s website at least)
The thread and process both seem to be valid and running.

Could anyone help me to get any closer to where my problem is? I think it must be either something small or I am just using completely the wrong method to do what I want to do. Thanks in advance.

  • 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-06-15T16:36:58+00:00Added an answer on June 15, 2026 at 4:36 pm

    According to msdn

    For out-of-context events, the event is delivered on the same thread that called SetWinEventHook.

    I tried your example in a console app and as you mentioned no events were received. But on a winform the example works and the events are received. So, I think, this is because of single thread of execution in a console application.

    Put the SetWinEventHook call in a button click event and you should start receiving events in the callback.

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

Sidebar

Related Questions

Application is a C# .Net 3.5 WCF Service. I'd like during the build process
My application has one activity which starts two services but does not bind them.
Application-Stack: Rails3, CanCan, Devise, Shoulda I've got some nested Resources and want to test
Application I am developing does some kind of server-side authorization. Communication is done via
My application is not all to complicated. It retrieves a single entity from a
Application requests KML data through AJAX from server. This data is stored in javascript
Application running in django, apache, python and mysql: I want to create global variable
Application.Run(form); Actually I tried to call this from my project. I got this exception.
My application want to upload data to backend. Upload functionality available 2 places. One
Application.Run(new Main()); This line gives TypeInitializationException was unhandled after I switched from 3.5 to

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.