callfunction() array return
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
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
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

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tinman.
--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.51, external editor)
This does not work in 3.62, but has worked in all previous versions of PB and hopefully will in the future:Originally posted by tony_usa
I know it is a lame question, but I have been unable to find an answer in previous posts.
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

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

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by El_Choni.
Array pointers are referred to like this:
This works:
If you want to return an array pointer:
And retrieve it like this (use only with long and string arrays, read below to know why):
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):
Not working (word data):
El_Choni
Array pointers are referred to like this:
Code: Select all
in()
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
Code: Select all
ProcedureReturn MyArray()
Code: Select all
*MyArrayCatcher() = CallFunction(whatever...)
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
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...
-
BackupUser
- PureBasic Guru

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

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
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:
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)
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Manolo.
Yes,
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.
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm