Return PB DLL unicode string to VB6 [Resolved]

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

Return PB DLL unicode string to VB6 [Resolved]

Post by Kwai chang caine »

Hello at all :D

Again a problem of UNICODE :|

Usually i use this code since several years without problem

PB Dll code

Code: Select all

ProcedureDLL.s Try()
 Static TheReturn.s
 
 TheReturn = "Hello of KCC"
 ProcedureReturn TheReturn
EndProcedure 
VB6 code

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 Try Lib "MyDLL.dll" () 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" (ByVal lpString As Any) As Long

Private Sub Form_Load()
 
 ChDir App.Path
 HwndLib = LoadLibrary("D:\MyDLL.dll")
 Dim PtrReturn As Long, StringDLL As String
 PtrReturn = Try()
 StringDLL = Space$(lstrlen(PtrReturn))
 lstrcpy StringDLL, PtrReturn
 MsgBox StringDLL
 FreeLibrary HwndLib
 
End Sub
but obviously after the 5.40 PB is UNICODE and now all my VB program not works when i compile a new DLL with the new PB :|

I have try to replace the "lstrcpy and lstrlen" to "lstrcpyW and lstrlenW" without succes :|
And it's worst with "LoadLibraryA" to "LoadLibraryW", it would be too simple :mrgreen:

If someone know the solution

Have a good day
Last edited by Kwai chang caine on Wed Sep 27, 2017 4:50 pm, edited 2 times in total.
ImageThe happiness is a road...
Not a destination
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Return PB DLL unicode string to VB6

Post by normeus »

KCC,
Use memory viewer to see the output of any string manipulation you are doing,
This code will take your text and return a stream of characters in ASCII.
It might work with what you are trying to do

Code: Select all

ProcedureDLL.s Try()
   Protected TheReturn.s
   Static ToReturn.s
   
   ToReturn="Hello from KCC" ; text to be converted to ascii
   
   TheReturn =Space( StringByteLength(ToReturn)/2)
 PokeS(@TheReturn,ToReturn,-1,#PB_Ascii)
 ProcedureReturn TheReturn
EndProcedure 

asciitext$ = try()
ShowMemoryViewer (@asciitext$,18)
Debug asciitext$ ;// ascii  will show in debugger as random characters
Norm.
( sorry I didnt read the whole question :wink: )
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
mk-soft
Always Here
Always Here
Posts: 5334
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Return PB DLL unicode string to VB6

Post by mk-soft »

Not tested, but old code works with vb6

Code: Select all

;- Structure vbString

Structure vbString
  len.l
  text.s
EndStructure

ProcedureDLL.i MyText()

  Static result.vbString
  
  result\text = "Hello World!"
  
  result\len = Len(result\text)
  ProcedureReturn @result\text
  
EndProcedure

; VB
; Declare Unicode Function MyText Lib "MyDLL.dll" () As String
Edit
But i think this is right

Code: Select all

;- Structure vbString

Structure vbString
  len.l
  text.s{255}
EndStructure

ProcedureDLL.i MyText()

  Static result.vbString
  
  result\text = "Hello World!"
  
  result\len = StringByteLength(result\text)
  ProcedureReturn @result\text
  
EndProcedure

; VB
; Declare Unicode Function MyText Lib "MyDLL.dll" () As String

*r1 = MyText()
Debug PeekS(*r1)
Debug PeekL(*r1-4)
Not tested
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Return PB DLL unicode string to VB6

Post by Kwai chang caine »

First thanks at you two for your precious help 8)

@NORMEUS
It's another solution to change the return of dll function to ASCII :idea: i have not thinking that :wink:
But i'm forced to change all my return function in all my dll....
I keep this solution if i don't find how receive in VB a UNICODE string from PB, again thanks 8)

@MKSOFT
You have right, that works in VB6 if i compile the DLL in ASCII, but if i compile with v5.61 it's exactely the same behaviour
I have just the first character "H" who return in VB6

I believe now PB is UNICODE so UTF16 like TsSoft say a day to me..
It's perhaps that the problem, VB is perhaps UTF8...i continue to search in this way, perhaps with a little bit of chance .... :wink:
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Return PB DLL unicode string to VB6

Post by Kwai chang caine »

Yes it's that the problem, i have exactely the same problem, but in another sense (Sending VB6 string to Dll PB) :oops:
http://www.purebasic.fr/english/viewtop ... 13&t=69060
In this thread, and thanks to TsSoft, i remember what he say :
TsSoft wrote:UTF8 <> UTF16
You have to use UNICODE (UTF16) and not UNICODE (UTF8)!
And thanks to a code of Lunasole, i have write this, and that works now, i have the full string in VB6 :D

