Passing array of DLL Pure to VB [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

Passing array of DLL Pure to VB [Resolved]

Post by Kwai chang caine »

Hello at all

Like usually, i try to do something, and i don't know how :oops:

I have create a DLL pure

Code: Select all

ProcedureDLL LeTablo(Pointeur.l)
 
 Dim Tablo.s(4)
 Tablo(1) = "Pure"
 Tablo(2) = "is "
 Tablo(3) = "your"
 Tablo(4) = "friend"
 ProcedureReturn @Tablo()

EndProcedure 
And i want to read this array in VB
I search a solution since an hour, and i have just found the inverse function.
But i have found this code for DLL in C.
But it's too difficult for my little head :oops:

Is it the more simple way for do this ???

Code: Select all

Private Declare Function AddLongs_Pointer Lib "MyStDll.dll" _
   (FirstElement As Long, ByVal lElements As Long) As Long

   Private Declare Function AddLongs_SafeArray Lib "MyStDll.dll" _
   (FirstElement() As Long, lSum As Long) As Long

  Private Sub Form_Load()
      Dim ArrayOfLongs(2) As Long
      Dim lSum As Long
      Dim k As Long

      ArrayOfLongs(0) = 1
      ArrayOfLongs(1) = 2
      ArrayOfLongs(2) = 3

      lSum = AddLongs_Pointer(ArrayOfLongs(0), UBound(ArrayOfLongs) + 1)
      MsgBox "Result with C array = " & Str$(lSum)

      k = AddLongs_SafeArray(ArrayOfLongs(), lSum)
      If k = 0 Then
         MsgBox "Result with Safearray = " & Str$(lSum)
      Else
         MsgBox "Call with Safearray failed"
      End If
   End Sub
Thanks for your precious help
Good day
Last edited by Kwai chang caine on Mon May 25, 2009 11:59 am, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Kwai you wouldn't be able to read that array if you were wearing bi-focal binoculars! The array is local to the function and will be destroyed as soon as the function ends!!!

You could use a static array (or global) but even then you haven't shown us the VB code which is attempting to read the array.

Personally I would get VB to pass a buffer to the PB dll which then places all the strings into the buffer. This avoids all the problems of freeing the array and freeing the individual strings etc.
I may look like a mule, but I'm not a complete ass.
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 »

Kwai you wouldn't be able to read that array if you were wearing bi-focal binoculars!
:lol: :lol:
You too, you like the humour :D

There no example in the forum in this sense.
And in the forum VB, the programmer don't know PB :?

There have some example with the great C.
But i'm too small for the great C :D
I'm always too small for the big pure, then ..... :oops:

I'm like the luky luke cow boy, lonesome :cry:
Why the gods create me with an empty head :?
ImageThe happiness is a road...
Not a destination
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

I think this could work, but i am not sure:

Code: Select all

ProcedureDLL LeTablo(Pointeur.l)
 
 Static Dim Tablo.s{100}(4)
 Tablo(1) = "Pure"
 Tablo(2) = "is "
 Tablo(3) = "your"
 Tablo(4) = "friend"
 ProcedureReturn @Tablo()

EndProcedure 
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
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 have create this function, with help of VbForum there have several month.

Code: Select all

Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long

Public Function StringDLL(Variable)

 Dim RetourAdresse As Long, RetourDll As String
 RetourAdresse = Variable
 RetourDll = Space$(lstrlen(RetourAdresse))
 lstrcpy RetourDll, RetourAdresse
 StringDLL = RetourDll
 
End Function
I use it all the day for read the string value of a return of my dll :D
But it's not works apparently with the array :?
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

Post by Kwai chang caine »

Thanks my god TsSoft 8)
I try this immediatly
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Does the following help :

Code: Select all

ProcedureDLL LeTablo() 
 Static  Dim Tablo.s(4) 
 Tablo(0) = "Pure" 
 Tablo(1) = "is " 
 Tablo(2) = "your" 
 Tablo(3) = "friend" 
 ProcedureReturn @Tablo() 
EndProcedure 


;Some code to demonstrate how to access the static array from the above function.

