简体   繁体   English

使用thiscall约定调用C ++成员函数

[英]Calling C++ member functions with the thiscall convention

I have an application written in C++ that loads my DLL that is written in Delphi. 我有一个用C ++编写的应用程序,可以加载用Delphi编写的DLL。 The application calls an exported function of the DLL and passes it a pointer to an object of a class that has several member functions that I want to call from my Delphi DLL. 该应用程序调用DLL的导出函数,并将其传递给指向具有多个成员函数的类的对象的指针,这些成员函数要从Delphi DLL中调用。 I have written a Delphi class equivalent of the C++ class: 我写了一个等效于C ++类的Delphi类:

CClass = class
    procedure A(param : Integer); virtual; abstract;
    procedure B; virtual; abstract;
end

and the C++ class looks the same 和C ++类看起来一样

class CClass {
public:
    virtual void A(int) = 0;
    virtual void B() = 0;
};

The member functions use the thiscall calling convention which does not have a Delphi equivalent. 成员函数使用thiscall调用约定,该约定没有Delphi等效项。 This probably means I need to use assembly. 这可能意味着我需要使用汇编。 I have tried: 我努力了:

function exported_procedure(ptr : CClass) : Integer; cdecl;
begin
    asm
        push ebx;
        push esi;
        mov ecx, ptr;
        push 4;
        call CClass.A;
        pop esi;
        pop ebx;
    end;

    Result := 0;
end

But I get a segmentation fault when the application calls this function. 但是,当应用程序调用此函数时,出现分段错误。 How do I properly call the member function? 如何正确调用成员函数?

The asm looks fine, that's how we call a thiscall interface. 汇编程序看起来不错,这就是我们调用thiscall接口的方式。 But this is a class. 但这是一类。 I don't think there's any reason to assume CClass.A actually calls void A(int). 我认为没有任何理由假设CClass.A实际上称为void A(int)。 You'd have to figure out the correct offset. 您必须找出正确的偏移量。 Having a Delphi class won't help I think: you should find out the offsets in C++, add that offset to ptr and call that address. 我想拥有一个Delphi类将无济于事:您应该找到C ++中的偏移量,将该偏移量添加到ptr并调用该地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM