Recent

Author Topic: String To unicode String  (Read 2265 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
String To unicode String
« on: March 12, 2021, 09:53:44 am »
Hello Guys :)

Rewriting some vb6 Code in lazarus.

So here is my issue:

I Have a Function in a DLL wich recieves a Unicode String and returns a Unicode String.

Simple i thought...

Code: Pascal  [Select][+][-]
  1.  
  2.       //JZa := StrConv(JZa, vbUnicode);  --> vb6
  3.       //JZa2 := StrConv(JZa2, vbUnicode);--> vb6
  4.       //JZaRueckgabe := StrConv(ZAJsonRequestBSTR(JZa), vbFromUnicode);--> vb6
  5.      
  6.        ShowMessage(JZa);
  7.        Temp := UTF8Decode(JZa);
  8.        ShowMessage(Temp);
  9.  
  10.  
  11.       //JZaRueckgabe := UTF8Encode(DLLAufrufe.Call_ZAJsonRequestBSTR(UTF8Decode(JZa)));
  12.       Temp := DLLAufrufe.Call_ZAJsonRequestBSTR(UTF8Decode(JZa));
  13.       ShowMessage('Unicode : ' + Temp + Char(13) + ' -------------- ' + Char(13) + 'Ansi String : ' + UTF8Encode(Temp));
  14.  

This Code Doesn't work so i wanted to ask u Guys if u have a better Solution.

Thanks :)

balazsszekely

  • Guest
Re: String To unicode String
« Reply #1 on: March 12, 2021, 10:11:23 am »
@Weitentaaal
Quote
This Code Doesn't work so i wanted to ask u Guys if u have a better Solution.

Before FPC 3.0.0
Code: Pascal  [Select][+][-]
  1. uses LazUTF8;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   U_JZA, U_JSON: UnicodeString;
  6.   JZA, JSON:  String;
  7. begin
  8.   U_JZA := UTF8ToUTF16(JZa);
  9.   U_JSON := DLLAufrufe.Call_ZAJsonRequestBSTR(UJZa);
  10.   JSON := UTF16ToUTF8(U_JSON);
  11. end;
  12.  

After FPC 3.0.0:
Code: Pascal  [Select][+][-]
  1. var
  2.   U_JZA, U_JSON: UnicodeString;
  3.   JZA, JSON:  String;
  4. begin
  5.   U_JZA := JZa;
  6.   U_JSON := DLLAufrufe.Call_ZAJsonRequestBSTR(UJZa);
  7.   JSON := U_JSON;
  8. end;
  9.  
It should work without typecast, but to be absolutely sure you can write: U_JZA := UnicodeString(JZa) /  JSON := String(U_JSON).

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: String To unicode String
« Reply #2 on: March 12, 2021, 10:24:43 am »
Used UTF8Decode --> didn't worked
Used UTF8ToUTF16 --> didn'T work
im pretty Sure Unicode(String) wont work either

balazsszekely

  • Guest
Re: String To unicode String
« Reply #3 on: March 12, 2021, 10:37:41 am »
@Weitentaaal
Quote
Used UTF8Decode --> didn't worked
Used UTF8ToUTF16 --> didn'T work
im pretty Sure Unicode(String) wont work either
I used UnicodeString/WideString for api calls many many times and I'm pretty sure it works.  Looking at your api name "ZAJsonRequestBSTR", more exactly at "BSTR" part, it reminds me that the proper string type to use is WideString, which is a COM compatible not reference counted string.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: String To unicode String
« Reply #4 on: March 12, 2021, 11:00:06 am »
I Guess it should work then. tryed that ang got Access Violation.

Not the result i wanted but i think the Conversion works.

New Problem now is that i don't know where this Access violation gets raised  xD

But Thnaks GetMem :)

PascalDragon

  • Hero Member
  • *****
  • Posts: 6381
  • Compiler Developer
Re: String To unicode String
« Reply #5 on: March 12, 2021, 03:36:01 pm »
How is your Call_ZAJsonRequestBSTR declared? If it's UnicodeString then you need to change it to WideString cause the BSTR compatible type is WideString, not UnicodeString.

 

TinyPortal © 2005-2018