Page 1 of 1
Why am I getting an invalid memory access here?
Posted: Tue Jan 20, 2009 6:42 am
by Mistrel
Am I missing something here? I get an invalid memory access on "EndProcedure". I'm using PB 4.30.
Code: Select all
OpenWindow(0,0,0,320,240,"")
Procedure PointInClientRect(hWnd, x, y)
Pt.POINT\x=100
Pt.POINT\y=100
ScreenToClient_(WindowID(0),@Pt.POINT)
GetClientRect_(WindowID(0),@Rc.RECT)
Lib=OpenLibrary(#PB_Any,"User32.dll")
Ptr=GetFunction(Lib,"PtInRect")
CloseLibrary(Lib)
If Ptr
CallFunctionFast(Ptr,@Rc,@Pt)
EndIf
EndProcedure
PointInClientRect(WindowID(0),100,100)
Posted: Tue Jan 20, 2009 6:48 am
by Mistrel
I think I found the problem. I need to pass a quad or two longs instead of an address:
Code: Select all
Procedure PointInClientRect(hWnd, x, y)
Pt.POINT\x=100
Pt.POINT\y=100
ScreenToClient_(WindowID(0),@Pt.POINT)
GetClientRect_(WindowID(0),@Rc.RECT)
Lib=OpenLibrary(#PB_Any,"User32.dll")
Ptr=GetFunction(Lib,"PtInRect")
CloseLibrary(Lib)
If Ptr
CallFunctionFast(Ptr,@Rc,PeekL(@Pt),PeekL(@Pt+SizeOf(LONG)))
EndIf
EndProcedure
Posted: Tue Jan 20, 2009 6:48 am
by freak
The second parameter to PtInRect is a POINT structure itself, not just a pointer to it. So the stack gets messed up.
You can solve this by passing the x/y as separate parameters to get the same stack layout as the POINT structure would have on the stack.
btw, this will not work on PB x64. For it to work there you have to put the whole POINT structure into a quad and pass that as a single parameter. It will work on both x86 and x64 if you use the quad way and a prototype instead of CallFunctionFast() (it does not support quads)
[edit] so close!

Posted: Tue Jan 20, 2009 7:00 am
by Mistrel
But it should still work in x64 with CallFunctionFast if I push the x and y variables as longs, correct?
Posted: Tue Jan 20, 2009 10:42 am
by srod
Use :
Works with PB x86 and x64.
Why are you loading the function from the dll?
Posted: Tue Jan 20, 2009 11:25 am
by Mistrel
srod wrote:Why are you loading the function from the dll?
http://www.purebasic.fr/english/viewtopic.php?t=35790
Posted: Tue Jan 20, 2009 12:07 pm
by srod
Ahhhh; fair enough!

As Freak says though, using a prototype would allow you to pass the entire point structure as a quad ala pt\y<<32+pt\x etc.
Posted: Tue Jan 20, 2009 3:00 pm
by Mistrel
I still love CallFunctionFast for quick one-line calls.

Posted: Tue Jan 20, 2009 6:28 pm
by ABBKlaus
I wonder why you never responded to that thread
Isn“t it fixed ?
Posted: Tue Jan 20, 2009 8:45 pm
by Mistrel
It might be. I needed an immediate solution so I just called the dll directly.
Re: Why am I getting an invalid memory access here?
Posted: Thu Mar 01, 2012 10:13 am
by wilbert
Have you considered creating your own point in structure procedure ?
It's something that should be easy to create.