Code: Select all

ProcedureDLL.s Try()
 Static TheReturn.s
 
 TheReturn = Space(100)
 
 PokeS(@TheReturn, "Hello of KCC", -1, #PB_UTF8)
 
 ProcedureReturn TheReturn
EndProcedure 
Then, i have understand now, VB6 is UNICODE but UTF8, and PB now is also UNICODE but UTF16, FRED choose UTF16 by default, because the true UNICODE is UTF16 and not UTF8 (CROSOFT always do whatever :?)

So my problem now is to find how convert the UTF16 pointer of the PB Dll in UTF8 of VB6 (Finally if i have understand correctly :oops:)
ImageThe happiness is a road...
Not a destination
User avatar
mk-soft
Always Here
Always Here
Posts: 5334
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Return PB DLL unicode string to VB6

Post by mk-soft »

I forgetted VB6 don´t support 'Declare Unicode ...'

Now DLL-Function with Result as String...
Tested with VB6 :wink:

Code: Select all

;-VB6 Result as String

#MAX_TEXTLEN = 1024

Structure vbString
  len.l
  text.s{#MAX_TEXTLEN}
EndStructure

ProcedureDLL.i MyText()
  Static result.vbString
  Protected text.s
  text = Left("Hello World!", #MAX_TEXTLEN)
  PokeS(@result\text, text, -1, #PB_Ascii)
  result\len = Len(text)
  ProcedureReturn @result\text
EndProcedure
Module
Declare Function MyText Lib "MyDLL.dll" () as String
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Return PB DLL unicode string to VB6

Post by Kwai chang caine »

Yeees that works !!! :D
This is another way for read my DLL in VB6

Now i am three methods, and i'm happy of that : 8)

1/ Dll return ASCII simple pointer/Declare long in VB6 (Normeus method)
2/ Dll return UTF8 simple pointer/Declare long in VB6 (Kcc method)
3/ Dll return ASCII structured pointer/Declare String in VB6 (MkSoft method)

The problem, it's i'm forcing to modifie all my PB fonctions of all my DLLS, with thousand of functions
My first goal, is found a method to send simply the UTF16 pointer

Code: Select all

ProcedureDLL.s Try()
 Static TheReturn.s
 TheReturn = "Hello of KCC"
 ProcedureReturn TheReturn
EndProcedure 
and read it in VB6
Like this i not modify all my PB Dll just the VB6 code who whant use the DLL, and it's really much less works for me :wink:
If you have an idea...

I search since yesterday and for the moment i just found how read file with BOM in UTF16 in VB6, but not directly read a UTF16 pointer in VB6 :|

But the difficulty is now, numerous members have forget the old VB6 or even never use it :|
And the VB coders not know PB....i'm stranded between two languages...and it's really difficult to be helped by someone who know this two languages

I have exactely the same problem, they are a long time, when i need to use PB ASCII Dll with VB6...
When i have found, with help of VB and PB programmers, i believe my nightmare is finish...
Strong error....FRED changing PB to UNICODE...and the nightmare start again :cry:

So thanks a lot MkSoft for your really precious help, and also the others members, i feel not alone ahead this complex problem 8)
ImageThe happiness is a road...
Not a destination
User avatar
mk-soft
Always Here
Always Here
Posts: 5334
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Return PB DLL unicode string to VB6

Post by mk-soft »

:)

Small addon for parameter 'Text as String'

Code: Select all

;-VB6 Result as String

#MAX_TEXTLEN = 1024

Structure vbString
  len.l
  text.s{#MAX_TEXTLEN}
EndStructure

ProcedureDLL.i MyText()
  Static result.vbString
  Protected text.s
  text = Left("Hello World!", #MAX_TEXTLEN)
  PokeS(@result\text, text, -1, #PB_UTF8)
  result\len = Len(text)
  ProcedureReturn @result\text
EndProcedure

ProcedureDLL.i MyReverseString(*Text.integer)
  Static result.vbString
  Protected *vbText.vbString, MyText.s
  *vbText = *text\i - 4
  MyText = PeekS(@*vbText\text, *vbText\len, #PB_UTF8)
  MyText = ReverseString(MyText) + " - " + Str(*vbText\len)
  PokeS(@result\text, MyText, -1, #PB_UTF8)
  result\len = Len(MyText)
  ProcedureReturn @result\text
EndProcedure
Module 'Change Path'
Declare Function MyText Lib "... \Ablage\MyDLL.dll" () As String
Declare Function MyReverseString Lib "... \Ablage\MyDLL.dll" (text As String) As String
Form
Private Sub Command1_Click()
Dim text, text2 As String
Dim len1 As Long
text = MyText()
len1 = Len(text)
text2 = MyReverseString("Hallo")
MsgBox text & " - " & len1 & vbNewLine & text2
End Sub
Testet with VB6 :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Return PB DLL unicode string to VB6

Post by Kwai chang caine »

Yeaaah great, with passing parameter !!! 8)

Here that works even without module

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 MyText Lib "MyDLL.dll" () As String
Private Declare Function MyReverseString Lib "MyDLL.dll" (text As String) As String

Private Sub Form_Load()
  
 Dim text, text2 As String
 Dim len1 As Long
 
 ChDir App.Path
 hWndLib = LoadLibrary(App.Path + "\MyDLL.dll")
 
 text = MyText()
 len1 = Len(text)
 text2 = MyReverseString("Hallo")
 MsgBox text & " - " & len1 & vbNewLine & text2
 
 FreeLibrary hWndLib

End Sub
ImageThe happiness is a road...
Not a destination
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Return PB DLL unicode string to VB6

Post by djes »

Yeah, congratulations !!!!!! :D

Finally, today is the end of a looooooong time story.

KCC, I hope you will this print this code on your WC door, engrave it on your fridge, and tattoo it on your wife's as-ahem- arm.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Return PB DLL unicode string to VB6

Post by Kwai chang caine »

:lol: :lol:
Hello DJES 8)
Perhaps, i can put it also beside the picture of FRED on my nightstand :mrgreen:

In fact, i have the chance MkSoft help me 8) for maried a old horse with a splendid woman :shock:

Image

I let you guess who is the old horse :mrgreen:

But my first problem, is not really fully fix ....
In fact, i try to modify the VB6 code for can read the PB UTF16 pointer...

But after all this time, nobody know read and mainly writing in the marble,

Image

the old horse VB6 language... :lol:

Image

I have an idea .....perhaps RASHAD !!! the hieroglyph specialist :wink: 8) :lol:
ImageThe happiness is a road...
Not a destination
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Return PB DLL unicode string to VB6

Post by djes »

Kwai chang caine wrote: But my first problem, is not really fully fix ....
In fact, i try to modify the VB6 code for can read the PB UTF16 pointer...
Image
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Return PB DLL unicode string to VB6

Post by Kwai chang caine »

:lol:
Yes it's possible...don't be affraid my little SNOOPY, come with me take your sugar :mrgreen:

Another kind member of VB forum give to me a solution, i give it, if someone also crazy than little KCC, need to do this :D

In fact my problem,(explain the kind VB developper) it's the automatic variable conversion of this #$£%*µ of VB6 :?
And if i write this, i stop this automatic conversion...

PAF !!!
like a fly under my flying squirt :D

Image

This the miracle VB code against the bad fly :mrgreen:

Code: Select all

Private Declare Function SysReAllocString Lib "oleaut32" (ByVal pbstr As Long, ByVal psz As Long) As Long

Private Sub Form_Load()
 
 ChDir App.Path
 HwndDll = LoadLibrary("D:\MyDll.dll")
 
 Dim PointerDll As Long, StringDll As String
 PointerDll = Try()
 SysReAllocString VarPtr(StringDll), PointerDll
 Debug.Print StringDll
 
 FreeLibrary HwndDll
 
End Sub
Apparently..they are again a problem for freeing the variable "PointerDll "..i continue :D
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Return PB DLL unicode string to VB6

Post by Kwai chang caine »

Well...like promise, i'm back for the end of the story 8)

Image

Now i have 5 ways for read UTF16 PB DLL in VB6

For freeing the variable, i must use

Code: Select all

Private Declare Function SysFreeString Lib "oleaut32" (ByVal pbstr As Long) As Long
The 5e way is use

Code: Select all

StringDll = Space$(lstrlenW(ByVal PointerDll))
lstrcpyW ByVal StrPtr(StringDll), ByVal PointerDll
Seriously...only CROSOFT can invent this style of code..never i found without help of VB Master 8)

So i thanks another time all the actors of this thread 8)

@MkSoft
Mainly the great MkSoft for have spent numerous of his time for find a solution to send directly a BSTR to VB6 8)
And also spent time to testing on VB6 :wink:
Super intelligent to simulate a BSTR with a structure, for put the len before the string...seriously 2...only CROSOFT can invent this :?
For that i give you a french kiss :D

Image

Have the very best night of the year 8)
ImageThe happiness is a road...
Not a destination
Post Reply