Page 1 of 1

Unicode strange behaviour

Posted: Tue Aug 29, 2017 1:09 pm
by Kwai chang caine
Hello at all :D

Today, i have again a strange behaviour to show :|

I use VB6 sometime, for calling PB dll
But like now, VB is unicode only, all my VB6 program not works when i recompile the old DLL with v5.60 :|

Apparently VB6 is UNICODE, but call library fonction in ASCII, it's the reason why all works very well with v5.40 and ASCII compilation 8)
So i have use the UTF8() function for convert the ASCII variable from VB6, and that not works :(

VB6 code

Code: Select all

Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Public Declare Function LoadLibraryA Lib "kernel32" (ByVal lpLibFileName As Any) As Long
Public Declare Function EssaiVb6 Lib "LaDll.dll" Alias "Essai" (ByVal Texte As String) As Long

Private Sub Form_Load()
 ChDir App.Path
 HwndLib = LoadLibraryA(App.Path + "\LaDll.dll")
 EssaiVb6("A que coucou les quéqués")
 FreeLibrary HwndLib
End Sub
Pb Dll

Code: Select all

ProcedureDLL.s Essai(Texte.s)
 *UTF8 = UTF8(Texte)
 Texte = PeekS(*UTF8, -1, #PB_UTF8)
 MessageRequester("Unicode variable", Texte)
 ProcedureReturn Texte
EndProcedure 
Since 4 hours, i have all try with VB and PB, and i have no new idea :|
But the worst, that works with this function "Ansi2Uni", and not with Fred UTF8() function :shock:

Code: Select all

Procedure.l Ansi2Uni(ansi.s)
  size.l=MultiByteToWideChar_(#CP_ACP,0,ansi,-1,0,0)
  Dim unicode.w(size)
  MultiByteToWideChar_(#CP_ACP, 0, ansi, Len(ansi) * 2, unicode(), size)
  ProcedureReturn @unicode() 
EndProcedure 

ProcedureDLL.s Essai(Texte.s)
 *pointeur = Ansi2Uni(Texte)
 Texte = PeekS(*pointeur)
 MessageRequester("Unicode variable", Texte)
 ProcedureReturn Texte
EndProcedure 
If someone understand something
Have a good day

Re: Unicode strange behaviour

Posted: Tue Aug 29, 2017 2:28 pm
by ts-soft
UTF8 <> UTF16

You have to use UNICODE (UTF16) and not UNICODE (UTF8)!

Re: Unicode strange behaviour

Posted: Tue Aug 29, 2017 2:40 pm
by Kwai chang caine
Hello TsSoft, the specialist in COM, OLE 8)
Very happy to read you, since a long time :D

Oooook !!!! this is the reason why !!! :shock:
So i can't use the Fred function, i'm forcing to use this one ?

Code: Select all

Procedure.l Ansi2Uni(ansi.s)
  size.l=MultiByteToWideChar_(#CP_ACP,0,ansi,-1,0,0)
  Dim unicode.w(size)
  MultiByteToWideChar_(#CP_ACP, 0, ansi, Len(ansi) * 2, unicode(), size)
  ProcedureReturn @unicode()
EndProcedure 

Re: Unicode strange behaviour

Posted: Tue Aug 29, 2017 3:10 pm
by Kwai chang caine
Believe you it's possible to forcing VB to send UTF8 ?

Re: Unicode strange behaviour

Posted: Tue Aug 29, 2017 3:15 pm
by Lunasole
Hi. This should work fine ^^

Code: Select all

EnableExplicit


; str$	:	PB unicode string
; RETURN:	ascii string packed into PB unicode string
Procedure$ ToAscii (str$)
	Protected out$ = Space(1 + Len(str$) / 2)
	PokeS(@out$, str$, -1, #PB_Ascii)
	ProcedureReturn out$
EndProcedure
; back to unicode
Procedure$ FromAscii (str$)
	ProcedureReturn PeekS(@str$, -1, #PB_Ascii)
EndProcedure



ProcedureDLL.s Essai(Texte.s)
 texte = FromAscii(texte)
 MessageRequester("Unicode variable", texte)
 
 texte = ToAscii(texte)
 ProcedureReturn Texte
EndProcedure
I'm often using those "packed strings" as it is better than having mess with memory buffers, etc


Kwai chang caine wrote:Believe you it's possible to forcing VB to send UTF8 ?
That's possible using WinAPI. Also there was function StrConv or something like it in VB6, can use it and send raw bytes array (or its pointer) to PB.
I would give you some code but don't have much wish to go back to VB :D

Re: Unicode strange behaviour

Posted: Tue Aug 29, 2017 3:58 pm
by Kwai chang caine
Thanks a lot LUNASOLE 8)
Lunasole wrote:I would give you some code but don't have much wish to go back to VB
You have right, and i understand you very well :lol:

But my problem is other, i don't want go back to VB6 too, but just continue to keep alive several big software i have create they are long...long..long
Imagetime :lol:

And sometime i need to modify a little bit the DLL, it's for that i have this problem :|

So if i have good understand you, i have two choices :

1/ Spent tons of hours to adding
- A converter Ascii/Unicode at each enter of each procedure of the DLL
- A converter Unicode/Ascii at each exit of each procedure of the same DLL :cry:

2/ Spent tons of hours to adding :
- A converter like "StrConv " at each call of functions, and each return of my DLL :cry:

I think i choose the first choice.....

Again a time, thanks at all, for all your very precious help 8)

Re: Unicode strange behaviour

Posted: Tue Aug 29, 2017 7:50 pm
by ts-soft
3. Choise: No Converter, use always UNICODE, no ascii, no utf-8.

Display Unicode Strings in Visual Basic 6.0

Re: Unicode strange behaviour

Posted: Tue Aug 29, 2017 9:23 pm
by Lunasole
ts-soft wrote:3. Choise: No Converter, use always UNICODE, no ascii, no utf-8.

Display Unicode Strings in Visual Basic 6.0
Oh I remember similar hell stuff. Don't remember exactly, but it had some "critical flaws" making that unusable in many cases.
Probably simpler to add 4 - just use older PB compiler (5.44 or whatever) with native ASCII, though @KCC already said about it

Re: Unicode strange behaviour

Posted: Wed Aug 30, 2017 9:13 am
by Kwai chang caine
@TSSOFT

I have nothing against using UNICODE in the two side (VB6 and Pb Dll), for me, the main thing it's VB6 and PB can exchange data 8)
But VB6 is apparently UNICODE and this donkey, send his string parameter in ASCII, translate them dynamically on the fly, and no choice for change that apparently :shock:

Furthermore, it's when even strange Fred no choose the UTF16 by default in PB, then i have read on the web the real UNICODE is the UTF16, and it's the reason why numerous program use UTF16 against UTF8. :shock:
Finally, i have create a wrapper for each call of my DLL, and replace each string by "StrConv(String)" like this, that works.... for the moment :mrgreen:

Thank you very much MASTER for give me the tips, you save my life 8)
Because never i thinking to that as FRED not create the #PB_UTF16 or UTF16(), :oops: and however i promise you, i have try tons of things, preferably whatever (Kcc method :mrgreen: ) before create this post :oops:

@LUNASOLE

Your code is perfect, and works very well 8)
It is short and powerfull, like Kcc love it :D
At the begining i put it everywhere in my DLL, but i have see it's too long to modify several hundred of functions :|
So finally i have create a wrapper in vb6, like i have say to TsSoft :wink:
But i keep it preciously, because since the Fred decision, i have alway a problem with this UNICODE of misfortune :?
Again thanks MASTER to your precious help 8)

Re: Unicode strange behaviour

Posted: Wed Aug 30, 2017 7:42 pm
by Lunasole
Hah, too many good words about my code ^^ Mostly I'm just too lazy to write something longer and looking "truly professional".
Anyway glad you liked it, good luck with that VB stuff (probably I'll soon play with it too, there are several large programs also made long ago with it)

Re: Unicode strange behaviour

Posted: Thu Aug 31, 2017 8:44 am
by Kwai chang caine
And again...often i don't dare writing the real words i want to write.
Since all this time, the old members, know kcc is the worst programmer of the west, and also a little bit crazy :lol:

But i have already say that so much time, to have the chance to read, write, laugh, and Furthermore sharing a part of big knowledge of this numerous MASTERS,
It's the more nice present i had in all my life.
I have this chance all the days, how much fan can do all that with his heroes, idol, gods ???

It's like that, since he are 16 years ago, kcc love programming, but the programmation not like kcc :lol:
And thanks at all masters of the pb forum, like you, and several others of this forum, kcc can when even do fully his passion...even with his little brain :mrgreen:
Then....what style of word, kcc must use for thanking all the days his heroes ???

It's the reason why, often, members believe that kcc fulsome, overdone, with his thanks, and his words "masters", "heroes", etc ...for just a code or a help...
Never a member can understand, what the value of this help, have for kcc alone in his deskroom behind his problem...never 8)

The master of pb give dream in a full day...it's amazing...

Again thanks, i have also sharing your code on the french forum.

Re: Unicode strange behaviour

Posted: Thu Aug 31, 2017 12:38 pm
by mk-soft
I think is the DLL as Unicode is this no a problem...

Not testet

Code: Select all

;-TOP
; Unicode

;- Structure vbString

Structure vbString
  len.l
  text.s
EndStructure

ProcedureDLL foo(*Text.String) ; Result as vbString
  Static vbResult.vbString
  vbResult\text = ReverseString(*Text\s)
  vbResult\len = Len(vbResult\text)
  ProcedureReturn @vbResult\text
EndProcedure