Transform a variable into a linkedlist

Just starting out? Need help? Post your questions and find answers here.
xgp
Enthusiast
Enthusiast
Posts: 128
Joined: Mon Jun 13, 2005 6:03 pm

Transform a variable into a linkedlist

Post by xgp »

Hi!, i am working to get the following:

"Specify as the lParam of EnumWindows_() a linkedlist so that on EnumWindowsProc() the windows' handle are added to that list."

Code: Select all

Procedure EnumWindowsProc(hWnd, DestinyList.l)
  
  CallDebugger
  
  Debug "DestinyList.l = "+Str(DestinyList.l)
  
  ; so, DestinyList = @MyWindowsList()
  ; How to add elements do MyWindowsList() from here?
  

  ProcedureReturn 0 ; run this 1 time only
EndProcedure


Procedure EnumMyWindows()
  
  Protected NewList MyWindowsList.l()
  
  ; add an element so i can use this list as
  ; a parameter, workaround?
  AddElement(MyWindowsList())
  MyWindowsList() = 15
  
  
  Debug "Address of MyWindowsList() = "+Str(@MyWindowsList())
  EnumWindows_(@EnumWindowsProc(), @MyWindowsList())
  
EndProcedure

EnumMyWindows()
I have been the last 2 hours searching on the forums trying do learn more about how pointers and linkedlists work together but i still haven't found a way to do this.
Maybe my approach is stupid and i can't see it? I'm feeling like i got everything i need like the address to the list i want, but the stop is on how to add elements to that address as i would simply if that was a normal linkedlist.

I know that, if the linkedlist was global i could simply work it out, but i want the linkedlist to be local to that procedure(EnumMyWindows()). Maybe this is the reason why i can't use it on EnumWindowsProc() ? - because MyWindowsList() is local to EnumMyWindows()?

(Regain breath)

Throw me some light on this if you can and, thanks for taking the time to read this anyway.

Regards,
xgp
xgp
Enthusiast
Enthusiast
Posts: 128
Joined: Mon Jun 13, 2005 6:03 pm

Post by xgp »

Hi!, I've managed to work it out!.
I based my code from Trond's Dynamic LinkedLists post ( http://www.purebasic.fr/english/viewtopic.php?t=24399 ) and it worked as i wanted.
Here it is:

Code: Select all

Procedure.q NewDynamicList()
  Protected NewList Local.l() 
  !mov eax, [esp] 
  !mov edx, [esp+4] 
  !add esp, 8 
  !ret 
EndProcedure

Procedure.l BindList(List.l(), ID.q)
  !mov eax, [p.v_ID] 
  !mov edx, [p.v_ID+4] 
  !mov ecx, [p.v_ID-4] 
  !mov [ecx], eax 
  !add ecx, 4 
  !mov [ecx], edx
EndProcedure

Procedure EnumWindowsProc(hWnd, lParam)
  
  Static  NewList GhostList.l()
  
  BindList(GhostList(), lParam)
  
  AddElement(GhostList())
  GhostList() = hWnd 
  
  ProcedureReturn 1
EndProcedure

Procedure EnumMyWindows()
  
  Protected NewList MyWindowsList()
  Protected WindowsList = NewDynamicList()
  
  EnumWindows_(@EnumWindowsProc(), WindowsList)
  BindList(MyWindowsList(), WindowsList)
  
  ForEach MyWindowsList()
    Debug MyWindowsList()
  Next
  
EndProcedure

EnumMyWindows()
Now, i wanted to be sure that this piece of code is stable or that won't lead to any memory leak but i am no expert in purebasic guts. Can you detect some possible troubles in this code?

Regards,
xgp
Post Reply