最近做一個軟件,用webbrowser做軟件界面,通過js的window.external調用delphi的函數實現數據庫操作等功能,在需要返回值的時候,遇到問題了,來源如下圖:

在接口中加入一方法,傳入參數為BSTR(wideString)類型,希望返回VARIANT(OLEVariant),自動生成的函數結構如下:
在***_TLB.pas中:
Itest = interface(IDispatch)
['{D9941302-C827-4517-ADA0-003D176E9E9A}']
function Method1(const a: WideString): OleVariant; stdcall;
end;
在接口實現單元中:
type
Ttest = class(TAutoObject, Itest)
protected
function Method1(const fd: WideString): OleVariant; stdcall;
end;
implementation
uses ComServ;
function Ttest.Method1(const fd: WideString): OleVariant;
begin
end;
運行后,在網頁中調用:
var a=window.external.Method1('fdsa');
alert(a);
執行時,報錯;
糾結很久,對函數重新申明如下圖:

在***_TLB.pas中:
Itest = interface(IDispatch)
['{D9941302-C827-4517-ADA0-003D176E9E9A}']
function Method1(const a: WideString): OleVariant; safecall;
end;
在接口實現單元中:
Ttest = class(TAutoObject, Itest)
protected
function Method1(const a: WideString): OleVariant; safecall;
end;
implementation
uses ComServ;
function Ttest.Method1(const a: WideString): OleVariant;
begin
end;
運行后可正常顯示返回內容,對於我這種菜鳥來說,唯一的區別就是safecall與stdcall, 由於沒有程序基礎,用delphi全靠自學,不太明白這兩個東西的區別,暫時不管他,只要程序能工作就行,在這里做個記錄,以免再犯錯。
