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

Just starting out? Need help? Post your questions and find answers here.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

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

Post 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)  {.....;}
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
inc.
Enthusiast
Enthusiast
Posts: 406
Joined: Thu May 06, 2004 4:28 pm
Location: Cologne/GER

Post 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.
Check out OOP support for PB here!
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

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

Post 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)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

I see ... Thanks.
I'll test this code.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

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

Post by traumatic »

What about using Static?
Good programmers don't comment their code. It was hard to write, should be hard to read.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post 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
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post 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 

Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Post Reply