Page 1 of 3

Passing VB Arrays

Posted: Mon Oct 04, 2004 5:26 pm
by ukandrewc
Can I pass a VB string array into PureBasic?

I want to create a PureBasic DLL that handles arrays created in VB. e.g.

In VB:
Redim MyArray(2) as string
MyArray(0)="one"
MyArray(1)="two"
MyArray(2)="three"

PureBasicFunction MyArray()

Any help would be appreciated, thanks

Posted: Tue Oct 05, 2004 7:54 am
by Seldon
This is how to pass the memory pointer of a VB array of bytes. With strings, it could be different as you must cope with VARIANT type (I think).
Anyway, this could be a good starting point.

VB:

Code: Select all

Private Declare Function My_Function Lib "My_dll.dll" (ByRef Array As Byte) As Long

Code: Select all

Dim MyArray(255) As Byte
Dim res As Long
res=My_Function(MyArray(0)) '1st element to pass its memory pointer
PB:

Code: Select all

ProcedureDLL My_Function(*mem_pointer)
 ProcedureReturn 0
EndProcedure

Posted: Tue Oct 05, 2004 9:15 am
by ukandrewc
Thanks for that

I am looking for a way to pass an array of strings

Posted: Sun Mar 04, 2007 11:44 pm
by SimpleMind
Seldon wrote:This is how to pass the memory pointer of a VB array of bytes. With strings, it could be different as you must cope with VARIANT type (I think).
Anyway, this could be a good starting point.

VB:

Code: Select all

Private Declare Function My_Function Lib "My_dll.dll" (ByRef Array As Byte) As Long

Code: Select all

Dim MyArray(255) As Byte
Dim res As Long
res=My_Function(MyArray(0)) '1st element to pass its memory pointer
PB:

Code: Select all

ProcedureDLL My_Function(*mem_pointer)
 ProcedureReturn 0
EndProcedure
Thanks Seldon! Your code example did the job. Now I can pass a string from MS-Word 2003 to PureBacis. Now, I'm starting the afterburner.

VBA-code:

Code: Select all

Declare Function InitString4 Lib "IS2.dll" (ByRef MyArray As Byte) As Long

Public Sub fromuc()
    Dim i As Long
    Dim x() As Byte
    x = StrConv("ABCDEFG", vbFromUnicode)
    a = InitString4(x(0))
End Sub
My PureBasic code:

Code: Select all

ProcedureDLL.l InitString4(*mem_pointer)
MessageRequester("Message from IS2.dll - peeks",PeekS(*mem_pointer))
  ProcedureReturn 1
EndProcedure

Posted: Thu May 28, 2009 3:01 pm
by Kwai chang caine
Hello at all

Like usual...i have not understand all the example, for pass an array of string of VB to Pure :oops:

I have do this.....but like i never must change a looser team.....
That's don't works :cry:


VB Code

Code: Select all

Private Declare Function InitString4 Lib "c:\MyDll.dll" (ByRef MyArray As Byte) As Long

Private Sub Form_Load()

 Dim Tablo(10) As String
 Tablo(1) = "Hello...i'm KCC"
 Tablo(2) = "i'm locked in VB :-("
 Tablo(3) = "and i want to jump in PB :-)"
 
 Dim x() As Byte
 x = StrConv(Tablo(1), vbFromUnicode)
 a = InitString4(x(0))

End Sub
Pure Code

Code: Select all

ProcedureDLL.l InitString4(*mem_pointer) 
 
 Dim Tablo.s(10)
 
 Repeat 
  
  Char = PeekB(*mem_pointer + Offset) 
  Offset + 1 
   
  If Char = #LF Or Char = #Null 
   LineNum + 1 
   MessageRequester("", Str(LineNum) + ".) " + PeekS(*mem_pointer + Marker, Offset - Marker - 2))
   Marker = Offset 
  EndIf 
  
 Until LineNum = 5
  
EndProcedure
Thanks

Re: Passing VB Arrays

Posted: Thu May 28, 2009 6:39 pm
by thearr
ukandrewc wrote:Can I pass a VB string array into PureBasic?

I want to create a PureBasic DLL that handles arrays created in VB. e.g.

