我無法從C頭中將類轉換爲在Delphi中使用。將C頭中的__declspec轉換爲Delphi
在C頭文件中的說明的一個片段是這樣的:
class __declspec(uuid("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
ISomeInterface
{
public:
virtual
BOOL
SomeBoolMethod(
VOID
) const = 0;
}
我正在寫導出接受一個ISomeInterface參數的方法的DLL,例如
function MyExportFunc (pSomeInterface: ISomeInterface): Cardinal; export; stdcall;
var
aBool: BOOL;
begin
aBool := pSomeInterface.SomeBoolMethod;
end;
我已經聲明ISomeInterface在德爾福這樣的:
type ISomeInterface = class
function SomeBoolMethod: BOOL; cdecl; virtual; abstract;
end;
調用訪問衝突的pSomeInterface.SomeBoolMethod結果。
我在做一些根本性的錯誤嗎?
實際的C頭是httpserv.h,我試圖在Delphi中實現IIS7本機模塊。
一些C++代碼,其工作原理是這樣的:
HRESULT
__stdcall
RegisterModule(
DWORD dwServerVersion,
IHttpModuleRegistrationInfo * pModuleInfo,
IHttpServer * pHttpServer
)
{
// etc
}
當調試我看到pModuleInfo參數包含具有下它6元__vfptr構件(名爲[0]〜[5]和具有地址作爲值),我推斷指針IHttpModuleRegistrationInfo類中的虛擬方法。
德爾福RegisterModule出口現在看起來像這樣:
function RegisterModule (dwServerVersion: DWORD; var pModuleInfo: Pointer; var pHttpServer: Pointer): HRESULT; export; stdcall;
begin
// etc
end;
pModuleInfo包含在CPP例子的等效地址到__vfptr構件,並且假定在__vfptr的順序是相同的標頭中的類聲明文件I提取方法地址:
function RegisterModule (dwServerVersion: DWORD; var pModuleInfo: Pointer; var pHttpServer: Pointer): HRESULT; export; stdcall;
var
vfptr: Pointer;
ptrGetName: Pointer;
ptrGetId: Pointer;
begin
vfptr := pModuleInfo;
ptrGetName := Pointer (Pointer (Cardinal(vfptr))^);
ptrGetId := Pointer (Pointer (Cardinal(vfptr) + 4)^);
end;
我現在有方法地址來調用,所以現在我只需要以某種方式調用它。儘管我可能會以這種錯誤的方式進行討論!
該類將很難轉換爲Delphi。首先,它不是一個真正的COM風格的界面。其次,它的方法使用Microsoft的默認調用約定來調用方法,這就是thiscall; Delphi不支持該調用約定,因此沒有Delphi代碼可以調用它。如果您對C++代碼有任何影響,請將其更改爲與其他Windows API兼容的內容。 –