Recent

Author Topic: Trouble converting PChar to String  (Read 5052 times)

DavidTh30

  • New Member
  • *
  • Posts: 19
Trouble converting PChar to String
« on: June 26, 2020, 01:56:12 pm »
procedure TForm1.Button2Click(Sender: TObject);
var
  i:integer;
begin
  for i:=0 to 1000 do
  ShowMessage(PChar(PByte(i)));
end;


How can I filter this PChar is 'string' or 'not string'.
"Try Except Finally" does not work for this kind of SIGSEGV error.
« Last Edit: June 26, 2020, 02:08:56 pm by DavidTh30 »

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 882
Re: Trouble converting PChar to String
« Reply #1 on: June 26, 2020, 02:05:24 pm »
What do you try to do? Read memory from address 0 to 1000? Not sure about Win64, but on Win32 this memory isn't readable. First 64k of memory is reserved for handles. I.e. address < 64k is handle and address >64k is address.

P.S. If you need to convert integer to string - use IntToStr. If you need to print contents of  integer as string - use PChar(@i).
« Last Edit: June 26, 2020, 02:17:10 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

DavidTh30

  • New Member
  • *
  • Posts: 19
Re: Trouble converting PChar to String
« Reply #2 on: June 26, 2020, 02:20:46 pm »
Normally during transfer DDE "Dynamic data exchange" using option "CF_TEXT", Microsoft always use variable "PByte".
Sometimes I got wrong pointer signal, So I receive something not a string.
Same thing in this case I got SIGSEGV error "cmpb".

I already try to convert using function copy PChar, I got SIGSEGV error "mov".

I'm not sure that hDataQUOTE address < 64k or >64k.
« Last Edit: June 26, 2020, 02:31:16 pm by DavidTh30 »

jamie

  • Hero Member
  • *****
  • Posts: 7832
Re: Trouble converting PChar to String
« Reply #3 on: June 26, 2020, 03:35:41 pm »
A miss use of pointers.

Code: Pascal  [Select][+][-]
  1. Achar := pchar(astring)^[someindex];
  2.  

On those lines.
The only true wisdom is knowing you know nothing

DavidTh30

  • New Member
  • *
  • Posts: 19
Re: Trouble converting PChar to String
« Reply #4 on: June 26, 2020, 05:20:43 pm »
Maybe I miss use of pointers.

// Pointers example#1
procedure TForm1.Button1Click(Sender: TObject);
var
  Pch1:^Char;
  Pch2:PChar;
  i:integer;
  i2:double;
  Pb:PByte;
  Pr:^real;
begin
  for i:=0 to 20 do
  begin
    i2:=i*0.123;  Pr:=@i2;
    Pb:=@i;  Pch1:=PChar(Pb); Pch2:=@i;
    ShowMessage(Pch1+' '+Pch2+ ' ' + IntToStr(Pb^)+ ' ' + FloatToStr(Pr^));
  end;
end; 


 %) %) %) %) %) %) %) %) %) %)

jamie

  • Hero Member
  • *****
  • Posts: 7832
Re: Trouble converting PChar to String
« Reply #5 on: June 26, 2020, 05:40:26 pm »
Why dont u tell us exactly what u are trying for?
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5558
  • My goal: build my own game engine using Lazarus
Re: Trouble converting PChar to String
« Reply #6 on: June 26, 2020, 05:41:45 pm »
He was trying to convert PChar to String.

jamie

  • Hero Member
  • *****
  • Posts: 7832
Re: Trouble converting PChar to String
« Reply #7 on: June 26, 2020, 06:05:33 pm »
U need know if ms is using widechar strings in the dde transaction.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7832
Re: Trouble converting PChar to String
« Reply #8 on: June 26, 2020, 09:19:20 pm »
Looking at your field variable names that start with an 'h' tells me that maybe a handle to an object not a direct pointer to use.

 I would try using a globallock( the handle ) which will return the pointer.

Also dont forget to unlock it.
The only true wisdom is knowing you know nothing

DavidTh30

  • New Member
  • *
  • Posts: 19
Re: Trouble converting PChar to String
« Reply #9 on: June 27, 2020, 12:38:14 am »
SIGSEGV error Interrupt on top of "Try Except Finally".
How can I know that target address is string, integer or double?

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Trouble converting PChar to String
« Reply #10 on: June 27, 2020, 12:48:02 am »
SIGSEGV error Interrupt on top of "Try Except Finally".
How can I know that target address is string, integer or double?

Hi!

If you don't know the logical use of that address you'll never know.

You can read an array of byte and then you can check if there are errors if you convert it to a float.
Or if the converting to a string makes some sense.

So again ( and again and again) like Mr. Madguy said:

What are you trying to do??????

Winni

ASBzone

  • Hero Member
  • *****
  • Posts: 733
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Trouble converting PChar to String
« Reply #11 on: June 27, 2020, 01:21:41 am »
SIGSEGV error Interrupt on top of "Try Except Finally".
How can I know that target address is string, integer or double?

At some point, I suspect that people are going to stop trying to help you until you answer the very basic question of "what are you trying to accomplish?"

Not at the task level, but at the project level.  What are you looking to accomplish overall with this application?

Without that, it is going to be very hard to help you solve your issue.   
-ASB: https://www.BrainWaveCC.com/

Lazarus v4.3.0.0 (bcf314a670) / FreePascal v3.2.3-46-g77716a79dc (aka fixes)
(Windows 64-bit install w/Win32 and Linux on ARM and x64 cross-compilers via FpcUpDeluxe)

My Systems: Windows 10/11 Pro x64 (Current)

jamie

  • Hero Member
  • *****
  • Posts: 7832
Re: Trouble converting PChar to String
« Reply #12 on: June 27, 2020, 01:28:53 am »
looking here

https://forum.lazarus.freepascal.org/index.php?topic=39403.0


I would say you have been working on this for a while now..

I don't see you using "DdeCreateStringHandle" for the  P_.HSZPair

Field.

 A handle is needed from the Dde system so it knows how to manage the string ,.
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5558
  • My goal: build my own game engine using Lazarus
Re: Trouble converting PChar to String
« Reply #13 on: June 27, 2020, 02:07:26 am »
procedure TForm1.Button2Click(Sender: TObject);
var
  i:integer;
begin
  for i:=0 to 1000 do
  ShowMessage(PChar(PByte(i)));
end;


That is wrong. These below are correct:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   aByte:  Byte;
  4.   aPByte: PByte;
  5.   S:      string;
  6.   i:      Integer;
  7. begin
  8.   S      := '';
  9.   aPByte := @aByte;
  10.   for i := 1 to 20 do begin
  11.     aByte := i;
  12.     S     := S + (aPByte^).ToString + ' ';
  13.   end;
  14.   ShowMessage(S);
  15. end;
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   aPChar: PChar;
  4.   S:      string;
  5.   i:      Integer;
  6. begin
  7.   S := 'Hello';
  8.   for i := 0 to S.Length - 1 do begin
  9.     aPChar := @S[1];
  10.     Inc(aPChar, i);
  11.     ShowMessage(aPChar^);
  12.   end;
  13. end;
« Last Edit: June 27, 2020, 02:27:24 am by Handoko »

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Trouble converting PChar to String
« Reply #14 on: June 27, 2020, 07:12:45 am »
Shouldn't the second be:

... some code deleted ...[/]

Otherwise, it will end up pointing always to S[2].

Just saying :-[


Never mind me ... didn't really look: Inc(aPChar, i);  :-[ :-[ :-[
« Last Edit: June 27, 2020, 07:16:51 am by lucamar »
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.

 

TinyPortal © 2005-2018