-1

I have an app that uses winrar (unrar64.dll). In delphi 10.3 the code runs fine, but running it under Delphi 11, it throws an access violation on this line

RARSetCallback(RARArchiveInstance, HandleRarCallBack, Integer(Self));

This function is in the dll header file as

TRARSetCallback = procedure(hArcData: THandle; Callback: TRARUnRarCallback; UserData: longint); stdcall;

The second param is a function defines as such

TRARUnRarCallBack = function(msg: Cardinal; UserData, P1, P2: longint): integer; stdcall;

I assume the data types may have changed from 10.3 to 11. but cannot figure out where the issues lie. Thank you

  • 1
    And how did you declare `HandleRarCallBack` yourself? Why are you casting `self` as `Integer` when the parameter should be `longint`? – AmigoJack Oct 01 '22 at 00:31
  • This was code I inherited and trying to make sense of it, so Im unsure. However, it was working in 10.3 but no longer does in 11. `function HandleRarCallBack(msg: THandle; UserData, P1, P2: LongInt): integer; stdcall; begin try Result := TRAR(UserData).OnUnRarCallBack(msg, UserData, P1, P2); except Result := -1; end; end;` – Richard Fragiacomo Oct 01 '22 at 03:27
  • Sorry, trying to figure how to code format in comments – Richard Fragiacomo Oct 01 '22 at 03:37
  • Does it help if you change LongInt to NativeInt or pointer? I suppose UserData, P1 and P2 are pointers and LongInt is 32-bit under Win64. The cast Integer(self) shall be at least NativeInt(self) or pointer(self), depending on how you modify the callback parameter types. – dwrbudr Oct 01 '22 at 05:49
  • 1
    Exactly. These look like pointers. Check the C header file for the dll. Make sure that you knownfor sure rather than guessing. @AmigoJack Integer and Longint are the same on Windows. – David Heffernan Oct 01 '22 at 06:56
  • The recent Delphi version supports ASLR. You can try to disable that in the Linker options. Note that you may still have a type problem, but that can as well reside inside the DLL itself. – Uwe Raabe Oct 01 '22 at 08:50
  • I don't recommend suppressing ASLR far better to fix the defects. The DLL won't be using 32 data types for pointers. – David Heffernan Oct 01 '22 at 08:53
  • 1
    Please edit your question instead of providing code in comments. And use code **block** formatting instead of code **fragment** formatting. – AmigoJack Oct 01 '22 at 09:44
  • @AmigoJack: Probably you meant to say `NativeInt`. As David pointed out, `LongInt` is the same as `Integer`, and doesn't vary with the pointer size. – Andreas Rejbrand Oct 01 '22 at 12:54

1 Answers1

4

All of the longints declared in your code are wrong. They should be LPARAM which is pointer sized.

You should refer to the official header file to make sure the types are defined correctly across your code. My guess is that you have an old Pascal header translation. You can either fix it yourself or search for a new one.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • It's fascinating how many issues of this type has surfaced since ASLR was enabled. The risk of *actual symptoms* of buggy code due to truncated pointers has gone from very low to very high essentially overnight. – Andreas Rejbrand Oct 01 '22 at 12:49
  • Thank you both for the info Ill look into and update accordingly. Thanks! – Richard Fragiacomo Oct 01 '22 at 17:18
  • Changing the Cardinal data types to THandle and the integers to LPARAM worked. Also, thanks for the info on ASLR, I figured there was some drastic change to handling of data types but was unaware of ASLR thanks for the info. Im new here. is there a way to send a thanks or something for helping? – Richard Fragiacomo Oct 01 '22 at 18:02
  • @RichardFragiacomo: The best thing you can do is to click the green (or maybe it's grey initially) checkmark to the left of David's answer (as soon as that one becomes available, it may take a few hours). – Andreas Rejbrand Oct 01 '22 at 18:04
  • Cardinal -> THandle in the one arg in this question is wrong. Cardinal is fine but DWORD is how it is declared in the header. You need to read that header file. – David Heffernan Oct 01 '22 at 18:54