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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:34:20+00:00 2026-06-04T21:34:20+00:00

The objective is to programmatically start a Windows Form, get its handle, and send

  • 0

The objective is to programmatically start a Windows Form, get its handle, and send info to its wndProc() function using Win Api’s SendMessage() function.

I got the SendMessage() part taken care of but the problem now is getting the form’s handle after the process has been started.

My first guess was that Process’ MainWindowHandle property would get me the handle I am looking for, but after I start the process MainWindowHandle remains equal to 0 and the following code doesn’t show the handle of the process I just started:

foreach (Process p in Process.GetProcesses())
{
Console.WriteLine(p.MainWindowHandle);
}

Can someone tell me how to do this and whether it can actually be done?

  • 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-04T21:34:21+00:00Added an answer on June 4, 2026 at 9:34 pm

    Sometimes the Process takes a second the set up everything, but the object is returned immediately.

    For that reason, you should wait a little bit, in order to let the Process really get it started, and then it’s MainWindowHandle will be set appropriately, ready to be consumed.

    var proc = Process.Start("notepad");
    
    Thread.Sleep(1000); // This will wait 1 second
    
    var handle = proc.MainWindowHandle;
    

    Another way to do it in a more smart fashion would be:

    var proc = Process.Start("notepad");
    
    try
    {
        while (proc.MainWindowHandle == IntPtr.Zero)
        {
            // Discard cached information about the process
            // because MainWindowHandle might be cached.
            proc.Refresh();
    
            Thread.Sleep(10);
        }
    
        var handle = proc.MainWindowHandle;
    }
    catch
    {
        // The process has probably exited,
        // so accessing MainWindowHandle threw an exception
    }
    

    That will cause the process to start, and wait until the MainWindowHandle isn’t empty.

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

Sidebar

Related Questions

Is it possible to programmatically get the properties of an object in objective-c without
I need to get a list of chapters of pdf document programmatically in Objective
Would you call this implementation of a multiton in objective-c 'elegant'? I have programmatically
Objective: In support of a Windows Service that may have multiple instances on a
Objective: My script will download a remote file upon form submission, since the file
In Objective-c/Cocoa, how do I get the icon that Finder uses for the desktop
In objective c, if the using the getter and directly accessing the ivar do
In Cocoa/ Objective C I need to be able to programmatically find out which
How can I programmatically scroll a UIWebView in Objective-C (iOS)? The only solution I
I'm building a program, and I'm quite confident using Objective-C, but I don't know

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.