Issue printing RawByteString in Python (DLL, ctypes) lazarus / 62-General ---------------------------------------------------------------------- codehorror · 2024-04-02 18:25 I'm writing a DLL that is consumed by a Python application. I have a function that makes a GET request using TFPHttpClient. The function returns a pointer to a RawByteString: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PResponseString = ^RawByteString;function Home(Client: PFPHttpClient): PResponseString; stdcall;  begin    New(Result);    try      Result^ := Client^.Get('https://www.example.com');    except      on E: Exception do      begin        OutputDebugString(PChar('Exception: ' + E.Message));        Dispose(Result);        Result := nil;      end;  end;  end;       I can call the function no problem. But the response is confusing me. Here's my Python code: Code: Python  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i from ctypes import c_bool, CDLL, c_void_p, c_char_pimport osimport sys DLL_NAME = "dllproj"dll_name = os.path.join(    os.path.abspath(os.path.dirname(__file__)), DLL_NAME + ".dll") dll = CDLL(dll_name) CreateClient = dll.CreateFBClientCreateClient.argtypes = [c_char_p, c_char_p]CreateClient.restype = c_void_p Home = dll.HomeHome.argtypes = [c_void_p]Home.restype = c_char_p IsConnected.argtypes = [c_void_p]IsConnected.restype = c_bool DestroyClient = dll.DestroyFBClientDestroyClient.argtypes = [c_void_p] # This is odd having to do that, need to find better way to do thiscuser = "7testing"x = "4test" client = CreateClient(    cuser.encode("ascii"),    x.encode("ascii")) response = Home(client)print(response)DestroyClient(client)  Note: I have to insert the length of the string for cuser and x because strings in pascal uses 0 byte to store length. When I print the response I get the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i b'\x88\x17\xd8\x01#\x02'  Which is confusing, because when I debug the DLL in Lazarus I can see the result contains what I expect - the HTML source code (see attatchment). So where is the rest of the bytes? I've tried using decode in Python using: ascii, UTF-8, and UTF-16. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i print(response.decode("utf-8"))  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 3: invalid start byte  Any advice, or ideas? ---------------------------------------------------------------------- Darie · 2024-04-02 18:39 Seems related, maybe it would help https://stackoverflow.com/questions/26802581/can-anyone-identify-this- encoding ---------------------------------------------------------------------- tetrastes · 2024-04-02 19:56 I doubt that python understands AnsiStrings. Try smth like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Home(Client: PFPHttpClient): PChar; stdcall;  begin    try      Result := PChar(Client^.Get('https://www.example.com'));    except      on E: Exception do      begin        OutputDebugString(PChar('Exception: ' + E.Message));        Result := nil;      end;    end;  end; Quote from: codehorror on April 02, 2024, 08:25:01 pmCode: Python  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # This is odd having to do that, need to find better way to do thiscuser = "7testing"x = "4test"  Note: I have to insert the length of the string for cuser and x because strings in pascal uses 0 byte to store length. Any advice, or ideas? Advice is to read about pascal strings. ---------------------------------------------------------------------- codehorror · 2024-04-02 20:50 Quote from: tetrastes on April 02, 2024, 09:56:20 pmI doubt that python understands AnsiStrings. Try smth like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Home(Client: PFPHttpClient): PChar; stdcall;  begin    try      Result := PChar(Client^.Get('https://www.example.com'));    except      on E: Exception do      begin        OutputDebugString(PChar('Exception: ' + E.Message));        Result := nil;      end;    end;  end; Quote from: codehorror on April 02, 2024, 08:25:01 pmCode: Python  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i # This is odd having to do that, need to find better way to do thiscuser = "7testing"x = "4test"  Note: I have to insert the length of the string for cuser and x because strings in pascal uses 0 byte to store length. Any advice, or ideas? Advice is to read about pascal strings. Thanks I'll give this a try now Regarding Pascal strings. I gave this a read (https://wiki.freepascal.org/Character_and_string_types), and noticed the first element of a string is the size of the string (if no length set, first two bytes, the other for ref count). But Python doesn't understand this. That's why I placed the length of each string as the first character in cuser and x. ---------------------------------------------------------------------- codehorror · 2024-04-02 20:54 That worked! I struggled for a good 4 hours with that. I think I should be able to use PChar to avoid the string length problem I have. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CreateFBClient(CUser: PChar; Xs: PChar): PFPHttpClient; stdcall;  Edit: Yup that works too. ---------------------------------------------------------------------- tetrastes · 2024-04-02 21:48 Quote from: codehorror on April 02, 2024, 10:50:40 pmRegarding Pascal strings. I gave this a read (https://wiki.freepascal.org/Character_and_string_types), and noticed the first element of a string is the size of the string ... That's why I placed the length of each string as the first character in cuser and x. This is only for ShortStrings. And you didn't place the length. You placed characters '7' and '4' (i.e. bytes $37 and $34, instead of $07 and $04). ---------------------------------------------------------------------- codehorror · 2024-04-02 22:10 Quote from: tetrastes on April 02, 2024, 11:48:30 pmQuote from: codehorror on April 02, 2024, 10:50:40 pmRegarding Pascal strings. I gave this a read (https://wiki.freepascal.org/Character_and_string_types), and noticed the first element of a string is the size of the string ... That's why I placed the length of each string as the first character in cuser and x. This is only for ShortStrings. And you didn't place the length. You placed characters '7' and '4' (i.e. bytes $37 and $34, instead of $07 and $04). Oh, yes I see where I went wrong there. Thank you for your help and advice it's much appreciated. ---------------------------------------------------------------------- Thaddy · 2024-04-03 09:50 Since rawbytestring is essentially a Tbytes in the context of this  thread, consider this simplification: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$modeswitch typehelpers}{$H+}uses   sysutils;  type    TByteHelper = type helper for Tbytes     function ToString:String;   end;      function TbyteHelper.ToString:string;   begin     setstring(Result, PChar(Self), length(self));   end;        operator := (const a:Tbytes):string;   begin     setstring(Result, PChar(a), length(a));   end; var   t:Tbytes = (65,66,67,68);  s:string;begin  writeln(t.ToString);  s := t;  writeln(s);end.It might help in your case. There is one caveat: maybe manually add a #0 or #0#0 for the Python interface. I can't test this right now.