[SOLVED] Pass parameter to function without using global variable lazarus / 62-General ---------------------------------------------------------------------- pcurtis · 2021-05-21 10:56 I have this function : 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; {%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;  I would like to pass a parameter (string) like this 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(AValue, Title) <> 0) then       AHandle := hWnd;   Result := True; end; function GetJPGViewHandle(Avalue : string) : hWnd; begin   AHandle := 0;   EnumWindows(@EnumWindowsProc, 0);   Result := AHandle; end; withput using global variables. How can I do that? ---------------------------------------------------------------------- Zvoni · 2021-05-21 11:05 What i could find out, apparantly it's possible to use a nested function as a callback. So in your case: Try it by nesting EnumWindowsProc within GetJPGViewHandle, and there you should be able to access your AValue- Argument ---------------------------------------------------------------------- MarkMLl · 2021-05-21 11:16 Quote from: Zvoni on May 21, 2021, 01:05:52 pmWhat i could find out, apparantly it's possible to use a nested function as a callback. Although I think that was added comparatively recently and might be mode-specific. MarkMLl ---------------------------------------------------------------------- pcurtis · 2021-05-21 11:20 When I nest I can't reference the callback function EnumWindows(@EnumWindowsProc, 0); fails Dumb question - How can I update FPC, without updating Lazarus ---------------------------------------------------------------------- 440bx · 2021-05-21 11:30 Quote from: pcurtis on May 21, 2021, 12:56:06 pmI would like to pass a parameter (string) 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; {%H-}lParam: LPARAM): bool; stdcall;var  Title: array[0..255] of Char;begin  GetWindowText(hWnd, Title, 255);  if (IsWindowVisible(hWnd)) and (pos(AValue, Title) <> 0) then      AHandle := hWnd;  Result := True;end; function GetJPGViewHandle(Avalue : string) : hWnd;begin  AHandle := 0;  EnumWindows(@EnumWindowsProc, 0);  Result := AHandle;end;  withput using global variables. How can I do that? I'm not completely sure this is what you want but, the following wouldn't require a global variable 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; {%H-}lParam: LPARAM): bool; stdcall;var  Title: array[0..255] of Char;   MyBuffer : { whatever type is convenient here } absolute lparam; begin  GetWindowText(hWnd, Title, 255);  if (IsWindowVisible(hWnd)) and (pos(AValue, Title) <> 0) then    AHandle := hWnd;   { put whatever you want in MyBuffer and the calling function will have access to it }   Result := True;end; function GetJPGViewHandle(Avalue : string) : hWnd;var  BufferForEnumWindowsProc : packed array[0..8191] of char; { just some memory block } begin  AHandle := 0;  EnumWindows(@EnumWindowsProc, @BufferForEnumWindowsProc);  Result := AHandle;end;   Note that the above just shows the concept.  You'll need declare the buffer and its type in accordance with what you need to do with the information you intend to gather. Essentially, you allocate the buffer locally and pass a pointer to it to the enumwindows callback which supposedly will fill the buffer with whatever information the caller is interested in. HTH. ---------------------------------------------------------------------- pcurtis · 2021-05-21 11:54 I need it the other way round. Pass a string to the callback. ---------------------------------------------------------------------- 440bx · 2021-05-21 12:03 Quote from: pcurtis on May 21, 2021, 01:54:19 pmI need it the other way round. Pass a string to the callback. In that case, instead of the caller passing a buffer, it passes a string (more accurately, pointer to a string.) ---------------------------------------------------------------------- Zvoni · 2021-05-21 12:21 Quote from: MarkMLl on May 21, 2021, 01:16:44 pmQuote from: Zvoni on May 21, 2021, 01:05:52 pmWhat i could find out, apparantly it's possible to use a nested function as a callback. Although I think that was added comparatively recently and might be mode-specific. MarkMLl You might be right. The sample i found was on a german Delphi-Forum from 2018 Soooo.... --> mode delphi? EDIT: Hmmm.... http://free-pascal-general.1045716.n5.nabble.com/Callbacks-as-nested- functions-td5730116.html Look at post 5 of Matthias ---------------------------------------------------------------------- MarkMLl · 2021-05-21 12:41 Quote from: Zvoni on May 21, 2021, 02:21:44 pmhttp://free-pascal- general.1045716.n5.nabble.com/Callbacks-as-nested-functions- td5730116.html Look at post 5 of Matthias Specifically, and in order for info to be in one place if somebody gets here via Google etc., the directive near the start of the example below: 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 program test1; {$mode objfpc}{$H+}{$ModeSwitch nestedprocvars} type  TCallback = procedure (a: integer) is nested; procedure DoCallback;  procedure DoThis(a: integer);  begin    writeln(a);  end;var  callback: TCallback;begin  callback := @DoThis;  callback(100);end; begin  DoCallback;end. // Keep in mind what Sven wrote. You cannot use "callback" outside of// DoCallBack, because FPC does not yet support Delphi's "reference to".  MarkMLl ---------------------------------------------------------------------- pcurtis · 2021-05-21 12:54 I'm not a hardcore programmer. I can't see  how to apply this to my code. ---------------------------------------------------------------------- PascalDragon · 2021-05-21 14:54 Quote from: pcurtis on May 21, 2021, 12:56:06 pmI have this function : 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; {%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;  I would like to pass a parameter (string) like this 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(AValue, Title) <> 0) then       AHandle := hWnd;   Result := True; end; function GetJPGViewHandle(Avalue : string) : hWnd; begin   AHandle := 0;   EnumWindows(@EnumWindowsProc, 0);   Result := AHandle; end; withput using global variables. How can I do that? As I had explained here in the other thread you can pass parameters through the lParam parameter of EnumWindows. As you should also get rid of the AHandle you can use a record: 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 type  TParamRec = record    handle: HWND;    title: String;  end;  PParamRec = ^TParamRec; function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): bool stdcall;var  Title: array[0..255] of Char;  args: PParamRec absolute lParam;begin  GetWindowText(hWnd, Title, 255);  if (IsWindowVisible(hWnd)) and (pos(args^.title, Title) <> 0) then      args^.handle := hWnd;  Result := True;end;     function GetJPGViewHandle(const aValue: String) : hWnd;var  args: TParamRec;begin  args.title := aValue;  args.handle := 0;  EnumWindows(@EnumWindowsProc, LPARAM(@args));  Result := args.handle;end; ---------------------------------------------------------------------- pcurtis · 2021-05-21 15:09 Thanks again.