Page 1 of 1
A DLL can return an array ?? [Resolved]
Posted: Wed Jun 20, 2007 9:52 am
by Kwai chang caine
Hello at all
Excuse my very bad english
I make this code :
Code of the DLL_Test :
Code: Select all
ProcedureDLL AttachProcess(instance)
Global Dim Array.s(5)
EndProcedure
ProcedureDLL CreateArray()
For i = 1 To 5
Array(i) = "Line " + Str(i)
Next
ProcedureReturn @Array(1)
EndProcedure
Code of the exe :
Code: Select all
Dim Array.s(5)
If OpenLibrary(0, "dll_test.dll")
ArrayAdress.l = CallFunction(0, "CreateArray")
Array(1) = PeekS(ArrayAdress)
For i = 1 To 5
Debug Array(i)
Next
CloseLibrary(0)
EndIf
Why the result return only the first line of the array :roll:
Thank you very much for your help
Re: A DLL can return an array ??
Posted: Wed Jun 20, 2007 10:12 am
by Trond
It's not possible to return an array. You must pass the array as a parameter to the function and fill it in.
Posted: Wed Jun 20, 2007 10:18 am
by Kwai chang caine
I'm very disappointed
I believed that pure knew make everything

Posted: Wed Jun 20, 2007 3:58 pm
by Psychophanta
Try this:
Code: Select all
ProcedureDLL AttachProcess(instance)
Global Dim Array.s(5)
EndProcedure
ProcedureDLL.l CreateArray()
For i=1 To 5
Array(i)="Line "+Str(i)
Next
ProcedureReturn @Array()
EndProcedure
and
Code: Select all
Dim Array.s(5)
If OpenLibrary(0,"dll_test.dll")
*ArrayAdress.l=CallFunction(0,"CreateArray")
Array()=*ArrayAdress
For i=1 To 5
MessageRequester("hi",Array(i))
Next
CloseLibrary(0)
EndIf
Posted: Wed Jun 20, 2007 4:24 pm
by Flype
'Static Dim' allow such manipulations.
Posted: Wed Jun 20, 2007 6:27 pm
by Trond
Flype wrote:'Static Dim' allow such manipulations.
Global Dim = Static Dim.
But each PB array is 8 bytes, the example above sets only 4 bytes, so something is bound to go wrong sometime unless someone knows exactly how PB's arrays are implemented.
Posted: Thu Jun 21, 2007 9:42 am
by Kwai chang caine
Thank you Psychophanta for your best code
It's work's fine
And thanks two at FLYPE for his help
I'm glad to see that PureBasic can make everything
I wish you a good day
Posted: Thu Jun 21, 2007 11:05 am
by Trond
Kwaï chang caïne wrote:It's work's fine
It has a memory leak...
Posted: Thu Jun 21, 2007 2:43 pm
by Flype
Trond wrote:Flype wrote:'Static Dim' allow such manipulations.
Global Dim = Static Dim.
But each PB array is 8 bytes, the example above sets only 4 bytes, so something is bound to go wrong sometime unless someone knows exactly how PB's arrays are implemented.
Trond wrote:It has a memory leak...
please trond, can you explain a bit more, i really don't understand the problem.
is this legal or not ?
if so, what's the problem and how to do what Kwaï chang caïne requested ?
Posted: Thu Jun 21, 2007 6:05 pm
by Psychophanta
Flype wrote:
is this legal or not ?
It is not legal in fact.
You can not re-assign the address of an already created Dim.
See
http://www.purebasic.fr/english/viewtopic.php?p=200183
anyway,
@Kwaï chang, you should use structures with [0] inside, instead of arrays with Dim:
Here is a completely safe example i think:
Code: Select all
;DLL:
ProcedureDLL AttachProcess(instance)
Global Dim Array.s(5)
EndProcedure
ProcedureDLL.l CreateArray()
For i=0 To 5
Array(i)="Line "+Str(i)
Next
ProcedureReturn @Array()
EndProcedure
Code: Select all
;EXE:
Structure array
item.s[0]
EndStructure
If OpenLibrary(0,"dll_test.dll")
*var.array=CallFunction(0,"CreateArray")
For i=0 To 5
MessageRequester("hi",*var.array\item[i])
Next
CloseLibrary(0)
EndIf
Posted: Thu Jun 21, 2007 10:06 pm
by Kwai chang caine
@Psychophanta
Thank you for your help
