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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:54:04+00:00 2026-06-07T00:54:04+00:00

I was looking at the example in Microsoft KB318804 but they use the threadId

  • 0

I was looking at the example in Microsoft KB318804 but they use the threadId of the “current” application!!! I have some C++ code which works but we have to rewrite it, and I would prefer to re-write in C# while I am in there. The one thing it does is get the threadId of the target application like so:

uint lastId = GetWindowThreadProcessId(targetHandle, IntPtr.Zero);

no, GetCurrentThread is not the correct call as I am getting the thread id of a remote application which is what we do today AND what we want to do. targetHandle is a handle to that remote application.

I cast this lastId to an int and tried to wire up C# code but the SetWindowsHookEx returns 0 and fails. Only AppDomain.GetCurrentThreadId() seems to work (even though it is deprecated, the replacement though doesn’t work either).

Do I have to go with the C++ code then? or is there a way to get it to work in C#?

Currently we register the hookhandler in C++ with the other application and get events back.

  • 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-07T00:54:05+00:00Added an answer on June 7, 2026 at 12:54 am

    Have you checked out the pinvoke.net entry for SetWindowsHookEx?

    If SetWindowsHookEx returns NULL, you are supposed to call GetLastError, so in C# you should call Marshal.GetLastWin32Error (Assuming that DllImportAttribute.SetLastError was included on the P/Invoke signature.)

    From pinvoke.net:

    Signature

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);
    

    Calling

    IntPtr hHook;
    
    using (Process process = Process.GetCurrentProcess())
    using (ProcessModule module = process.MainModule)
    {
        IntPtr hModule = GetModuleHandle(module.ModuleName);
    
        hHook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, hook, hModule, 0);
    }
    

    Possibly related questions:

    • PInvoke errors calling external SetWindowsHookEx and GetModuleHandle
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to add some new functionality to an existing application (database is Microsoft
I am looking for some Microsoft API that whould have the same capabilities for
I'm working on some customized authentication code based on Microsoft's membership stuff. While looking
I'm looking at the TPL exception handling example from MSDN @ http://msdn.microsoft.com/en-us/library/dd537614(v=VS.100).aspx The basic
I'm looking for example code for setting up a connection to a bluetooth device
im looking for an example script. I saw one yesterday but for the life
I took the code from this example. http://msdn.microsoft.com/en-us/library/2tw134k3.aspx What I am wondering (and I've
I have a table (actually a view, but simplified my example to a table)
I'd like to learn what various code libraries, which I think are all Microsoft's,
I'm Looking for Code to populate a Microsoft Sql Server Database, with my email

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.