A DLL can return an array ?? [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

A DLL can return an array ?? [Resolved]

Post by Kwai chang caine »

Hello at all

Excuse my very bad english :oops:

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
Last edited by Kwai chang caine on Thu Jun 21, 2007 9:45 am, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: A DLL can return an array ??

Post 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.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

I'm very disappointed

I believed that pure knew make everything :cry:
ImageThe happiness is a road...
Not a destination
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

'Static Dim' allow such manipulations.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Thank you Psychophanta for your best code 8)

It's work's fine

And thanks two at FLYPE for his help :wink:
I'm glad to see that PureBasic can make everything :D

I wish you a good day
ImageThe happiness is a road...
Not a destination
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Kwaï chang caïne wrote:It's work's fine
It has a memory leak...
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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 ?

Code: Select all

Array()=*ArrayAdress
if so, what's the problem and how to do what Kwaï chang caïne requested ?
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Flype wrote: is this legal or not ?

Code: Select all

Array()=*ArrayAdress
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
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

@Psychophanta
Thank you for your help 8)
ImageThe happiness is a road...
Not a destination
Post Reply