Passing all variables between Dinosaurs VB6 to Baby PB

Share your advanced PureBasic knowledge/code with the community.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Passing all variables between Dinosaurs VB6 to Baby PB

Post by Kwai chang caine »

Hello :D

Welcome in the new club of the dinosaurs users of VB6 !!!

Since several years, i try to passing all style of variables between PB and VB.
It's not simple for KCC, because VB not managing his variables like C and PB :?
He use BSTR, UNICODE ans SAFEARRAY :evil:

KCC have not big knowledge.. before he was helped by his MASTER...but now...his MASTER is tired to help KCC :(
Perhaps he don't like whirligig.....and KCC is the king for turned round.. :oops:
But i thanks him when even, for all his help since several month.... 8)

So, perhaps a day ...a member need to do this, and be happy to find this tips, and not eat all his fingers to the bone 8)

..........................................................................
.............................. THE STRINGS ..........................
..........................................................................

Passing a VB6 string by reference (Method allocate string by VB)

VB6 code

Code: Select all

Private Declare Sub GetMessage Lib "Fonctions.dll" (ByRef Chaine As String)

Private Sub Form_Load()
 Test
End Sub

Sub Test()
 
 ChDrive (Left(App.Path, 1))
 ChDir App.Path
 Dim Chaine As String

 Chaine = String(255, vbNullChar)
 GetMessage Chaine
 MsgBox "Longueur de la chaine = " + Str(Len(Chaine)) + Chr(13) + Chaine

End Sub
PB code

Code: Select all

ProcedureDLL GetMessage(*chaine)
 a$ = "Hello World !"
 CopyMemory(@a$, PeekL(*chaine), Len(a$))
EndProcedure 
Passing a VB6 string by value (Method allocate string by VB)

VB6 code

Code: Select all

Private Declare Sub GetMessage Lib "Fonctions.dll" (ByVal Chaine As String)

Private Sub Form_Load()
 Test
End Sub

Sub Test()

 ChDrive (Left(App.Path, 1))
 ChDir App.Path
 Dim Chaine As String
 Chaine = String(255, vbNullChar)
 GetMessage Chaine
 MsgBox "Longueur de la chaine = " + Str(Len(Chaine)) + Chr(13) + Chaine

End Sub
PB code

Code: Select all

ProcedureDLL GetMessage(*chaine)
 a$ = "Hello World !"
 CopyMemory(@a$, *chaine, Len(a$))
EndProcedure 
Last edited by Kwai chang caine on Wed Sep 29, 2010 4:21 pm, edited 15 times in total.
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Kwai chang caine »

Reserved
VB6 code PB code
Last edited by Kwai chang caine on Wed Sep 29, 2010 3:49 pm, edited 2 times in total.
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Kwai chang caine »

..............................................................................
...................... THE STRUCTURE (UDT) ..........................
..............................................................................

Passing a structure with three differents elements (Method VB allocate string)

VB6 code

Code: Select all

Private Declare Sub InitData Lib "Fonctions.dll" (Donnees As TDATA)

Private Type TDATA
 x As Integer
 y As Long
 strVariable As String
End Type

Private Sub Form_Load()
 Test
End Sub

Sub Test()

 ChDrive (Left(App.Path, 1))
 ChDir App.Path
 Dim Donnees As TDATA

 Donnees.strVariable = Space(30)
  
 InitData Donnees
 MsgBox "x = " + Str(Donnees.x) + Chr(13) + "y = " + Str(Donnees.y) + Chr(13) + "Len of sentence = " + Str(Len(Donnees.strVariable)) + Chr(13) + Donnees.strVariable

End Sub
PB code

Code: Select all

Structure DataPb
 x.i
 y.l
 strVariable.s
EndStructure

ProcedureDLL InitData(*Variable.DataPb)
 
 NewStrVariable$ = "String of lenght variable"
 *Variable\x = 15
 *Variable\y = 52445
 CopyMemory(@NewStrVariable$, @*Variable\strVariable, Len(NewStrVariable$))

EndProcedure 
Passing a structure of five differents elements (Method VB allocate string)

VB6 code

Code: Select all

Private Declare Sub InitData Lib "Fonctions.dll" (Donnees As TDATA)

Private Type TDATA
 x As Integer
 y As Long
 reel As Double
 strVariable As String
 strFixe As String * 11
