Recent

Author Topic: [SOLVED] Struggling to get a WideString with IntToStr  (Read 2975 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
[SOLVED] Struggling to get a WideString with IntToStr
« on: January 21, 2019, 05:42:16 pm »
Hi

I think this is a simple one for people who get widestrings and pointers better than me. I am using a Delphi API with Lazarus (using {mode DELPHI}) and one of the functions outputs text to an output message using LPWSTR (long pointer to wide string).

What I have below does compile but I am struggling getting it to output anything (it outputs empty lines currently).

Code: Pascal  [Select][+][-]
  1.  
  2. function X_ProcessItem(nItemID : LongWord; lpReserved : Pointer) : integer; stdcall; export;
  3. var
  4.   ItemSize : Int64;
  5.   ItemSizeWStr : WideChar;
  6.   Buf : array[0..1000000] of WideChar;  // because I can't do array[0..Buflen-1] of WideChar for some reason)
  7. begin
  8.   ItemSize := XAPI_GetItemSize(nItemID); // One API call. Gets the size and returns an Int64
  9.   StringToWideChar(IntToStr(ItemSize), @ItemSizeWStr, Length(ItemSizeWStr));  // IntToStr won't give me a widechar as needed for the output API function so doing this to convert.
  10.   lstrcpyw(Buf, @ItemSizeWStr);  // this is from the Windows API, and it should copy the widestring to the Buffer
  11.   XAPI_OutputMessage(@Buf[0], 0);  // The last API call that should output the content of the Buffer, ideally "12345" (or whatever the value of ItemSize is
  12. end;      
  13.  

Where am I going wrong please?
« Last Edit: January 21, 2019, 06:10:35 pm by Gizmo »

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Struggling to get a WideString with IntToStr
« Reply #1 on: January 21, 2019, 06:10:24 pm »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [SOLVED] Struggling to get a WideString with IntToStr
« Reply #2 on: January 21, 2019, 07:15:15 pm »
Wouldn't this be easier:

Code: Pascal  [Select][+][-]
  1. function X_ProcessItem(nItemID : LongWord; lpReserved : Pointer) : integer; stdcall; export;
  2. var
  3.   ItemSize : Int64;
  4.   ItemWStr : WideString;
  5. begin
  6.   ItemSize := XAPI_GetItemSize(nItemID); { Get the size as an Int64 }
  7.   ItemWStr := IntToStr(ItemSize);        { and convert to WideString}
  8.   XAPI_OutputMessage(@ItemWStr[0], 0);  // The last API call that should output the content of the Buffer, ideally "12345" (or whatever the value of ItemSize is
  9. end;

It's untested but it should work. Your problem was that you were trying to use a WideChar as if it were a WideString, so use a WideString!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: [SOLVED] Struggling to get a WideString with IntToStr
« Reply #3 on: January 22, 2019, 06:11:12 pm »
Thanks for the pointers there.

I'm having to use buffer 'arrays of widechars' to ensure the API is happy.

 

TinyPortal © 2005-2018