Mainwindowhandle handle for external third-party programs initiated through process in C # 0

Source: Internet
Author: User

External third-party program initiated through process in the original C # Mainwindowhandle handle is 0

a few days ago, I encountered a WinForm program in C # that started a third-party jar program and modified the jar to run the form's title. The general requirements are described above, the following is the original ideas and implementation of the code. STEP1//Based on the path to the jar file, start the jarProcess p = process.start (Jarfilepath);STEP2//reference to SetWindowText declaration in WindowsAPI[System.Runtime.InteropServices.DllImport ("user32.dll", EntryPoint = "SetWindowText")]public static extern int SetWindowText (INTPTR hwnd, String lpstring);STEP3//Handle of the jar form getting startedIntPtr hwnd = P.mainwindowhandle; STEP4//Change the title bar text of the jar formSetWindowText (hwnd, "title bar new text");with these four steps above, it feels like you should be able to change the window title bar of the external third-party program that was launched. However, the actual results are not as imagined. Why the title bar text of the external third-party application form that was launched was not changed, what line of code was not executed, or what line of code was wrong, and when debug stepping, but did not find any problems, the new form of the handle is also normal, after executing the fourth step above the line of code, Look at the title bar text of the new form, which is perfectly changed. Best of all, the next idea is to see not debug single-step execution, which line out of the problem, so after the key step 3, add the following debug output codeConsole.WriteLine ("handle>>>" + hwnd);At this time the execution result is really leaking out the problem, debug statement output Mainwindowhandle is 0. The key to the next question is to find out why the Process.mainwindowhandle get a handle to the form 0 is the cause of the problem. after Google, found the cause of the problem, foreign experts to explain the reasons, the feeling is quite reliable. The value is also zero for processes that has been hidden, that's, processes that's not visible in the taskbar. This can is the case for processes that appear as icons in the notification area, at the far right of the taskbar.The general meaning is that the process obtained through Process.Start has not yet created an icon on the taskbar, so the process seems to be invisible, so the resulting handle is 0.It's easy to see why Process.mainwindowhandle is a normal reason for debugging in single-step debug .based on these explanations, a solution to add code before STEP3 is found. While (p.mainwindowhandle = = IntPtr.Zero)                {System.Threading.Thread.Sleep (+);                }to this, the problem is resolved. But there is still a question worth thinking about. In the process class there is waitforinputidle () This method, see the MSDN explanation seems to be waiting, I try not to use the while loop above, and with P. WaitForInputIdle (); instead, the result is not. for what, I hope to have a familiar friend can give an explanation.

Mainwindowhandle handle for external third-party programs initiated through process in C # 0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.