;Method 1.
;---------
  *ptr.string = LeTablo()
  For i = 0 To 3
    Debug *ptr\s
    *ptr+SizeOf(LONG)
  Next

Debug "===================="

;Method 2.
;---------
  *ptr = LeTablo()
  For i = 0 To 3
    Debug PeekS(PeekL(*ptr))
    *ptr+SizeOf(LONG)
  Next
Using a static array like this will not be threadsafe, which is why I recommend using a buffer as described in my previous post.
I may look like a mule, but I'm not a complete ass.
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 »

Thanks at you two
But in the VB code, there have two declaration :shock:

Code: Select all

Private Declare Function AddLongs_Pointer Lib "MyStDll.dll" _ 
   (FirstElement As Long, ByVal lElements As Long) As Long 

   Private Declare Function AddLongs_SafeArray Lib "MyStDll.dll" _ 
   (FirstElement() As Long, lSum As Long) As Long 
Is it obliged ??
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

Post by Kwai chang caine »

I have try this and no good result :cry:

DLL

Code: Select all

ProcedureDLL LeTablo() 
  
 Static Dim Tablo.s{100}(4) 
 Tablo(1) = "Pure" 
 Tablo(2) = "is " 
 Tablo(3) = "your" 
 Tablo(4) = "friend" 
 ProcedureReturn @Tablo() 

EndProcedure 
VB

Code: Select all

Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString1 As Any) As Long
Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long
Private Declare Function LeTablo Lib "c:\EssaiTablo.dll" () As Long

Public Function StringDLL(Variable)
 Dim RetourAdresse As Long, RetourDll As String
 RetourAdresse = Variable
 RetourDll = Space$(lstrlen(RetourAdresse))
 lstrcpy RetourDll, RetourAdresse
 StringDLL = RetourDll
End Function

Private Sub Form_Load()
 EssaihWnd = LoadLibrary("c:\EssaiTablo.dll")
 MsgBox StringDLL(LeTablo())
 FreeLibrary (EssaihWnd)
End Sub
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

Post by Kwai chang caine »

It's the begin of the beginning :lol:

I have try this

PB

Code: Select all

ProcedureDLL LeTablo() 
 Static  Dim Tablo.s(4) 
 Tablo(0) = "Pure" 
 Tablo(1) = "is " 
 Tablo(2) = "your" 
 Tablo(3) = "friend" 
 ProcedureReturn @Tablo() 
EndProcedure 
VB

Code: Select all

Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString1 As Any) As Long
Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long
Private Declare Function LeTablo Lib "c:\EssaiTablo.dll" () As Long

Public Function StringDLL(Variable)
 Dim RetourAdresse As Long, RetourDll As String
 RetourAdresse = Variable
 RetourDll = Space$(lstrlen(RetourAdresse))
 lstrcpy RetourDll, RetourAdresse
 StringDLL = RetourDll
End Function

Private Sub Form_Load()
 EssaihWnd = LoadLibrary("c:\EssaiTablo.dll")
 MsgBox StringDLL(LeTablo())
 FreeLibrary (EssaihWnd)
End Sub
And i have several square and hieroglyph :shock: in the msgbox
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

Post by Kwai chang caine »

There have news :D
Because KCC is a mule, but he try anything, normal he understand nothing :D

But this time, Kcc have a return, YOUPI !!!! :D
I have the word "Pure" in my msgbox
Kcc is happy
But Kcc, wanted more, he want all the word :?
Kcc think that is the fault of Chr(13), chr(0) or a thing like that.

But Kcc don't know what is "the thing like that" :cry:

PB

Code: Select all

ProcedureDLL LeTablo() 
 Static  Dim Tablo.s(4) 
 Tablo(0) = "Pure" 
 Tablo(1) = "is " 
 Tablo(2) = "your" 
 Tablo(3) = "friend" 
 ProcedureReturn @Tablo(0) 
EndProcedure 
VB

Code: Select all

Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString1 As Any) As Long
Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long
Private Declare Function LeTablo Lib "c:\EssaiTablo.dll" () As Long

