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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:20:06+00:00 2026-05-15T10:20:06+00:00

i use to create a custom function like winexec(…):Hwnd that will retun the handle

  • 0

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.

  • 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-05-15T10:20:07+00:00Added an answer on May 15, 2026 at 10:20 am

    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) and ShellExecute return absolutely no information about the programs they start, if indeed they start any program at all. (ShellExecute might 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 CreateProcess or ShellExecuteEx instead. 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 with FindWindow; 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.

    EnumWindows is 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:

    type
      PWindowSearch = ^TWindowSearch;
      TWindowSearch = record
        TargetProcessID: DWord;
        ResultList: TWndList;
      end;
    

    TWndList is a type I made up to hold a list of HWnd values. If you have Delphi 2009 or later, you could use TList<HWnd>; for earlier versions, you could use a TList descendant or whatever else you choose.

    CreateProcess will tell you the new process ID in the dwProcessID member of the TProcessInformation record it fills; ShellExecuteEx only returns a process handle, so use GetProcessID on that. The window-enumerating function needs a callback function matching this signature:

    function SelectWindowByProcessID(Wnd: HWnd; Param: LParam): Bool; stdcall;
    

    You can use EnumWindows to get a handle list like this:

    function GetWindowListByProcessID(pid: DWord): TWndList;
    var
      SearchRec: TWindowSearch;
    begin
      Result := TWndList.Create;
      try
        SearchRec.TargetProcessID := pid;
        SearchRec.ResultList := Result;
        Win32Check(EnumWindows(SelectWindowByProcessID, LParam(@SearchRec)));
      except
        Result.Free;
        raise;
      end;
    end;
    

    You’ll implement the callback function like this:

    function SelectWindowByProcessID(Wnd: HWnd; Param: LParam): Bool; stdcall;
    var
      SearchRec: PWindowSearch;
      WindowPid: DWord;
    begin
      SearchRec := PWindowSearch(Param);
      Assert(Assigned(SearchRec));
      GetWindowThreadProcessID(Wnd, WindowPid);
      if WindowPid = SearchRec.TargetProcessID then
        SearchRec.ResultList.Add(Wnd);
      Result := True;
    end;
    

    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 CloseHandle on it so the OS can clean up the process’s bookkeeping information.

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

Sidebar

Related Questions

No related questions found

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.