0

I'm using Delphi 11. And I have an application that I compile both 32 or 64 bits target.

For localization of my app, I'm using this code to load my language file on startup.

    LoadResStringFunc := nil; // Disable strings caching

    NewInstance := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE); 
    ....
    LibModuleList.ResInstance := NewInstance;

FileName is a DLL file that has the translated resources (RCDATA and "String Table"). It seems this file to be a 32 bits DLL whatever the 32/64 bits output of the exe file selected.

It works fine with 32 bit image exe file, but does not work anymore with the application compiled to 64 bits application. The error code is a 0XC0000005 Access violation once the

    LibModuleList.ResInstance := NewInstance;

is set, and any call to LoadResString() in system.pas . It seems memory is totally corrupted. It came suddenly (or I cannot remember what could cause this). It used to work with 64 bits exe file generated. I totally reseted the translation project from scratch (keeping the previous current one aside), and I have still the same issue. I spend a lot of time trying to figure this out with no success. What could cause this ?

1
  • You are ignoring the possibility LoadLibraryEx fails and returns null.
    – Brian
    Jun 3 at 8:57

1 Answer 1

2

So I found the culprit, in the line

 Var NewInstance:Longint

 NewInstance := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE); 

Changed into

 Var NewInstance:NativeInt; // Better for 64 bits ...

 NewInstance := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);

It was working with NewInstance as Longint for years, when windows decided to provide me an handle that is larger than 32 bits. This happens recently because of a windows update ! How I found this ? By putting {$R+} that should be enabled all the time indeed .... despite this is slower. Another annoying thing is that with localization, when inline variable declaration are used; the "extract resource string" does not work anymore, that is really really bad. Remi, loading DLL compiled with 32 bits compiler regarding RCdata and String works for sure. This is not one would expect, but this is an exception looks like.

4
  • 1
    Delphi 11.2 enabled Windows high entropy ASLR which means the top 32bits of 64bit addresses are much less likely to be all 0s. It breaks buggy code that puts 64bit pointers into 32 bit integers.
    – Brian
    Jun 3 at 20:26
  • 1
    LoadLibrary/Ex() returns a handle, not an integer, so you should be using THandle instead Jun 3 at 21:45
  • Support 64-Bit ASLR : I've learned something today. Remi : yes Thandle this is what I use usually. This code was a stupid copy and paste from another source. Jun 3 at 22:38
  • 64 bits ASLR : I did not know and I have found the explanations and this is a good comment ! I've learned something. Jun 8 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.