Why am I getting an invalid memory access here?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Why am I getting an invalid memory access here?

Post 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)
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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! :)
quidquid Latine dictum sit altum videtur
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

But it should still work in x64 with CallFunctionFast if I push the x and y variables as longs, correct?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Use :

Code: Select all

PtInRect_(rc,pt\y<<32+pt\x)
Works with PB x86 and x64.

Why are you loading the function from the dll?
I may look like a mule, but I'm not a complete ass.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

srod wrote:Why are you loading the function from the dll?
http://www.purebasic.fr/english/viewtopic.php?t=35790
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I still love CallFunctionFast for quick one-line calls. :)
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

Mistrel wrote:
srod wrote:Why are you loading the function from the dll?
http://www.purebasic.fr/english/viewtopic.php?t=35790
I wonder why you never responded to that thread :shock:
Isn´t it fixed ?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

It might be. I needed an immediate solution so I just called the dll directly.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Why am I getting an invalid memory access here?

Post by wilbert »

Have you considered creating your own point in structure procedure ?
It's something that should be easy to create.
Post Reply