-3

Can anyone tell me why this code works in Delphi 7, but in version 10.4 it has a memory error?

procedure ChatPrintf(ChatPrintMSG: PChar);
var
  ChatPrint: Cardinal;

  procedure ChatPrintASM(ChatPrintMSG: PChar); assembler;
  asm
    lea edx, [ChatPrintMSG]
    push edx
    call [ChatPrint]
    pop edx
  end;
begin
  ChatPrint := $009E0C30;
  ChatPrintASM(ChatPrintMSG);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  ChatPrintf('it: works');
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Waloszek
  • 11
  • 1
  • Stack Overflow is an English-only site. – Andreas Rejbrand Aug 10 '22 at 13:46
  • 1
    Anyhow: In Delphi 2009, strings went from Ansi (1 byte per char) to Unicode (2 bytes per char). That's likely the issue. – Andreas Rejbrand Aug 10 '22 at 13:47
  • 1
    Why are you writing asm code at all? – David Heffernan Aug 10 '22 at 15:25
  • 2
    @DavidHeffernan He doesn't - again it's a copy without linking the sources: either [this](https://progamercity.net/ghack-requests/7401-hack-priston-tale-brazil.html) or [that](https://www.webcheats.com.br/topic/1771622-pegando-adress-para-chatprinft/). Most likely nothing exists at that address (and/or OP is compiling in 64 bit, should it be Delphi XE2). – AmigoJack Aug 10 '22 at 16:38
  • @AmigoJack - That's a good find. I suspect that perhaps he doesn't know pascal. – Rohit Gupta Aug 10 '22 at 21:21
  • 2
    There is no good reason to use assembly at all in this code. It can be re-written using pure Pascal, eg: `procedure ChatPrintf(ChatPrintMSG: PAnsiChar); type ChatPrintProc = procedure(Msg: PAnsiChar); cdecl; var ChatPrint: ChatPrintProc; begin ChatPrint := ChatPrintProc($009E0C30); ChatPrint(ChatPrintMSG); end;` – Remy Lebeau Aug 11 '22 at 00:46
  • btw, when you say "memory error", what is the exact message? There is a chance some the newer compiler turns on some flags that Windows support for executable's memory protection. Are you trying to load some other EXE/DLL and modify (hack) its behaviour at runtime? – George Birbilis Aug 11 '22 at 11:55

1 Answers1

0

As Andreas has mentioned, the default string type has changed. The easiest way to fix it, is to replace pchar with pansichar

The new definition would be

procedure ChatPrintASM(ChatPrintMSG: PAnsiChar);
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
  • i made the changes but i have the same error " access violation addres " – Waloszek Aug 10 '22 at 14:38
  • 1
    I just tried it and that code works for me. Has it occurred to you that perhaps the **absolute address** that you have assigned to your external procedure is incorrect? – Rohit Gupta Aug 10 '22 at 21:18
  • i use same code on delphi 7 and works but 10.4 get error – Waloszek Aug 10 '22 at 22:24
  • 6
    Is there a particular reason that you think that an absolute address will be the same in different versions of the compiler? – Rohit Gupta Aug 10 '22 at 23:48