Rewrite a function lazarus / 21-General ---------------------------------------------------------------------- pcurtis · 2021-05-19 08:22 Is there a better way to do this? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  AHandle: HWND; function EnumWindowsProc(hWnd: HWND; {%H-}lParam: LPARAM): bool stdcall;var  Title: array[0..255] of Char;begin  GetWindowText(hWnd, Title, 255);  if (IsWindowVisible(hWnd)) and (pos('JPEGView', Title) <> 0) then      AHandle := hWnd;  Result := True;end; function GetJPGViewHandle : hWnd;begin  AHandle := 0;  EnumWindows(@EnumWindowsProc, 0);  Result := AHandle;end; procedure TForm1.FormCreate(Sender: TObject);begin  Caption := GetJPGViewHandle.ToString;end; ---------------------------------------------------------------------- pcurtis · 2021-05-19 10:22 I thought like this : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetJPGViewHandle(AText : String) : hWnd;var  AHandle : HWND;  function EnumWindowsProc(hWnd: HWND; {%H-}lParam: LPARAM): bool stdcall;    var      Title: array[0..255] of Char;    begin      GetWindowText(hWnd, Title, 255);      if (IsWindowVisible(hWnd)) and (pos(AText, Title) <> 0) then        AHandle := hWnd;      Result := True;    end;begin  AHandle := 0;  EnumWindows(@EnumWindowsProc, 0);  Result := AHandle;end; procedure TForm1.FormCreate(Sender: TObject);var  AHandle : hWnd;begin  AHandle := GetJPGViewHandle('JPEGView');end;  but I can't reference the callback function  EnumWindows(@EnumWindowsProc, 0); ---------------------------------------------------------------------- Zvoni · 2021-05-19 10:31 In your first Code, are you missing a Begin/end around Line 9/10? Because currently, you set the Result always to True, which would continue the Enumeration EDIT: and is there a semicolon missing in your Function-header? Between "bool" and "stdcall" EDIT2: Found this: https://forum.lazarus.freepascal.org/index.php?topic=13007.0 Looks different to yours ---------------------------------------------------------------------- Mr.Madguy · 2021-05-19 10:41 May be it's better to use FindWindow and search by window class? ---------------------------------------------------------------------- pcurtis · 2021-05-19 11:09 @Mr.Madguy - can't use FindWindow because the class is not unique and the caption varies. @Zvoni semicolon yes, but compiles and works without it . begin end are not needed. The code works, but I want to simplify it. ---------------------------------------------------------------------- PascalDragon · 2021-05-19 11:23 Quote from: pcurtis on May 19, 2021, 01:09:34 pm@Zvoni semicolon yes, begin end are not needed. The code works, but I want to simplify it. The lParam parameter of EnumWindows is a) a type of pointer size and b) passed through to your callback. Thus you can do it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): bool stdcall;var  Title: array[0..255] of Char;  hdl: PHWND absolute lParam;begin  GetWindowText(hWnd, Title, 255);  if (IsWindowVisible(hWnd)) and (pos('JPEGView', Title) <> 0) then      hdl^ := hWnd;  Result := True;end; function GetJPGViewHandle : hWnd;begin  EnumWindows(@EnumWindowsProc, LPARAM(@Result));end; This way you don't need to use a global variable (which would be bad if multi threading is involved and no synchronization is used). ---------------------------------------------------------------------- pcurtis · 2021-05-19 11:35 Doesn't compile. I get QuoteCompile Project, Target: project1.exe: Exit code 1, Errors: 3, Hints: 1 unit1.pas(33,8) Error: Identifier not found "PHWND" unit1.pas(33,14) Error: Error in type definition unit1.pas(37,12) Error: Illegal qualifier unit1.pas(43,33) Hint: Conversion between ordinals and pointers is not portable ---------------------------------------------------------------------- dseligo · 2021-05-19 11:37 Use PHANDLE instead of PHWND. ---------------------------------------------------------------------- 440bx · 2021-05-19 11:41 Quote from: pcurtis on May 19, 2021, 01:09:34 pm@Mr.Madguy - can't use FindWindow because the class is not unique and the caption varies. @Zvoni semicolon yes, but compiles and works without it . begin end are not needed. The code works, but I want to simplify it. I have a question for you... do you know beforehand the name of the executable that has the string "JPEGView" in its caption ? if you know the executable's name then it would likely be simpler to find out if the executable is running and then do whatever it is you want to do with it. Just a thought, because looking for strings in window captions is not a reliable way to getting to an executable of interest. ---------------------------------------------------------------------- pcurtis · 2021-05-19 11:54 OK, how do I find if an executable is running, and get it's top level window handle? ---------------------------------------------------------------------- 440bx · 2021-05-19 13:45 Quote from: pcurtis on May 19, 2021, 01:54:20 pmOK, how do I find if an executable is running, and get it's top level window handle? There are a number of APIs that will list which executables are running.  The Toolhelp32 functions Process32First/Next among others would do that.  You get the process id from them, then use the same method used in EnumWindows, that is, with the process id open the process, get the full image name (Process32First returns an abbreviated image name - no path information - which is not enough to uniquely identify an executable.) That step gives you the process id and the full image name. Once you have that, you can use EnumWindows to enumerate the top level windows.  Once you hit the window that has the same process id then you've got the process and window you are interested in. For good measure, it must be noted that it is possible for the process you found in the first step to end, a new process be created by the user or Windows that just happens to use the same process id as the process that ended.  This can happen because Windows re-uses process ids but, because of how Windows goes about re-using process ids, it is extremely unlikely to happen unless you purposely create a contrived case that would cause that situation.  I mention it only for completeness.  If I were you, I wouldn't worry about it. HTH. ---------------------------------------------------------------------- Zvoni · 2021-05-19 13:48 can you use WMI? If yes, you can query "winmgmts" select * from win32_process where name='myexename.exe' If ReturnCount>0 Then ExeIsRunning:=True; EDIT: Regarding WMI: Jurrasic Pork released something: https://forum.lazarus.freepascal.org/index.php?topic=24490.0 ---------------------------------------------------------------------- pcurtis · 2021-05-19 14:36 @Zvoni - Do you have a small sample? ---------------------------------------------------------------------- Zvoni · 2021-05-19 15:08 Look at the link Otherwise, tomorrow, i think i can find the link to the WMI i used in the past ---------------------------------------------------------------------- ASerge · 2021-05-20 01:39 Quote from: pcurtis on May 19, 2021, 10:22:29 amIs there a better way to do this? My version . What's important: 1. In general, GetWindowText does not work across application boundaries. Although Windows makes an exception for top level windows (for compatibility with Win 3.0), but it is always better to do it correctly. 2. Using SendMessageTimeout you can exclude hung applications. 3. The search stops as soon as the window is found. 4. When comparing, you can skip the conversion of PChar to string (implicitly, via the Pos function), and use ready-made functions. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Windows; function ContinueSearchNextWindow(AWnd: HWND; ALParam: LPARAM): BOOL; stdcall;const  CWinTitleContains = 'JPEGView';  SMTO_ERRORONEXIT = $20; // Not defined in windows.pasvar  Buffer: array[Byte] of Char;  Len: LRESULT;begin  if SendMessageTimeoutA(AWnd, WM_GETTEXT, SizeOf(Buffer), LPARAM(@Buffer),    SMTO_ABORTIFHUNG or SMTO_ERRORONEXIT, MSecsPerSec, @Len) <> 0 then  begin    if Len >= Length(CWinTitleContains) then    begin      if AnsiStrPos(Buffer, CWinTitleContains) <> nil then      begin        HWND(Pointer(ALParam)^) := AWnd;        Exit(False);      end;    end;  end;  Result := True;end; function GetJPGViewHandle: HWND;begin  Result := 0;  EnumWindows(@ContinueSearchNextWindow, LPARAM(@Result));end; ---------------------------------------------------------------------- PascalDragon · 2021-05-20 07:03 Quote from: pcurtis on May 19, 2021, 01:35:48 pmDoesn't compile. I get QuoteCompile Project, Target: project1.exe: Exit code 1, Errors: 3, Hints: 1 unit1.pas(33,8) Error: Identifier not found "PHWND" unit1.pas(33,14) Error: Error in type definition unit1.pas(37,12) Error: Illegal qualifier unit1.pas(43,33) Hint: Conversion between ordinals and pointers is not portable Yes, sorry, as dseligo said, use PHandle instead.