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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:53:53+00:00 2026-06-12T22:53:53+00:00

In my c# project I’m trying to intercept mouse clicks from another program, but

  • 0

In my c# project I’m trying to intercept mouse clicks from another program, but only the ones that come from a certain hwnd as well… I’ve read here that I can filter my messages using the lParam but I’ve not succeeded to cast it to something I can actually get Hwnd’s back from.

This is how I set up the global mouse hook:

SetWindowsHookEx(WH_MOUSE_LL, s_MouseDelegate, IntPtr.Zero, 0);

I then catch the messages in this function:

private static int MouseHookProc(int nCode, int wParam, IntPtr lParam)
{
    if (nCode >= 0)
    {
          switch (wParam)
          {
               case WM_LBUTTONDOWN:
                    mouseDown = true;
                    mouseUp = false;
                    break;
               case WM_LBUTTONUP:
                    mouseUp = true;
                    mouseDown = false;
                    break;
          }
     }
     return CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam);
}

I then made the CWPSTRUCT like this:

[StructLayout(LayoutKind.Sequential)]
public struct CWPSTRUCT
{
    public IntPtr lparam;
    public IntPtr wparam;
    public int message;
    public IntPtr hwnd;
}

And here’s the part where it probably goes wrong…I’ve tried 2 things:

CWPSTRUCT cwp = (CWPSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPSTRUCT));

Or the unsafe version this is where I got it from:

CWPSTRUCT* cp = (CWPSTRUCT*)lParam;

When using the first option I always get 0 for the hwnd part and with the unsafe version I just get nothing… I don’t really know what I’m doing wrong here. Any help would be appreciated 🙂

Thanks

  • 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-12T22:53:54+00:00Added an answer on June 12, 2026 at 10:53 pm

    Since you’re hooking WH_MOUSE_LL, lParam contains pointer to MSLLHOOKSTRUCT instead of CWPSTRUCT (which is actually for WH_CALLWNDPROC).

    So you should define the following structures:

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int X;
        public int Y;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    public struct MSLLHOOKSTRUCT
    {
        public POINT pt;
        public int mouseData;
        public int flags;
        public int time;
        public UIntPtr dwExtraInfo;
    }
    

    And marshal lParam to MSLLHOOKSTRUCT:

    MSLLHOOKSTRUCT mouseLowLevelHook = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
        typeof(MSLLHOOKSTRUCT));
    

    Also, you should change wParam‘s type from int to IntPtr, so it will work properly on 64-bit platforms.

    Additional links:

    • Hook Overview
    • SetWindowsHookEx
    • LowLevelMouseProc callback function
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project that is trying to use context-free grammars for parsing
Trying to compile a Web Project here but I keep on getting package does
Now that my project is almost finished I am trying to implement the login
I am trying to introduce this into my project, I did some search but
I'm trying to add videos to my WPF project, but I can't find a
I'm trying to upgrade my project and so I've come to transactions. This is
I am trying to consume a web service in my project but it does
I'm trying to recompile an old Deplhi 5 project but I'm receiving a fatal
I am trying to figure out a clean way to intercept uncaught exceptions that
I am trying to pull the project from git hub. one of my project

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.