How to get the Handle that is executed in winexec or shellexecute?

前端 未结 3 1800
温柔的废话
温柔的废话 2021-01-28 09:54

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 w

3条回答
  •  醉梦人生
    2021-01-28 10:28

    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; 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.

提交回复
热议问题