End Type

Private Sub Form_Load()
 Test
End Sub

Sub Test()

 ChDrive (Left(App.Path, 1))
 ChDir App.Path
 Dim Donnees As TDATA

 Donnees.strVariable = Space(30)
 Donnees.strFixe = Space(11)
  
 InitData Donnees
 MsgBox "x = " + Str(Donnees.x) + Chr(13) + _
 "y = " + Str(Donnees.y) + Chr(13) + _
 "Reel = " + Str(Donnees.reel) + Chr(13) + _
 "String fixe = " + Donnees.strFixe + Chr(13) + _
 "Len of sentence = " + Str(Len(Donnees.strVariable)) + Chr(13) + _
 Donnees.strVariable
 

End Sub
PB code

Code: Select all

Structure DataPb
 x.i
 y.l
 Reel.d
 strVariable.s
 strFixe.s{11}
EndStructure

ProcedureDLL InitData(*Variable.DataPb)
 
 NewStrVariable$ = "String of lenght variable"
 *Variable\x = 15
 *Variable\y = 52445
 *Variable\reel = 2459.65
 CopyMemory(@"0123456789A", @*Variable\strFixe, SizeOf(*Variable\strFixe))
 CopyMemory(@NewStrVariable$, @*Variable\strVariable, Len(NewStrVariable$))

EndProcedure 
Last edited by Kwai chang caine on Fri Oct 01, 2010 5:02 pm, edited 10 times in total.
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Kwai chang caine »

Reserved
VB6 code PB code
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Kwai chang caine »

Reserved
VB6 code PB code
ImageThe happiness is a road...
Not a destination
Flower
User
User
Posts: 22
Joined: Fri Jan 08, 2010 8:05 am
Location: United States

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Flower »

I like your writing and posting style, KCC :mrgreen:
I don't have VB6 installed at this time, but you could try some secret function called StrConv, StrPtr & CopyMemory :wink:

Some references here: http://vb.mvps.org/tips/varptr.asp
(Declare Function is not necessary for XxxPtr on VB6)
Registered PureBasic user since 4.50
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Kwai chang caine »

The first member of my new club wrote:I like your writing and posting style, KCC
Thanks a lot FLOWER, and welcome in my new club 8)

Take a drink...it's the house who pay to you :mrgreen: (Not fred ...not FRED :oops: just little KCC :roll: )
Image

Yeeeaaahh !!!!!
My first client a nice little flower 8)
Can i put you, in a vase, for make my new THREAD CLUB more nice ???? :lol: :lol:

Thanks a lot for your link 8)
ImageThe happiness is a road...
Not a destination
Vitor_Boss®
User
User
Posts: 81
Joined: Thu Sep 23, 2010 4:22 am

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Vitor_Boss® »

Split strings

VB:

Code: Select all

Dim ExampleArray(0) As String
Dim Size As Integer
ExampleArray = Split("This|is|a|Split|Example", "|")
For Size = 0 to Ubound(ExampleArray) 'Size of array
 Debug.Print ExampleArray(Size) & vbCrLf
Next

Output is:
This
is
a
Split
Example
PB:

Code: Select all

Procedure Split(Array Result.s(1), Expression.s, Delimiter.s,Limit.l=-1)
  Protected C.w = 0
  Protected Size.w = 0
  Size=CountString(Expression,Delimiter)
    If Size=0
      ReDim Result(0)
      Result(0) = Expression
    Else
      If Limit > 0 And Size > Limit-1
        Size = Limit-1
      EndIf  
      ReDim Result(Size)
      For C = 0 To Size
        Result(C) = StringField ( Expression, C+1, Delimiter ); Thanks to peterb
      Next   
    EndIf
  EndProcedure


Dim ExampleArray.s(0)
Dim Size.w
Split(ExampleArray(),"This|is|a|Split|Example", "|")
For Size = 0 to ArraySize(ExampleArray()) 'Size of array
 Debug ExampleArray(Size) + Chr(10)+Chr(13) ; Thanks KCC
Next

