delphi - How can I modify my EnumWindowNamesProc to work with Lazarus and use a List as a parameter? -


overview

i trying enumerate list of visible window names , populate list passed parameter.

in delphi able work following:

function enumwindownamesproc(whandle: hwnd; list: tstrings): bool; stdcall; var   title: array[0..255] of char; begin   result := false;    getwindowtext(whandle, title, 255);   if iswindowvisible(whandle)   begin     if title <> ''     begin       list.add(string(title));     end;   end;   result := true; end; 

then call above so:

enumwindows(@enumwindownamesproc, lparam(fwindownames));

note: fwindownames stringlist created inside custom class.


problem

i trying make function compatible lazarus/fpc wont accept list: tstrings parameter.

the compiler error in lazarus complains type mismatch (i have highlighted important parts):

error: incompatible type arg no. 1: got " (address of function(longword;tstrings):longbool;stdcall) ", expected " (procedure variable type of function(longword;longint):longbool;stdcall) "

i can stop compiler complaining changing function declaration so:

{$ifdef fpc}   function enumwindownamesproc(whandle: hwnd; param: lparam): bool; stdcall; {$else}   function enumwindownamesproc(whandle: hwnd; list: tstrings): bool; stdcall; {$endif} var   title: array[0..255] of char; begin   result := false;    getwindowtext(whandle, title, 255);   if iswindowvisible(whandle)   begin     if title <> ''     begin       {$ifdef fpc}         // list no longer available       {$else}         list.add(string(title));       {$endif}     end;   end;   result := true; end;   

but lose list parameter.

i know modify code , use listbox1 example directly inside function hoping create reusable function not need know vcl/lcl controls, instead hoping more elegant solution , pass tstrings based parameter , add instead.


question:

so question is, in delphi able pass tstrings based parameter enumwindownamesproc in lazarus wont accept it. possible, , if so, how can modify code lazarus accepts list: tstrings parameter?

you can. don't have lose list.
use correct parameters , typecast

function enumwindownamesproc(whandle: hwnd; list: lparam): bool; stdcall; var   title: array[0..255] of char; begin   result := false;    getwindowtext(whandle, title, 255);   if iswindowvisible(whandle)   begin     if title <> ''     begin       tstringlist(list).add(string(title));     end;   end;   result := true; end;  enumwindows(@enumwindownamesproc, lparam(list)); 

to complete answer. there 1 more option - define function pointer (like in delphi). can use same way.

function enumwindows(lpenumfunc:pointer; lparam:lparam):winbool; external 'user32' name 'enumwindows'; 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -