[SOLVED] Get unique constant identifier lazarus / 21-General ---------------------------------------------------------------------- pcurtis · 2021-05-18 14:23 I can get the handle of the foreground app with 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   AHandle: HWND;   AHandle := GetForegroundWindow;  How can I get a unique constant identifier (maybe exe name, or anything else but must be unique) for this handle? ---------------------------------------------------------------------- Thaddy · 2021-05-18 14:30 https://www.freepascal.org/docs-html/rtl/sysutils/createguid.html Note that handles are per session, so not permanent. ---------------------------------------------------------------------- pcurtis · 2021-05-18 14:38 The identifier must be constant across restarts, hence application EXE name. ---------------------------------------------------------------------- Handoko · 2021-05-18 14:41 Maybe by pressing Ctr+Shift+G in the Lazarus' Source Code Editor. ---------------------------------------------------------------------- pcurtis · 2021-05-18 14:48 What does that do. On my system it opens the AMD Radeon control panel ---------------------------------------------------------------------- Handoko · 2021-05-18 14:53 It will generate something like this: ['{97AEE240-25CB-4A98-AC64-626413E51E6A}'] They call it UUID/GUID: https://en.wikipedia.org/wiki/Universally_unique_identifier It promises to be unique (by who?). Everytime you press the shortcut, you will get different result. What wrong with your computer? The ctrl-shift-g (insert guid) shortcut is documented here: https://wiki.lazarus.freepascal.org/Lazarus_IDE_Shortcuts#macOS.2C_Laz arus_style ---------------------------------------------------------------------- lucamar · 2021-05-18 15:02 Quote from: pcurtis on May 18, 2021, 04:48:58 pmWhat does that do. On my system it opens the AMD Radeon control panel  It inserts a GUID in the source such as used for interface definitions, e.g..: 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 ['{E93B22EC-6270-4D83-9021-2045DDFCFC08}'] ---------------------------------------------------------------------- pcurtis · 2021-05-18 15:19 That will not help. I have no chance to modify the 'target app'. Really I need to get the EXE name from a handle. ---------------------------------------------------------------------- 440bx · 2021-05-18 15:25 Quote from: pcurtis on May 18, 2021, 05:19:23 pmThat will not help. I have no chance to modify the 'target app'. Really I need to get the EXE name from a handle. I know a Windows only solution.  Would that suffice ? ---------------------------------------------------------------------- pcurtis · 2021-05-18 15:27 Yes, I'm a windoze man. ---------------------------------------------------------------------- 440bx · 2021-05-18 15:35 Quote from: pcurtis on May 18, 2021, 05:27:29 pmYes, I'm a windoze man. It's really simple then.  Here is a code snippet that should get you goingCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     WindowThreadId := GetWindowThreadProcessId(Wnd, @WindowProcessId);   ProcessHandle := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,                               FALSE,                                 WindowProcessId);     if ProcessHandle <> 0 then    begin      ProcessExecutableSize := sizeof(WindowProcessExecutable);  // a buffer you declare anywhere you want.       QueryFullProcessImageName(ProcessHandle,                              0,                                WindowProcessExecutable,                              ProcessExecutableSize); with the window handle, get the process id, with the process id open the process to get a process handle, with the process handle get the executable name using QueryFullProcessImageName.  Go to the balcony, wave the executable's name and let the fat ladies know it's time for them to sing. HTH. ---------------------------------------------------------------------- Handoko · 2021-05-18 15:42 Interesting. I bookmarked it, maybe I can use it someday. Thank you for sharing it. ---------------------------------------------------------------------- pcurtis · 2021-05-18 15:45 Thanks. Do you have a little more complete version? My WinAPI knowledge sucks. ---------------------------------------------------------------------- Remy Lebeau · 2021-05-18 16:00 Quote from: 440bx on May 18, 2021, 05:35:18 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ProcessExecutableSize := sizeof(WindowProcessExecutable);  // a buffer you declare anywhere you want. You should use Length() instead of Sizeof(). QueryFullProcessImageName() wants a character count, not a byte count.  If you compile with a Unicode buffer, Sizeof() will report twice the size, thus risking a buffer overflow by telling QueryFullProcessImageName() there is room for more characters than there really is. ---------------------------------------------------------------------- 440bx · 2021-05-18 16:09 Quote from: Remy Lebeau on May 18, 2021, 06:00:53 pmQuote from: 440bx on May 18, 2021, 05:35:18 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ProcessExecutableSize := sizeof(WindowProcessExecutable);  // a buffer you declare anywhere you want.You should use Length() instead of Sizeof(). QueryFullProcessImageName() wants a character count, not a byte count.  If you compile with a Unicode buffer, Sizeof() will report twice the size, thus risking a buffer overflow by telling QueryFullProcessImageName() there is room for more characters than there really is. You're right.  I'm used to using sizeof because I do everything in ansi  (extended) ascii(single byte characters), with ansi ascii, that works but, it wouldn't work with non-sbcs.  When I use unicode I define the buffers a different way that spares me from using sizeof. Quote from: Handoko on May 18, 2021, 05:42:41 pmInteresting. I bookmarked it, maybe I can use it someday. Thank you for sharing it. You're welcome Handoko. Quote from: pcurtis on May 18, 2021, 05:45:57 pmThanks. Do you have a little more complete version? My WinAPI knowledge sucks. You're welcome.  I attached a complete example to this post (including executable).  It might be a little more complete than you were asking for.  It's an example about how to use qsort that uses the steps I outlined in the previous post.  Suggestion: make a directory where you'll expand the archive, there is about a half dozen files. HTH. ---------------------------------------------------------------------- pcurtis · 2021-05-18 16:18 May be I'm stupid, but I can't find any code. ---------------------------------------------------------------------- 440bx · 2021-05-18 16:25 Quote from: pcurtis on May 18, 2021, 06:18:27 pmMay be I'm stupid, but I can't find any code. you need to expand the archive.  The .7z file is a compressed archive, you need 7zip to decompress it.  7zip is a free download, I recommend it, very nice utility. Once expanded, you'll find there is a .lpr file (and the .lps, lpi).  The .lpr is the entire program. ---------------------------------------------------------------------- pcurtis · 2021-05-18 16:50 OK, got it. Compiles and runs. It's way out of my competence ---------------------------------------------------------------------- 440bx · 2021-05-18 17:03 Quote from: pcurtis on May 18, 2021, 06:50:58 pmOK, got it. Compiles and runs. It's way out of my competence  Good on both counts.  That it's out of your competence is a good thing.  Use the debugger to step through the program.  This might help 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(Wnd : HWND; WindowListInt : ptruint) : BOOL; stdcall;  { this function is called by the EnumWindows call. It fills the a window    }  { list with the window information we are interested in.                  } var  WindowList            : PWindowList absolute WindowListInt;   ProcessHandle         : THANDLE;   TimeOfNoInterest    : TFILETIME;  ProcessExecutableSize : DWORD; begin  EnumWindowsProc := TRUE;       { continue enumerating until no more windows }   with WindowList^.WindowInfo[WindowList^.WindowCount] do  begin    WindowHandle  := Wnd;     WindowThreadId := GetWindowThreadProcessId(Wnd, @WindowProcessId);    WindowVisible  := IsWindowVisible(Wnd);    GetWindowText(Wnd, WindowCaption, sizeof(WindowCaption));     { get the process times for this process.  NOTE: the method used here is  }    { very inefficient since a handle for the same process is obtained for    }    { every window the process has created.                                   }     ProcessHandle := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,                               FALSE,                                 WindowProcessId);     if ProcessHandle <> 0 then    begin      { IMPORTANT: the size must be reset every time because QueryFullProcess }      {            ImageName sets it to the length of the returned string     }      {            every time it's called.                                  }       ProcessExecutableSize := sizeof(WindowProcessExecutable);       QueryFullProcessImageName(ProcessHandle,                              0,                                WindowProcessExecutable,                              ProcessExecutableSize);       GetProcessTimes(ProcessHandle,                      WindowProcessCreationTime,                      TimeOfNoInterest,                    TimeOfNoInterest,                      TimeOfNoInterest);       CloseHandle(ProcessHandle);    end;  end;   with WindowList^ do  begin    WindowInfoPointers[WindowCount] := @WindowInfo[WindowCount];    inc(WindowCount);     if WindowCount > high(WindowInfo) then    begin      EnumWindowsProc := FALSE;   { no more room, stop enumerating            }    end;  end;end;  and 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     WM_COMMAND:    begin      case LOWORD(wParam) of        IDM_UPDATE_WINDOW_LIST:        begin          ZeroMemory(WindowList, sizeof(WindowList^));           { fill it with the window handles and their caption text            }           EnumWindows(@EnumWindowsProc,                      ptruint(WindowList));           { pass the WindowList   }           { if the "sort by process" menu item is checked then the new window }        { list must be sorted by process before the listbox is updated.   }           MenuItemState := GetMenuState(GetMenu(Wnd),                                      IDM_PROCESS_SORT,                                      MF_BYCOMMAND);           if MenuItemState and MF_CHECKED <> 0 then          begin            { sort the window list                                          }             with WindowList^ do          begin              qsort(@WindowInfoPointers,                     WindowCount,                     sizeof(WindowInfoPointers[low(WindowInfoPointers)]),                   TCompareFunction(@CompareWindowInfoProcessId));            end;           PostMessage(Wnd, WM_COMMAND, IDM_UPDATE_LISTBOX, 0);          exit;          end;           { if the "sort by process creation time" is checked then the new    }          { window list must be sorted accordingly.                           }           MenuItemState := GetMenuState(GetMenu(Wnd),                                      IDM_PROCESS_CREATION_SORT,                                      MF_BYCOMMAND);           if MenuItemState and MF_CHECKED <> 0 then          begin            { sort the window list                                          }             with WindowList^ do          begin              qsort(@WindowInfoPointers,                     WindowCount,                     sizeof(WindowInfoPointers[low(WindowInfoPointers)]),                   TCompareFunction(@CompareWindowInfoProcessTimes));            end;             PostMessage(Wnd, WM_COMMAND, IDM_UPDATE_LISTBOX, 0);            exit;          end;           { unnecessary but safe                                            }           PostMessage(Wnd, WM_COMMAND, IDM_UPDATE_LISTBOX, 0);           exit;        end;  Put a breakpoint at each of the highlighted lines, debug the program and step through the code when the breakpoints are hit.  Hover over the variables and you'll see how it all works. You'll figure it out. ---------------------------------------------------------------------- pcurtis · 2021-05-18 17:12 I think I've got it  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 GetCurrentActiveProcessPath: String;var  ProcessHandle : DWORD;  ProcessExecutableSize     : DWORD;  WindowHandle              : HWND;  WindowProcessId           : DWORD;  WindowThreadId            : DWORD;  WindowProcessCreationTime : TFILETIME;                     { inefficient  }  WindowProcessExecutable   : packed array[0..511] of char;  { inefficient  }  WindowVisible             : BOOL;  WindowCaption             : packed array[0..127] of char;begin  WindowHandle := GetForegroundWindow;   WindowThreadId := GetWindowThreadProcessId(WindowHandle, @WindowProcessId);   ProcessHandle := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,                               FALSE,                                 WindowProcessId);   if ProcessHandle <> 0 then    begin      ProcessExecutableSize := Length(WindowProcessExecutable);  // a buffer you declare anywhere you want.       QueryFullProcessImageName(ProcessHandle,                               0,                               WindowProcessExecutable,                             @ProcessExecutableSize);     end;  Result := WindowProcessExecutable;end; ---------------------------------------------------------------------- 440bx · 2021-05-18 17:24 That looks like it should work. ---------------------------------------------------------------------- pcurtis · 2021-05-19 08:25 Indeed it does. Thanks.