In VB:
Redim MyArray(2) as string
MyArray(0)="one"
MyArray(1)="two"
MyArray(2)="three"

PureBasicFunction MyArray()

Any help would be appreciated, thanks
I'm not sure if it works, because I haven't got VB for ages, but you can try to test this:

VB:

Code: Select all

Dim MyArrayVB(1) as String
Dim MyArrayPB(1) as Long

MyArrayVB(0)="one"
MyArrayVB(1)="two"

MyArrayPB(0)= StrPtr(MyArrayVB(0))
MyArrayPB(1)= StrPtr(MyArrayVB(1))

MyFunctionPB MyArrayPB(0) 'Send pointer to PB DLL function (ByRef)
PB:

Code: Select all

ProcedureDLL MyFunctionPB(*ArrayFromVB)
 Dim MyArray.s(1)

 CopyMemory(*ArrayFromVB, @MyArray(), 2*4)
 MessageRequester(MyArray(0), MyArray(1))
EndProcedure

Posted: Thu May 28, 2009 6:55 pm
by Kaeru Gaman
strings in PB are pointers to the stringpool.

when you have an Array.s(19) this will be an Array of twenty pointers,
the strings are not anywhere near those Array.

you need to use fixlength strings in an array if you want the strings to be located there.

Posted: Fri May 29, 2009 8:34 am
by Kwai chang caine
@THEARR my savior of VB :D
Glad to talk to you another time

I have try your method but i have an error "TypeArgument BYREF incompatible" in VB
I have try to change the BYREF in the declaration by a BYVAL but i have another error "out of capacity" :cry:

VB Code

Code: Select all

Private Declare Function MyFunctionPB Lib "c:\MyDll.dll" (ByVal MyArray As Byte) As Long

Private Sub Form_Load()

 Dim MyArrayVB(1) As String
 Dim MyArrayPB(1) As Long
 
 MyArrayVB(0) = "one"
 MyArrayVB(1) = "two"
 
 MyArrayPB(0) = StrPtr(MyArrayVB(0))
 MyArrayPB(1) = StrPtr(MyArrayVB(1))
 
 MyFunctionPB MyArrayPB(0) 'Send pointer to PB DLL function (ByRef)

End Sub
PB Code

Code: Select all

ProcedureDLL MyFunctionPB(*ArrayFromVB) 
 Dim MyArray.s(1) 
 CopyMemory(*ArrayFromVB, @MyArray(), 2*4) 
 MessageRequester(MyArray(0), MyArray(1)) 
EndProcedure
@KAERU GAMAN
Thanks to explain me, and try to help me 8)

I believe that an array of string, is an array of pointers, only if the array is dynamic like this "Dim Array()" ??? :shock:
Because like we don't know the len of the variable, we have just the adress of her :roll:

Like this

Array.s(0) = @Array.s(0)
Array.s(1) = @Array.s(1)
Array.s(2) = @Array.s(2)

But when the array is STATIC like this "Dim Array(10)"
The variable is one after other separate by a chr(0)

Like this

Array.s(0) + chr(0) + Array.s(1) + chr(0) + Array.s(2) + chr(0)
I be mistaked ????

Posted: Fri May 29, 2009 9:55 am
by thearr
Kwaï chang caïne wrote:I have try your method but i have an error "TypeArgument BYREF incompatible" in VB
I have try to change the BYREF in the declaration by a BYVAL but i have another error "out of capacity" :cry:

VB Code

Code: Select all

Private Declare Function MyFunctionPB Lib "c:\MyDll.dll" (ByVal MyArray As Byte) As Long
You declare MyArray as Long and trying to pass it to the function as Byte, try to change MyFunction declaration.

Kwaï chang caïne wrote: But when the array is STATIC like this "Dim Array(10)"
The variable is one after other separate by a chr(0)
No, it's the array of pointers to null-terminated strings that are located somethere as Kaeru Gaman has written.

Posted: Fri May 29, 2009 10:06 am
by Kwai chang caine
I'm really a waffer :cry:
I have try all solution

Code: Select all