Public Function StringDLL(Variable)
 Dim RetourAdresse As Long, RetourDll As String
 RetourAdresse = Variable
 RetourDll = Space$(lstrlen(RetourAdresse))
 lstrcpy RetourDll, RetourAdresse
 StringDLL = RetourDll
End Function

Private Sub Form_Load()
 EssaihWnd = LoadLibrary("c:\EssaiTablo.dll")
 Debug.Print StringDLL(LeTablo())
 FreeLibrary (EssaihWnd)
End Sub
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

Post by Kwai chang caine »

Nobody know how to do for return all the array in VB in one time ????? :cry:
ImageThe happiness is a road...
Not a destination
thearr
User
User
Posts: 21
Joined: Sun Apr 26, 2009 3:18 am
Location: RU

Post by thearr »

Maybe this can help?

PB

Code: Select all

ProcedureDLL LeTablo()
 Static  Dim Tablo.l(4)
 Tablo(0) = @"Pure"
 Tablo(1) = @"is "
 Tablo(2) = @"your"
 Tablo(3) = @"friend"
 ProcedureReturn @Tablo(0)
EndProcedure
VB

Code: Select all

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest as Any, source as Any, ByVal size as Long)

Dim Tablo(0 to 3) as Long
CopyMemory Tablo(0), ByVal LeTablo(), 16

For i = 0 to 3
 Debug.Print StringDLL(Tablo(i)) 'Use your code to get the string
Next i
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 »

thearr you have a new friend :D

Since october 2008, i search a solution to pass a full array of a PB DLL to VB in one time ...

In the french forum DENIS help me, but it is not in one function :roll:

The problem when i programming VB and PB together, it's that in this forum they are no much VB programmer, they are much C programmer.

And in the VB forum, when i talk of a DLL, all believe that KCC is a C programmer. :lol:

Here.... always know KCC, it's like a family, and know he is a wafer programming, but nobody know him in the VB forum
Then when i say the DLL is in PB, always go and not help KCC :cry:

They believe PB is a language for baby....a toy for fun :?
So KCC is alone, when he ask this type of question, box in two language :cry:

Thanks to you, KCC is happy because he can pass an array to VB now :D
And it's very very important for him 8)

I never know how i can to thank you :roll:
Perhaps i give to you, a big french kiss :lol:
Like i have already give to several kind and strong members of this forum :wink:

Image

I wish you a very very good day

Just a last question :roll:
They are no way to find the end of the array ?????
Last edited by Kwai chang caine on Wed Jun 03, 2009 9:56 am, edited 1 time 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

Post by Kwai chang caine »

I move a little bit thanks the code of THEARR

Now, i can't know the size of the array, i have the idea to put it in the 0 element.

Like this :

Code: Select all

ProcedureDLL TabloDansLaDll()

 Static Dim Tablo.s(10)
 
 x = 0
 x + 1:Tablo(x) = "Hello"
 x + 1:Tablo(x) = "i'm an"
 x + 1:Tablo(x) = "array"
 x + 1:Tablo(x) = "made and"
 x + 1:Tablo(x) = "managing by"
 x + 1:Tablo(x) = "a PB DLL"
 x + 1:Tablo(x) = "and a GOD of VB"
 x + 1:Tablo(x) = "can"
 x + 1:Tablo(x) = "read me in a VB program"
 x + 1:Tablo(x) = ":-)"
 Tablo(0) = Str(x)
 
 ProcedureReturn @Tablo()
 
EndProcedure
And i use this code in Visual Basic to read the DLL

Code: Select all

 Dim Tablo() As Long, AdrLongTablo As Long, LongTablo As Integer
 CopyMemory AdrLongTablo, ByVal TabloDansLaDll(), 4
 LongTablo = StringDLL(AdrLongTablo) + 1
 ReDim Tablo(0 To LongTablo)
 CopyMemory Tablo(0), ByVal TabloDansLaDll(), LongTablo * 4
  
 For i = 1 To UBound(Tablo())
  'Debug.Print "Adresse : " + Str(Tablo(i))
  Debug.Print StringDLL(Tablo(i))
 Next i
Can you say to me if it's the better way ??? :roll:
Or perhaps they are a way to know the size of the array ????
ImageThe happiness is a road...
Not a destination
Post Reply