Delphi 6 Memory Access Violation Error when Accessing a Method or Property from a .DLL file

StackOverflow https://stackoverflow.com/questions/5311877

  •  24-10-2019
  •  | 
  •  

Question

There is a bug in a third party .DLL file that we're using and I've been assigned to fix it. The library was made by the company last hired to maintain the code and we only have some of the source code. Whenever the objects used for processing are in the code we have, they're in the form of OleVariants. My solution was to create a subclass of the one with the bug and override the method to correct the bug. The problem is that whenever I try calling a method from the parent class I get a Memory Access Violation error from a different .DLL file.

I'm a complete Delphi newbie with this being my third week working with it. Any help would be appreciated.

Thank You.

EDIT: I should probably elaborate a bit more. I'm replacing the original object with an instance of the new class which has the fix in it. I'm trying to cast the original object from OleVariant up to either the new class and recast it down or pass it to the constructor for the new object so I can maintain the data in the object. I'm wondering if there's a way to do this and not have an error when I either call the inherited function or the function from the object after it has been cast back up to its original type. Thank you again.

EDIT 2: To answer one question, I'm trying to cast from the OleVariant to it's original class or to the subclass I created.

To answer the second question: I imported the library involved then wrote the following:

Subclass = class(SuperclassFromDll)

Where "Subclass" and "SuperclassFromDll" are the actual class names. If this isn't the right way to do it, please tell me how to replace the function in question (possibly by writing the fix into the DllName_TLB unit?). Sorry for any lack of clarity, I'm trying to ask the question quickly so I can get back to trying to figure it out.

EDIT 3: I should also note that the DLL file was created from a Delphi project made by the previous company.

Was it helpful?

Solution

You can't do this. As David Heffernan says it is "non-trivial", which is a nice way of saying you would have to be a genius to pull it off. So you need to not be a Delphi newbie if you are going to attempt it. (OTOH, If you were not a delphi newbie you wouldn't even consider it).

For one thing you will only be able to cast a COM object to a Delphi object if the COM object is implemented in Delphi, AND dynamically linked, AND compiled with the same version of Delphi.

A better, simpler solution, since the problem reportedly occurs when you call the method on the class, is to isolate the circumstances which give rise to the problem, and avoid calling the method in those circumstances.

You can do that in a wrapper class. So rather than casting the OleVariant (I assume COM object under the hood) and casting that to a delphi wrapper class, create your own class CProblemObjectShim which has the problem class as a member. Then implement ALL the methods by calling the contained object, and adding the additional checks or steps neccessary to work around the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top