Page 1 of 1

Returning a structure as procedure parameter

Posted: Tue May 22, 2012 8:31 pm
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.

Re: Returning a structure as procedure parameter

Posted: Tue May 22, 2012 8:53 pm
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.

Re: Returning a structure as procedure parameter

Posted: Tue May 22, 2012 10:04 pm
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()

Re: Returning a structure as procedure parameter

Posted: Tue May 22, 2012 10:25 pm
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.