Convert pointer to string

0

Hello,

I would like to know how to do the pointer to string conversion, because when I try to use the Pointer in a MessageBoxA for example, it returns me the following error:

I'm trying to display the message like this:

 MessageBoxA(0, Pointer ,nil,0);
    
asked by anonymous 18.10.2015 / 07:30

3 answers

3

The Pointer and the pointer definition in Delphi, a data type. A pointer saves the address of a variable that is in memory, and through it one can access the value of the variable that the pointer is pointing to.

Example, accessing a string variable:

procedure TForm1.Button1Click(Sender: TObject);
var
  pExemplo: Pointer;
  vStr, r: string;
begin
  vStr := 'exemplo ponteiro';

  pExemplo := @vStr;//pExemplo recebe o endereço de vStr.

  r := string(pExemplo^);//Aqui é feita a conversão da variável para string. 

  ShowMessage(r);
end;

The variable r received the value of vStr through the pExemplo pointer.

More about pointers in Delphi here . I hope I have helped you.

    
18.10.2015 / 19:27
2

After searching a bit more, I discovered that I should first turn it into Integer (Integer (Pointer)) and then convert it to string (IntToStr (Int))

Code would look like this: IntToStr(integer( Pointer ))

    
18.10.2015 / 08:04
0

Let the compiler help you. Use PChar , is the safest and easiest way to do it.

int WINAPI MessageBox(
  _In_opt_ HWND    hWnd,
  _In_opt_ LPCTSTR lpText,
  _In_opt_ LPCTSTR lpCaption,
  _In_     UINT    uType
);

MessageBox(0, PChar('Teste'), PChar('Titulo'), MB_OK);

However, in most cases I would use:

First of all:

uses Dialogs;
ShowMessage(const Msg: String);

Second Place

uses Forms;
Application.MessageBox(Text, Caption: PChar; Flags: LongInt = MB_OK);

If you migrate your application to Unicode , you will not want to call the MessageBoxA function, because if your string is unicode (it probably will be if you declare your variable as string ), your code may fail .

    
19.10.2015 / 12:49