Page 1 of 1

Porting c/c++ to PB : Return Structure without pointer

Posted: Fri Feb 01, 2008 1:04 am
by eddy
Hi,
How can I convert this ?
I know PB procedures can return structure pointer.
But C functions can return a structure without any pointer.

Code: Select all

struct MyResultStruct MyProcedure(void)  {.....;}

Posted: Fri Feb 01, 2008 8:33 am
by inc.
Imho not possible.

Whats the size of the struct? If it wont exceed 64bit/8bytes you can wrap it into a quad variable.

Re: Porting c/c++ to PB : Return Structure without pointer

Posted: Fri Feb 01, 2008 11:45 am
by tinman
eddy wrote:Hi,
How can I convert this ?
I know PB procedures can return structure pointer.
But C functions can return a structure without any pointer.

Code: Select all

struct MyResultStruct MyProcedure(void)  {.....;}
I showed Mistrel some code recently that showed how C does this. It takes a pointer to the structure to return as a hidden parameter to the procedure. In PB you would need to do this manually:

Code: Select all

Procedure MyProcedure(*return_structure.MyStructure)
EndProcedure

Define.MyStructure my_value
MyProcedure(@my_value)

Posted: Fri Feb 01, 2008 1:42 pm
by eddy
I see ... Thanks.
I'll test this code.

Re: Porting c/c++ to PB : Return Structure without pointer

Posted: Fri Feb 01, 2008 2:27 pm
by traumatic
What about using Static?

Posted: Fri Feb 01, 2008 8:46 pm
by Mistrel
Tinman's method works great. Here is a link to the other post for more information.

http://www.purebasic.fr/english/viewtopic.php?t=30538

Posted: Sun Feb 10, 2008 3:04 am
by eddy
It does not work like in your example... I tested DLL and Staticlib :? versions.

For information, I found how to convert this kind of function by using "ImportC"
But it's bad... the returned type can be bigger than 8 bytes (.q) :?

Code: Select all

Structure cpVect
	x.f
	y.f
EndStructure

ImportC "ChipmunkLib.a.lib"
	cpvforangle.q(a.f)
EndImport 


v_cpVect.q = cpvforangle(#PI/4.0)
v.cpVect
v\x= PeekF(@v_cpVect+OffsetOf(cpVect\x))
v\y= PeekF(@v_cpVect+OffsetOf(cpVect\y))
Debug v\x
Debug v\y

Posted: Sun Feb 10, 2008 4:22 pm
by eddy
it's better with DLL : there's no limitation

Code: Select all

Structure cpVect
	x.f
	y.f
EndStructure

If OpenLibrary(1,"chipmunk-4.0.2.dll")
	func = GetFunction(1,"cpvforangle")

	
	v_cpVect=CallFunctionFast(func,#PI/4.0)
	v.cpVect
	v\x= PeekF(@v_cpVect+OffsetOf(cpVect\x))
	v\y= PeekF(@v_cpVect+OffsetOf(cpVect\y))
	Debug v\x
	Debug v\y
EndIf