...continua...
" >in VB6 I can
> use "iArray" as a variable and it is assumed to mean
> "iArray(0)", Or, I could have specified the element "iArray(0)" as
> an argument in a function expecting a simple variable.
The difference is that in Gambas arrays are objects.
> I read that objects can be passed as arguments
True, but not to library written in C which doesn't support objects at all.
And with other languages, I don't think the object structure in memory is
same (=compatible). However you can pass objects as arguments in libraries
written with Gambas.
Jussi "
----
Ad ogni modo io propongo una cosa così:
' void *memcpy(void *str1, const void *str2, size_t n)
Private Extern memcpy(str1 As Pointer, str2 As Pointer, n As Integer) In "libc:6"
Public Sub Main()
Dim src, dest As Pointer
Dim s As String = "Questa è una prova con la funzione 'memcpy()'."
src = Alloc(s)
dest = Alloc(Len(s))
memcpy(dest, src, Len(s))
Print "Dopo memcpy, " & Quote("dest") & ":\n"; String@(dest)
Free(src)
Free(dest)
End
Un esempio fra molti, mescolando risorse native di Gambas e funzioni esterne, potrebbe essere la copia di dati da un vettore di tipo Byte[ ] a un altro vettore di tipo Byte[ ]:
Library "libc:6"
' void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n)
' Copy N bytes of SRC to DEST.
Private Extern memcpy(__dest As Pointer, __src As Pointer, __n As Long)
Public Sub Main()
Dim sor As Byte[]
Dim des As Byte[]
Dim b As Byte
sor = [11, 22, 33, 44, 55, 66, 77, 88]
des = New Byte[sor.Count]
' Copia un numero "sor.Count" di dati dall'area di memoria di "sor" nell'area di memoria di "des":'
memcpy(des.Data, sor.Data, sor.Count)
' Verifica il risultato:'
For Each b In des
Print b
Next
End
Di seguito un esempio simile al precedente, ma reso più complesso, poiché i dati andranno scritti nell'apposito membro (void *data) della Struttura, chiamata CARRAY e contenuta nel file sorgente ".../main/gbx/gbx_c_array.h" dell'Interprete.
Private Const NUM As Integer = 8
Library "libc:6"
' void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n)
' Copy N bytes of SRC to DEST.
Private Extern memcpy(__dest As Pointer, __src As Pointer, __n As Long)
Public Sub Main()
Dim p As Pointer
Dim sor, des As Byte[]
Dim b As Byte
sor = New Byte[]
sor = [11, 22, 33, 44, 55, 66, 77, 88]
des = New Byte[NUM]
' Penetra nella Struttura denominata CARRAY, contenuta nel file sorgente ".../main/gbx/gbx_c_array.h":'
p = Object.Address(des)
' Copia una quantità "NUM" di dati dall'area di memoria di "sor" nell'area di memoria di "des":'
memcpy(Pointer@(p + 32), sor.Data, NUM)
' Verifica il risultato:'
For Each b In des
Print b
Next
End
Vedi anche:
http://www.gambas-it.org/wiki/index.php?title=Configurazione,_organizzazione_ed_impostazione_dell%27array_secondo_i_sorgenti_di_Gambas
Aggiungo quest'altro caso molto più complesso.
...ricordate queste mie parole ?
CHI DOMINA la MEMORIA, DOMINA il MONDO degli "OGGETTI"
Ogni "Oggetto" di Gambas è innanzitutto costituito da più porzioni di memoria, organizzate secondo le specifiche Strutture previste dai sorgenti di Gambas per ciascun Oggetto, dedicate a immagazzinare i dati che caratterizzeranno gli Oggetti medesimi.
Poter e sapere dunque accedere alle aree di memoria di un Oggetto, significa poter modificare i valori delle sue proprietà.
Con un'operazione di ingegneria si possono modificare "geneticamente" Oggetti che in apparenza sono quelli originari, e invece fanno riferimento ad altri Oggetti.
Ponete sul Form una "PictureBox" e un "TextBox".
Library "libc:6"
' void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n)
' Copy N bytes of SRC to DEST.
Private Extern memcpy(__dest As Pointer, __src As Pointer, __n As Long)
Public Sub Form_Open()
Dim p1, p2 As Pointer
Dim t1, t2 As Pointer
Dim s As String
With PictureBox1
.Background = Color.Yellow
.Foreground = Color.Blue
End With
' Accediamo profondamente nelle Strutture sorgenti della "PictureBox":'
p1 = Object.Address(PictureBox1)
p2 = Pointer@(p1)
' Accediamo profondamente nelle Strutture sorgenti del "TexteBox":'
t1 = Object.Address(TextBox1)
t2 = Pointer@(t1)
' Facciamo in modo che la "PictureBox" punti alle risorse della "TextBox" !!!'
memcpy(p1, t1, 144)
memcpy(p2, t2, 512)
' ...è la PictureBox a spostarsi ?'
PictureBox1.X = 500
' Scusate...ma...quando mai la PictureBox ha avuto la Proprietà ".Text" ?!?!?'
PictureBox1.Text = PictureBox1.Name
' Eeeh ! ...che nome ha la "PictureBox" ???????'
Print PictureBox1.Text, TextBox1.Text
' Scoprirete che le risorse della PictureBox non sono più quelle della Classe PictureBox, bensì della TextBox !!!'
For Each s In Object.Class(PictureBox1).Symbols
Print s
Next
End
Public Sub TextBox1_Enter() ' Provate a passare con il mouse sulla "PictureBox".... '
Print "Passaggio del Mouse sul TextBox1 ?"
End
Chiameremo questo caso: Oggetti Geneticamente Modificati ;D