0

I have created a DLL file with some functions and wish to reuse in a program multiple times in its different functions. But the Access-violation error comes after 2nd function of the program when calls the same DLL functions.

I'm currently using GetProcAddress. For example:

function xyz:boolean
var
   dllHandle : cardinal;
   EnBFStr : TEnBFStr;
   StrToHex : TStrToHex;
   Encodeddata , HexString : UnicodeString;

   begin
     dllHandle := LoadLibrary('Utilities.dll') ;
     if dllHandle <> 0 then
     begin
        Encodeddata:='Sample';
        @EnBFStr := GetProcAddress(dllHandle, 'EncodeBlowFishString') ;
        @StrToHex := GetProcAddress(dllHandle, 'UniStrToUniHexStr') ;
        if Assigned (EnBFStr) then
           Encodeddata:=EnBFStr('Key','Text') ; //Sample would be replaced

        if Assigned (StrToHex ) then
           HexString :=StrToHex(Encodeddata) ; //call the function

        FreeLibrary(dllHandle) ;
 end;

There are other functions which is loading the library and calling these DLL functions multiple times. Also, within the same procedure/function, we are calling these DLL functions multiple times in (IF Else) conditions.

In earlier part of the program, I have tried to check for the DLL file is present. Also, I tried to directly load the functions as another alternative:

function EncodeBlowFishString (Const Key:UnicodeString; Const DecodedString:UnicodeString; ): UnicodeString; stdcall;
external 'Utilities.dll' name 'EncodeBlowFishString';

function UniStrToUniHexStr(Const aString:UnicodeString): UnicodeString; stdcall;
external 'Utilities.dll';

1 Answer 1

3

You are breaking the rules of memory allocation for DLLs. The return value is allocated by the callee but deallocated by the caller. Two solutions:

  1. Use ShareMem as described in the comment at the top of a new library project.
  2. Use standard interoperability techniques to ensure that allocation and deallocation always happens in the same module.

As an aside it is greatly wasteful to load and unload a DLL each time you want to use it. Load the DLL once only.

Furthermore I would like to point out that encryption operates on binary data and in my view you are storing up a world of pain by working instead with text.

Sign up to request clarification or add additional context in comments.

5 Comments

You are correct. I'm using the Binary Data to encrypt it. It was just an example.
You are passing text and not binary.
Sorry, Actually, that EncodeBlowFishString custom function itself is doing everything for me. That is why, I passed Unicode String and then it casted everything inside it. It was inline then I decided to have a DLL for all these custom-functions
That's the problem in my view. You have lost control of text encoding. If it were me I'd explicitly specify an encoding. Anyway that is an aside. I dealt with your actual question.
@david-heffeman I have added a wrapper inside DLL so that it does most of the work instead of reusing the same functions outside the scope of any caller's procedure or function. I referred your example in stackoverflow. Since I was coming from Pascal Lazarus to Delphi, some things are trickier. Thank you for your help

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.