从 .DLL 文件访问方法或属性时出现 Delphi 6 内存访问冲突错误
我们正在使用的第三方 .DLL 文件中有一个错误,我被指派去修复它。该库是由最后聘请来维护代码的公司制作的,我们只有一些源代码。每当用于处理的对象出现在我们的代码中时,它们都是 OleVariants 的形式。我的解决方案是创建一个有错误的子类,并重写该方法来纠正错误。问题是,每当我尝试从父类调用方法时,我都会收到来自不同 .DLL 文件的内存访问冲突错误。
我是一名完全的 Delphi 新手,这是我使用它的第三周。任何帮助将不胜感激。
谢谢。
编辑:我可能应该详细说明一下。我正在用其中包含修复程序的新类的实例替换原始对象。我试图将原始对象从 OleVariant 转换为新类并将其重新转换或将其传递给新对象的构造函数,以便我可以维护对象中的数据。我想知道是否有一种方法可以做到这一点,并且当我在将对象强制转换回原始类型后调用继承的函数或对象的函数时,不会出现错误。再次感谢您。
编辑2:为了回答一个问题,我试图从 OleVariant 转换到它的原始类或我创建的子类。
回答第二个问题:我导入了涉及的库,然后编写了以下内容:
Subclass = class(SuperclassFromDll)
其中“Subclass”和“SuperclassFromDll”是实际的类名。如果这不是正确的方法,请告诉我如何替换有问题的函数(可能通过将修复写入 DllName_TLB 单元?)。对于任何不清楚的地方,我深表歉意,我正在尝试快速提出问题,以便我可以回过头来尝试弄清楚。
编辑 3:我还应该注意到,DLL 文件是从前一家公司制作的 Delphi 项目创建的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能这样做。正如大卫·赫弗南(David Heffernan)所说,这“非同小可”,这是一种很好的说法,说明你必须是天才才能实现这一目标。因此,如果您打算尝试的话,您不必是 Delphi 新手。 (OTOH,如果您不是 delphi 新手,您甚至不会考虑它)。
一方面,如果 COM 对象在 Delphi 中实现,并且动态链接,并且使用相同版本的 Delphi 进行编译,则您只能将 COM 对象转换为 Delphi 对象。
一个更好、更简单的解决方案是隔离引起问题的情况,并避免在这些情况下调用该方法,因为据报告该问题是在您调用类上的方法时发生的。
您可以在包装类中做到这一点。因此,不要强制转换 OleVariant(我假设 COM 对象在底层)并将其强制转换为 delphi 包装类,而是创建您自己的类
CProblemObjectShim
,其中包含问题类作为成员。然后通过调用所包含的对象并添加解决问题所需的附加检查或步骤来实现所有方法。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.