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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:54:38+00:00 2026-05-11T13:54:38+00:00

I want to send keyboard input to a window in another process, without bringing

  • 0

I want to send keyboard input to a window in another process, without bringing that window to the foreground. I can use PostMessage to fake the WM_KEYDOWN and WM_KEYUP; all I need to know is which window handle should receive the keyboard input — i.e., something like GetFocus, but for another, non-active application.

The GetGUIThreadInfo API looks promising — it returns an hwndFocus for another app. But I’ve had no luck getting it to work from C# on my 64-bit OS. I’ve copied (and then further tweaked) the declarations from pinvoke.net, but all I ever get back is a generic error code (more details below).

I am setting cbSize before I call GetGUIThreadInfo, so I’ve avoided the most obvious potential problem.

I’m running 64-bit Vista, so I don’t know whether the problem is that I’m not using the API correctly, or that it works differently in 64-bit — I have yet to find a code sample that specifically says it works successfully in Win64.

Here’s sample code. I’m using GetWindowThreadProcessId as recommended, so I don’t think the problem has to do with mixing thread IDs with thread handles:

[StructLayout(LayoutKind.Sequential)] internal struct Rect {     public int Left;     public int Top;     public int Right;     public int Bottom; }  [StructLayout(LayoutKind.Sequential)] internal class GuiThreadInfo {     public int cbSize;     public uint flags;     public IntPtr hwndActive;     public IntPtr hwndFocus;     public IntPtr hwndCapture;     public IntPtr hwndMenuOwner;     public IntPtr hwndMoveSize;     public IntPtr hwndCaret;     public Rect rcCaret; }  [DllImport('user32.dll')] internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); [DllImport('user32.dll', SetLastError = true)] internal static extern bool GetGUIThreadInfo(uint idThread, ref GuiThreadInfo lpgui);  IntPtr GetFocusedHandleFromProcessWithWindow(IntPtr window) {     var threadId = GetWindowThreadProcessId(window, IntPtr.Zero);     var info = new GuiThreadInfo();     info.cbSize = Marshal.SizeOf(info);     if (!GetGUIThreadInfo(threadId, ref info))         throw new Win32Exception();     return info.hwndFocus; } 

window is a valid window handle; GetWindowThreadProcessId returns a nonzero thread handle. But the call to GetGUIThreadInfo always returns false, and the exception message is always ‘The parameter is incorrect’.

Just in case the problem was that GetGUIThreadInfo somehow doesn’t have a 64-bit version, I tried changing all the 8-byte IntPtrs in the GuiThreadInfo declaration to 4-byte ints, but I still got the same error.

Does anyone have a working C# sample of GetGUIThreadInfo on Win64? Or, is there another way to find what the focused child-window handle would be in another app, without making that app active?

  • 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. 2026-05-11T13:54:39+00:00Added an answer on May 11, 2026 at 1:54 pm

    I haven’t looked at it too closely but one thing jumps out. In your GetGUIThreadInfo call you pass the GUIThreadInfo structure by ref but you’ve defined it as a class so you are sending a reference by ref, in other words a pointer to a pointer. Either change your GUIThreadInfo to a struct or remove the ref on the parameter and add [In, Out] attributes.

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

Sidebar

Ask A Question

Stats

  • Questions 157k
  • Answers 157k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are several ways to accomplish this: You could make… May 12, 2026 at 11:09 am
  • Editorial Team
    Editorial Team added an answer As an answer to how to override a method in… May 12, 2026 at 11:09 am
  • Editorial Team
    Editorial Team added an answer ANSWER: After extensive work by EngineYard (they're awesome!) they were… May 12, 2026 at 11:09 am

Related Questions

i'm trying to send fake keyboard input to an application that's running in a
I want to convert a string into a series of Keycodes, so that I
I am working on developing an on-screen keyboard with java. This keyboard has a
I want to send an Application Key Presses, To Automate some stuff that has

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.