i use to create a custom function like winexec(…):Hwnd that will retun the handle of executed application.
i did use the findwindow() but having problem if it change window caption.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
There is no general way to get “the” window handle of an application because there’s no guarantee that any program has one window handle. A program may have many top-level handles (i.e., Microsoft Word, one for each document), or it may have no windows at all. You might question what you really need the window handle for; there could be better ways of doing whatever it is you’re trying to do that don’t require any specific window handle.
WinExec(which has been deprecated for nearly 15 years, so you should seriously consider not using it anymore) andShellExecutereturn absolutely no information about the programs they start, if indeed they start any program at all. (ShellExecutemight use DDE to send a command to an already-running instance of the application.) And if they start an application, it might finish running before your program gets to run anymore.You can use
CreateProcessorShellExecuteExinstead. If they start a program, they will give you a process handle representing the program they started. You can use that to help you get additional information about the program, such as a list of its windows. Don’t bother withFindWindow; the caption and window class aren’t guaranteed to be unique; a program might use the same class name for many different windows, and multiple instances of a program would use the same class name without much way to select the one you really want.EnumWindowsis a function you can use to get a list of candidate window handles. You give it a function pointer, and it will call that function once for each top-level window on the desktop. You’ll need a way of telling it which process you’re interested in, and a way for it to return a list of results. The function only accepts one parameter, so the parameter will have to be a pointer to a structure that holds more information:TWndListis a type I made up to hold a list ofHWndvalues. If you have Delphi 2009 or later, you could useTList<HWnd>; for earlier versions, you could use aTListdescendant or whatever else you choose.CreateProcesswill tell you the new process ID in thedwProcessIDmember of theTProcessInformationrecord it fills;ShellExecuteExonly returns a process handle, so useGetProcessIDon that. The window-enumerating function needs a callback function matching this signature:You can use
EnumWindowsto get a handle list like this:You’ll implement the callback function like this:
Once you have the list, you can inspect other attributes of the window to determine which ones are really the ones you want. You might determine it by window title or class name, or perhaps by the other controls that are one that window.
When you’re finished using the process handle, make sure you call
CloseHandleon it so the OS can clean up the process’s bookkeeping information.