Private Declare Function MyFunctionPB Lib "c:\MyDll.dll" (ByRef MyArray As Long) As Long
Private Declare Function MyFunctionPB Lib "c:\MyDll.dll" (ByRef MyArray As byte) As Long
Private Declare Function MyFunctionPB Lib "c:\MyDll.dll" (Byval MyArray As Byte) As Long
Private Declare Function MyFunctionPB Lib "c:\MyDll.dll" (ByRef MyArray As Long) As Byte
etc .....
And or i have a message of error.....or the VB IDE crash :oops:

Code: Select all

I have also change 
Dim MyArrayPB(1) As Long
Dim MyArrayPB(1) As Byte
My savior wrote:No, it's the array of pointers to null-terminated strings that are located somethere as Kaeru Gaman has written.
Oooohh !!! because they are two type of array :shock:
Some array with chr(0) at the end of each recor
And some array without chr(o) ????? :shock:

Posted: Fri May 29, 2009 10:14 am
by thearr
Try this:

Code: Select all

***VB***
Private Declare Sub MyFunctionPB Lib "c:\MyDll.dll" (ByRef MyArray As Long)
***PB***
Dim MyArrayPB(1) as Long
And also declare MyArrayVB and MyArrayPB as global arrays.

Posted: Fri May 29, 2009 10:42 am
by Kwai chang caine
KCC do all the thing that THEARR say to him :D

VB FORM Code

Code: Select all

Private Declare Sub MyFunctionPB Lib "c:\MyDll.dll" (ByRef MyArray As Long)

Public Sub Form_Load()
 
 MyArrayVB(0) = "one"
 MyArrayVB(1) = "two"
 
 MyArrayPB(0) = StrPtr(MyArrayVB(0))
 MyArrayPB(1) = StrPtr(MyArrayVB(1))
 
 MyFunctionPB MyArrayPB(0) 'Send pointer to PB DLL function (ByRef)

End Sub
VB MODULE Code

Code: Select all

Public MyArrayVB(1) As String
Public MyArrayPB(1) As Long
PB Code

Code: Select all

ProcedureDLL MyFunctionPB(*ArrayFromVB) 
 Dim MyArray.s(1) 
 CopyMemory(*ArrayFromVB, @MyArray(), 2*4) 
 MessageRequester(MyArray(0), MyArray(1)) 
EndProcedure
And i obtain a msgbox with a "o" in the titlebar and a "t" in the prompt :shock:
When i click ok......VB crash

KCC is very very sad :cry:

Posted: Fri May 29, 2009 11:36 am
by thearr
Kwaï chang caïne wrote:And i obtain a msgbox with a "o" in the titlebar and a "t" in the prompt :shock:
When i click ok......VB crash

KCC is very very sad :cry:
OK, we've almost got it.

Now try to chek "Unicode executable" option in PB IDE before compiling DLL.

Posted: Fri May 29, 2009 11:59 am
by Rings
Vb uses safearrays.
A good documentation (for all VB-Types )
can be found here:
http://www.codeguru.com/vb/gen/vb_misc/ ... php/c7495/

Posted: Fri May 29, 2009 1:59 pm
by Kwai chang caine
@THEARR

Yes KCC again do that the master THEARR say....and KCC move :D
Now ....it's the same things, but in the title i have "One" and in the prompt i have "two" :D
And the VB crash :cry:

It's better, it's better continue the fight :D

If you have the same code, who don't crash the RAD....i'm interesting :D
Because i prefer close the VB with the button "quit" :lol:

Thanks for your help :wink:

@RINGS
Rings wrote:Vb uses safearrays.
A good documentation (for all VB-Types )
can be found here:
http://www.codeguru.com/vb/gen/vb_misc/ ... php/c7495/
Hello RINGS

I'm happy because i believe it's the first time i talk to you, since the beginning of the big adventure of "KCC in the land of the programming" 8)

I want to say to you, because i don't have the way before, that you are also one of my hero 8)
But KCC is sad because you are not enough on the forum now.
When i begin to read this forum ...the MASTER RINGS is always...
And now ..... :cry:

It' the reason why, i'm proud to see one of your answer for me 8)

Obviously ......you have right....i have see this story of "safearrays" somewhere :roll:
But like all the thing that KCC read........KCC not understand :D

Perhaps my SAVIOR THEARR understand,....... and what you say help him,.... to help me :D

I wish you a good day 8)