0

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.

mnuzzo
  • 3,529
  • 4
  • 26
  • 29
  • 1
    Please tell us how you are converting the class of an existing instance. That's non-trivial and would involve modifying the VMT or hooking. I already asked you this in your previous question. – David Heffernan Mar 15 '11 at 12:49
  • 1
    `My solution was to create a subclass of the one with the bug...` How did you do that? Because you **can't** do that, and it's probably the key to the problem. You probably think you're subclassing, but you can't subclass a class from the DLL, so you're not actually subclassing. – Cosmin Prund Mar 15 '11 at 12:53
  • @David, how can you subclass a class in a DLL? Making the new class assignment-compatible with the old class? – Cosmin Prund Mar 15 '11 at 13:01
  • @David, wouldn't that subclass the `TBase` class *my compiler* knows about? Not the one in the DLL? – Cosmin Prund Mar 15 '11 at 13:10
  • All, please see [this post](http://stackoverflow.com/questions/5302768/convert-olevariant-to-object-in-delphi), and you'll understand what @mnuzzo is actually trying to do. He's working with an OleVariant returned by a C DLL that contains an IDispatch interface, and doesn't understand that it's not a Delphi class. – Ken White Mar 15 '11 at 14:37
  • @mnuzzo I don't see how this is different from your [last question](http://stackoverflow.com/questions/5302768/convert-olevariant-to-object-in-delphi), it really looks like the same issue, you shall delete one question and edit/clarify the one that stands. – jachguate Mar 15 '11 at 15:27

1 Answers1

2

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.

Ben
  • 34,935
  • 6
  • 74
  • 113
  • Actually, the OleVariant is a class made in Delphi whose interface is a subinterface of a subinterface of IDispatch. They seem to be using OleVariant as a pointer. The DLL file was created from a Delphi project. Also, I've considered just making a wrapper class, but that option is less desirable. – mnuzzo Mar 15 '11 at 13:09
  • @mnuzzo, here's your problem. An OleVariant is *not* a Delphi class. It's a data type that can store many different kinds of data and convert that data to other types when needed. As was said in the other post on this topic, you're working with an IDispatch *interface*, not a Delphi class. – Ken White Mar 15 '11 at 14:34