callfunction() array return

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tony_usa.

Hi,

I know it is a lame question, but I have been unable to find an answer in previous posts.

================================
I have a DLL, with a C function
void test(int *in)
{
in[0] = 30;
in[1] = 40;
}

================================
Then I call it from PB:
Dim in.w(2)
in(0) = 10
in(1) = 20

CallFunction(0, "_test", @in) ; Library was initialized

Debug PeekW(@in) ; returns 30 (good)

==================================
How do I access the next item of the array, so that I have 40? Is there a cleaner way to return an array?

Thanks
Tony
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by tony_usa

I know it is a lame question, but I have been unable to find an answer in previous posts.
This does not work in 3.62, but has worked in all previous versions of PB and hopefully will in the future:

Code: Select all

Structure wptr
  w.w[0]
EndStructure

Dim in.w(2)

*array_ptr.wptr = @in

CallFunction(0, "_test", *array_ptr)
Debug *array_ptr\w[0] ; prints 30
Debug *array_ptr\w[1] ; prints 40

--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.51, external editor)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Manolo.

Hi,
Please try with the next code:

Dim in.w(1)
in(0) = 10
in(1) = 20

CallFunction(0, "_test", @in) ; Library was initialized

Debug PeekW(@in) ; returns 30 (good)

!ADD esp,2;sum 2 positions

Debug PeekW(@in)

Regards,
Manolo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Array pointers are referred to like this:

Code: Select all

in()
This works:

Code: Select all

Structure wptr
  w.w[0]
EndStructure

Dim in.w(2)
in(0) = 0
in(1) = 1
in(2) = 2

*array_ptr.wptr = in()

;CallFunction(0, "_test", *array_ptr)
Debug *array_ptr\w[0] ; prints 0
Debug *array_ptr\w[1] ; prints 1
Debug *array_ptr\w[2] ; prints 2
If you want to return an array pointer:

Code: Select all

  ProcedureReturn MyArray()
And retrieve it like this (use only with long and string arrays, read below to know why):

Code: Select all

*MyArrayCatcher() = CallFunction(whatever...)
You can also 'cast' an array, but you can only do that with string and long arrays, because for some reason casted arrays ignore the .w and .b types and assume long data (I think this should be fixed):

Code: Select all

; Works
Dim in.l(2)
in(0) = 0
in(1) = 1
in(2) = 2

Dim *array_ptr.l(2)
*array_ptr() = in()

;CallFunction(0, "_test", *array_ptr())
Debug *array_ptr(0) ; prints 0
Debug *array_ptr(1) ; prints 1
Debug *array_ptr(2) ; prints 2
Not working (word data):

Code: Select all

; Doesn't work
Dim in.w(2)
in(0) = 0
in(1) = 1
in(2) = 2

Dim *array_ptr.w(2)
*array_ptr() = in()

;CallFunction(0, "_test", *array_ptr())
Debug *array_ptr(0) ; Prints (in(1)<<16)|in(0)
Debug *array_ptr(1) ; Prints (in(3)<<16)|in(2) if in(3) existed
Debug *array_ptr(2) ; And so on...
El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by GPI.

Then I call it from PB:
Dim in.w(2)
in(0) = 10
in(1) = 20

CallFunction(0, "_test", @in) ; Library was initialized

Debug PeekW(@in) ; returns 30 (good)


debug peekw(@in+2); because of .w


PII 333, 256MB, Asus TNT2Ultra 32MB, AWE Gold 64 4MB
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

@in is NOT a pointer to the in() array, but a pointer to the in variable. See for yourself:

Code: Select all

Dim in.w(2)
in(0) = 10
in(1) = 20

Debug @in ; pointer to in variable
Debug in() ; pointer to the in() array
Debug PeekW(in()) ; Prints 10
Debug PeekW(@in) ; Prints 0 (variable uninitialized)
El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Manolo.

Yes,

Code: Select all

Dim in.w(2)
in(0) = 10
in(1) = 20
Debug PeekW(in()) ; Prints 10
Debug PeekW(in()+2) ; but this print 20. hehehe.

Manolo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tony_usa.

Thanks, it works!
Post Reply