Returning a structure as procedure parameter

Just starting out? Need help? Post your questions and find answers here.
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

Returning a structure as procedure parameter

Post by javabean »

Hi,
When I pass a pointer to a structure as procedure parameter I get an IMA.
I've been playing around for hours with the following code, but I can't figure out what's the problem...I'm stumped...

Dll Code:

Code: Select all

ProcedureDLL.i Test(*Rate.STRING)
 Protected retval.i

  *Rate\s = "Hello"

ProcedureReturn retval
EndProcedure
Exe Code:

Code: Select all

Import "xrate.lib"   
  Test.i(*Rate.STRING)
EndImport


Procedure Testit()
  xrate.STRING

  Test(@xrate)

  Debug xrate\s
EndProcedure

Testit()
When I call "Test()" outside of a procedure everything is fine, but when called inside 'Testit()' I get an IMA.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Returning a structure as procedure parameter

Post by netmaestro »

xrate.String is local to the procedure, non-persistent and the dll can't write to it. Make it Shared, Static or Global and all will be well.
BERESHEIT
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

Re: Returning a structure as procedure parameter

Post by javabean »

Thanks netmaestro, this works well!

But now I try to change xrate.STRING after the procedure has returned...and again I get an IMA...

Code: Select all

Import "xrate.lib"   
  Test.i(*Rate.STRING)
EndImport

Procedure Testit()
  Static xrate.STRING

  Test(@xrate)
  xrate\s = "Changed"

  Debug xrate\s
EndProcedure

Testit()
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Returning a structure as procedure parameter

Post by srod »

The dll and the client are using different heap memory and you will get an IMA error in the client if it tries to free a string created in the DLL (as indeed your code will be attempting).

You are best off getting either the client or the DLL to write all strings in order to prevent this crossing of the DLL boundary. For example, pass a DLL function the address of a memory buffer so that the DLL can poke the string directly etc.
I may look like a mule, but I'm not a complete ass.
Post Reply