Using a string pointer to send a string through Windows messages

I am trying to understand how line pointers work. I have a code (not quite original) that was written by someone, and there is no person here, so I need to understand the idea of ​​such a use.

var
  STR: string;
  pStr: ^string;
begin
  STR := 'Hello world';
  New(pStr);
  pStr^ := STR;

  PostMessage(Handle, WM_USER+1, wParam(pStr), 0);
end;

Now I know for sure that the message handler receives the message, and the pointer contains a line with which to work, but what happens “under the hood” of these operations ?

I tried to make a small project. I thought that assigning a string to what the pointer to str points to would actually increase the refcount of the original string and not make any copies of the string, but refcount remained 1, and it looks like it copied the contents.

, , ? New , ? refcount/length , PChar(@pStr^[1])[-8], (14), .

, questioin, , Windows?

+4
1

New(pStr) string . string - , , . a string , , , .

, . , - . , IPC.

, , , . - :

var
  p: ^string;
  str: string;
....
p := Pointer(wParam);
str := p^; 
Dispose(p);

, . :

{$APPTYPE CONSOLE}

var
  pStr: ^string;
  p: PInteger;

begin
  New(pStr);
  pStr^ := 'Hello world';

  p := PInteger(pStr^);
  dec(p);
  Writeln(p^); // length
  dec(p);
  Writeln(p^); // ref count

  Readln;
end.

:

11
1
+8

All Articles