最近做一个软件,用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;


运行后可正常显示返回内容,对于我这种菜鸟来说,唯一的区别就是safecallstdcall, 由于没有程序基础,用delphi全靠自学,不太明白这两个东西的区别,暂时不管他,只要程序能工作就行,在这里做个记录,以免再犯错。



posted on 2012-02-11 18:53  Bach  阅读(3381)  评论(3编辑  收藏  举报