Recent

Author Topic: [SOLVED] Get unique constant identifier  (Read 3445 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Get unique constant identifier
« on: May 18, 2021, 04:23:55 pm »
I can get the handle of the foreground app with

Code: Pascal  [Select][+][-]
  1.   AHandle: HWND;
  2.  
  3.   AHandle := GetForegroundWindow;
  4.  

How can I get a unique constant identifier (maybe exe name, or anything else but must be unique) for this handle?
« Last Edit: May 19, 2021, 10:23:52 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Get unique constant identifier
« Reply #1 on: May 18, 2021, 04:30:19 pm »
https://www.freepascal.org/docs-html/rtl/sysutils/createguid.html

Note that handles are per session, so not permanent.
Specialize a type, not a var.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Get unique constant identifier
« Reply #2 on: May 18, 2021, 04:38:40 pm »
The identifier must be constant across restarts, hence application EXE name.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Get unique constant identifier
« Reply #3 on: May 18, 2021, 04:41:41 pm »
Maybe by pressing Ctr+Shift+G in the Lazarus' Source Code Editor.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Get unique constant identifier
« Reply #4 on: May 18, 2021, 04:48:58 pm »
What does that do. On my system it opens the AMD Radeon control panel  %)
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Get unique constant identifier
« Reply #5 on: May 18, 2021, 04:53:35 pm »
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_Lazarus_style
« Last Edit: May 18, 2021, 04:58:10 pm by Handoko »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Get unique constant identifier
« Reply #6 on: May 18, 2021, 05:02:15 pm »
What 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][+][-]
  1. ['{E93B22EC-6270-4D83-9021-2045DDFCFC08}']
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Get unique constant identifier
« Reply #7 on: May 18, 2021, 05:19:23 pm »
That will not help. I have no chance to modify the 'target app'. Really I need to get the EXE name from a handle.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Get unique constant identifier
« Reply #8 on: May 18, 2021, 05:25:27 pm »
That 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 ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Get unique constant identifier
« Reply #9 on: May 18, 2021, 05:27:29 pm »
Yes, I'm a windoze man.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Get unique constant identifier
« Reply #10 on: May 18, 2021, 05:35:18 pm »
Yes, I'm a windoze man.
It's really simple then.  Here is a code snippet that should get you going
Code: Pascal  [Select][+][-]
  1.     WindowThreadId := GetWindowThreadProcessId(Wnd, @WindowProcessId);
  2.  
  3.     ProcessHandle := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,
  4.                                  FALSE,
  5.                                  WindowProcessId);
  6.  
  7.     if ProcessHandle <> 0 then
  8.     begin
  9.       ProcessExecutableSize := sizeof(WindowProcessExecutable);  // a buffer you declare anywhere you want.
  10.  
  11.       QueryFullProcessImageName(ProcessHandle,
  12.                                 0,
  13.                                 WindowProcessExecutable,
  14.                                 ProcessExecutableSize);
  15.  
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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Get unique constant identifier
« Reply #11 on: May 18, 2021, 05:42:41 pm »
Interesting. I bookmarked it, maybe I can use it someday.

Thank you for sharing it.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Get unique constant identifier
« Reply #12 on: May 18, 2021, 05:45:57 pm »
Thanks. Do you have a little more complete version? My WinAPI knowledge sucks.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1311
    • Lebeau Software
Re: Get unique constant identifier
« Reply #13 on: May 18, 2021, 06:00:53 pm »
Code: Pascal  [Select][+][-]
  1. 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.
« Last Edit: May 18, 2021, 06:02:30 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Get unique constant identifier
« Reply #14 on: May 18, 2021, 06:09:40 pm »
Code: Pascal  [Select][+][-]
  1. 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.



Interesting. I bookmarked it, maybe I can use it someday.

Thank you for sharing it.
You're welcome Handoko.



Thanks. 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.
« Last Edit: May 18, 2021, 06:12:08 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018