Output is:
This
is
a
Split
Example
Last edited by Vitor_Boss® on Fri Oct 01, 2010 3:35 pm, edited 1 time in total.
Sorry by bad English.
HP Pavilion DV6-2155DX: Intel i3-330m 2.13 / 4GB DDR3 / 500GB Sata2 HD / Display 15.6" LED / Win7 Ultimate x64 / PB 4.50 x86 demo.
peterb
User
User
Posts: 60
Joined: Sun Oct 02, 2005 8:55 am
Location: Czech Republic
Contact:

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by peterb »

to Vitor_Boss®: in PB exist function StringField

Code: Select all


text.s      = "This|is|a|Split|Example"
delimiter.s = "|"

ubound      = CountString ( text, delimiter )

Dim ExampleArray.s( ubound )

For c = 0 To ubound
  ExampleArray( c ) = StringField ( text, c + 1, delimiter )
  Debug ExampleArray( c )
  
Next

User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Kwai chang caine »

1 / Another thing, apparently since some version of PB, there is a problem to passing array with procedureDLL :(
So i think that perhaps your code risk to create bug ... :(

2 / And a little fix in your PB call

Code: Select all

Dim ExampleArray.s(0)
Define Size.w
Split(ExampleArray(),"This|is|a|Split|Example", "|")

For Size = 0 To ArraySize(ExampleArray()) ;Size of array
 Debug ExampleArray(Size) + Chr(13)
Next
3 / In the VB code, it's interesting to see how you declare your DLL and the parameter BYVAL, BYREF, etc...
Because it's the more difficult to do :wink:

Thanks for your apport 8)
ImageThe happiness is a road...
Not a destination
Vitor_Boss®
User
User
Posts: 81
Joined: Thu Sep 23, 2010 4:22 am

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Vitor_Boss® »

@peterb
I'm newer on PB and don't know about that.
Thank you for improve the code. This is the way.

@KCC
This thread is only to DLL like functions?
I'm sorry, I'm not an expert in VB6 programming.
Sorry by bad English.
HP Pavilion DV6-2155DX: Intel i3-330m 2.13 / 4GB DDR3 / 500GB Sata2 HD / Display 15.6" LED / Win7 Ultimate x64 / PB 4.50 x86 demo.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Kwai chang caine »

Aaaaahhh !!!! yes i better understand now :lol:
You believe it's for converting VB code in PB or reverse :D

So it's also a good idea :idea: , but not the first idea of this thread :oops:
Yes, it's for using VB6 and PB together, and for do that just one solution is possible, because VB don't know create standard DLL. :evil:

Create PB exe.....create VB exe....and create PB Dll for working with the both applications :D
I'm sorry, I'm not an expert in VB6 programming.
With me...we are two :mrgreen:
ImageThe happiness is a road...
Not a destination
Vitor_Boss®
User
User
Posts: 81
Joined: Thu Sep 23, 2010 4:22 am

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Vitor_Boss® »

The title of this thread isn't specific for that. :o

Sorry but I read "Passing all variables between Dinosaurs VB6 to Baby PB" and not "Passing all variables from DLL between Dinosaurs VB6 and Baby PB" :o
Sorry by bad English.
HP Pavilion DV6-2155DX: Intel i3-330m 2.13 / 4GB DDR3 / 500GB Sata2 HD / Display 15.6" LED / Win7 Ultimate x64 / PB 4.50 x86 demo.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Passing all variables between Dinosaurs VB6 to Baby PB

Post by Kwai chang caine »

But what is the variable you passing between VB and PB in your code ????

You pass variable PB to a procedure PB and VB to a procedure VB, but never a variable PB to a procedure VB, or a variable VB to a procedure PB.
It's that BETWEEN VB and PB :mrgreen:

In fact it's not really forced to use DLL....
There are several method to passing variables between EXE (Pb or VB)
Pipe, Shared memory, and other....

If you have this style of code it's really interesting too 8)

The important things it's that a variable (Array, Udt...) is in VB and go to PB exe or dll..
Or a Variable is in PB and go to a VB exe...(Not Dll because VB6 DLL are activex, and not interesting...for me)

And the miracle.....it's variables (UDT, ArrayString, String....) is in VB, go to PB ...
is modify by PB..and return to VB.... :shock:


Since one year, i try to do this correctly and there are always a problem :?
I have use several method and never can finish one perfectly :(

SROD found always a problem in my code....often with the VB <===> PB :(
ImageThe happiness is a road...
Not a destination
Post Reply