• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

转Delphi和JavaScript互通

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

http://www.raysoftware.cn/?p=305

Delphi2010以后增加了新的RTTI信息,也就是通过RTTI可以在运行时获取/调用对象的公开成员或者函数. ScriptControl可以添加外部的对象,这个对象是个IDispatch接口,脚本调用的时候实际上是调用IDispatch的Invoke方法. 那么我们只要实现了IDispatch的Invoke方法,在里面通过RTTI再转而调用Delphi对象的Public方法即可.通过这个可以代理任何Delphi的对象.

仅仅调用Delphi对象似乎还不够完美,对象事件如果能关联到脚本的函数就更好了.那好,封装一个事件代理的类就可以. 例子如下:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Fscript := CreateScriptControl();
  // 把Form1当成一个对象添加到Script中
  Fscript.AddObject(Self.Name, SA(Self), true);
  
  Fscript.AddCode('function Form1_OnMouseMove(Sender, shift, x, y)' //
    + '{' // 在JS里面直接调用Form1上的任何Public的东西就都可以了,JS里面几乎没有类型的概念.事件的参数随便.计算也随便
    + 'Form1.Button1.Caption = "x:"+x+";"+"y:"+y +";" + "shift:" + shift;' //
    + '}' //
    + 'function Button1_Click(Sender)' //
    + '{' //调用Delphi对象的方法
    + 'Form1.SetBounds(0,0,800,480);' //
    + '}' //
    );
  
  //关联Delphi的事件到JS的函数
  Self.OnMouseMove := TEventDispatch.Create<TMouseMoveEvent>(Self, Fscript,
    'Form1_OnMouseMove');
  Button1.OnClick := TEventDispatch.Create<TNotifyEvent>(Button1, Fscript,
    'Button1_Click');
end;

看上去很爽吧. 不过这个仅供我自己玩的,代码实现的比较毛糙,也没有经过严格的测试,甚至自己也没从头到尾再检查一次.如果有需要实用的朋友最好谨慎,肯定有细节问题要解决. 另外这个ScriptControl仅仅有32位的,在64位Windows上的system32里面并没有这个DLL,仅仅在SysWow64中才有.也就是说如果你要开发64位Windows程序就不能用了.当然如果是在64位Windows中运行的32位程序则没问题.

下面是代码,写的比较丑.

{
  让Delphi使用windows自带的scriptcontrol,在javascript中可以调用delphi的对象,
  并且可以使用事件.
  wr960204武稀松 2013
}
unit ScriptObjectUtilsWithRTTI;
  
interface
  
{
  是否使用外部的MSScriptControl_TLB单元.我把这个单元的接口声明都放在后面了,
  可以避免引入ActiveX等单元
  如果觉得我的声明太旧或者有问题,可以打开这个开关,使用外部自己Import生成的单元
}
{ .$DEFINE Use_External_TLB }
{ 这个开关是使用LoadLibrary方式加载COM DLL,也就及时COM组件没有注册也可以创建COM对象 }
{$DEFINE COMOBJ_FROMDLL}
  
uses
{$IFDEF Use_External_TLB}
  MSScriptControl_TLB,
{$ENDIF}
  System.ObjAuto,
  System.Classes, System.RTTI, System.Variants,
  Winapi.Windows, Winapi.ActiveX, System.TypInfo;
  
type
{$REGION 'MSScriptControl_TLB'}
{$IFDEF Use_External_TLB}
  IScriptControl = MSScriptControl_TLB.IScriptControl;
{$ELSE}
  ScriptControlStates = TOleEnum;
  IScriptModuleCollection = IDispatch;
  IScriptError = IDispatch;
  IScriptProcedureCollection = IDispatch;
  
  IScriptControl = interface(IDispatch)
    ['{0E59F1D3-1FBE-11D0-8FF2-00A0D10038BC}']
    function Get_Language: WideString; safecall;
    procedure Set_Language(const pbstrLanguage: WideString); safecall;
    function Get_State: ScriptControlStates; safecall;
    procedure Set_State(pssState: ScriptControlStates); safecall;
    procedure Set_SitehWnd(phwnd: Integer); safecall;
    function Get_SitehWnd: Integer; safecall;
    function Get_Timeout: Integer; safecall;
    procedure Set_Timeout(plMilleseconds: Integer); safecall;
    function Get_AllowUI: WordBool; safecall;
    procedure Set_AllowUI(pfAllowUI: WordBool); safecall;
    function Get_UseSafeSubset: WordBool; safecall;
    procedure Set_UseSafeSubset(pfUseSafeSubset: WordBool); safecall;
    function Get_Modules: IScriptModuleCollection; safecall;
    function Get_Error: IScriptError; safecall;
    function Get_CodeObject: IDispatch; safecall;
    function Get_Procedures: IScriptProcedureCollection; safecall;
    procedure _AboutBox; safecall;
    procedure AddObject(const Name: WideString; const Object_: IDispatch;
      AddMembers: WordBool); safecall;
    procedure Reset; safecall;
    procedure AddCode(const Code: WideString); safecall;
    function Eval(const Expression: WideString): OleVariant; safecall;
    procedure ExecuteStatement(const Statement: WideString); safecall;
    function Run(const ProcedureName: WideString; var Parameters: PSafeArray)
      : OleVariant; safecall;
    property Language: WideString read Get_Language write Set_Language;
    property State: ScriptControlStates read Get_State write Set_State;
    property SitehWnd: Integer read Get_SitehWnd write Set_SitehWnd;
    property Timeout: Integer read Get_Timeout write Set_Timeout;
    property AllowUI: WordBool read Get_AllowUI write Set_AllowUI;
    property UseSafeSubset: WordBool read Get_UseSafeSubset
      write Set_UseSafeSubset;
    property Modules: IScriptModuleCollection read Get_Modules;
    property Error: IScriptError read Get_Error;
    property CodeObject: IDispatch read Get_CodeObject;
    property Procedures: IScriptProcedureCollection read Get_Procedures;
  end;
{$ENDIF}
{$ENDREGION 'MSScriptControl_TLB'}
  
  { 事件代理的泛型类,可以把Delphi的事件映射到Javascript的函数上.
    注意,这是一个TComponent的派生类.如果不指定Ownder的话要手工释放的.
  }
  TEventDispatch = class(TComponent)
  private
    FScriptControl: IScriptControl;
    FScriptFuncName: string;
    FInternalDispatcher: TMethod;
    FRttiContext: TRttiContext;
    FRttiType: TRttiMethodType;
    procedure InternalInvoke(Params: PParameters; StackSize: Integer);
    function ValueToVariant(Value: TValue): Variant;
    constructor Create(AOwner: TComponent; ATTypeInfo: PTypeInfo);
      reintroduce; overload;
  public
    class function Create<T>(AOwner: TComponent; ScriptControl: IScriptControl;
      ScriptFuncName: String): T; reintroduce; overload;
  
    destructor Destroy; override;
  
  end;
  
  { 很普通,创建一个MSWindows自带的ScriptControl实例,默认脚本是Javascript }
function CreateScriptControl(ScriptName: String = 'javascript'): IScriptControl;
{ 创建对象的IDispatch的代理, Owned表示这个IDispatch拥有代理对象的生杀大权,当代理的IDispatch
  释放的时候这个Obj也会被释放掉 }
function SA(Obj: TObject; Owned: Boolean): IDispatch; overload;
{ 创建对象的IDispatch的代理 }
function SA(Obj: TObject): IDispatch; overload;
  
implementation
  
uses
{$IFNDEF COMOBJ_FROMDLL}
  System.Win.ComObj,
{$ENDIF}
  System.SysUtils;
  
function CreateScriptControl(ScriptName: String): IScriptControl;
const
  CLASS_ScriptControl: TGUID = '{0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC}';
{$IFDEF COMOBJ_FROMDLL}
  MSSCRIPTMODULE = 'msscript.ocx';
var
  DllGetClassObject: function(const clsid, IID: TGUID; var Obj)
    : HRESULT; stdcall;
  ClassFactory: IClassFactory;
  hLibInst: HMODULE;
  hr: HRESULT;
begin
  Result := nil;
  hLibInst := GetModuleHandle(MSSCRIPTMODULE);
  if hLibInst = 0 then
    hLibInst := LoadLibrary(MSSCRIPTMODULE);
  if hLibInst = 0 then
    Exit;
  DllGetClassObject := GetProcAddress(hLibInst, 'DllGetClassObject');
  if Assigned(DllGetClassObject) then
  begin
    hr := DllGetClassObject(CLASS_ScriptControl, IClassFactory, ClassFactory);
    if hr = S_OK then
    begin
      hr := ClassFactory.CreateInstance(nil, IScriptControl, Result);
      if (hr = S_OK) and (Result <> nil) then
        Result.Language := ScriptName;
    end;
  end;
end;
{$ELSE}
  
begin
  Result := CreateComObject(CLASS_ScriptControl) as IScriptControl;
  if Result <> nil then
    Result.Language := ScriptName;
end;
{$ENDIF}
  
type
  TDispatchKind = (dkMethod, dkProperty, dkSubComponent);
  
  TDispatchInfo = record
    Instance: TObject;
    case Kind: TDispatchKind of
      dkMethod:
        (MethodInfo: TRttiMethod);
      dkProperty:
        (PropInfo: TRttiProperty);
      dkSubComponent:
        (ComponentInfo: NativeInt);
  end;
  
  TDispatchInfos = array of TDispatchInfo;
  
  {
    IDispatch代理类.通过RTTI可以把Delphi对象的成员/属性/函数映射给IDispatch.
    而且忽略调用协议.
  }
  TScriptObjectAdapter = class(TInterfacedObject, IDispatch)
  private
    //
    FRttiContext: TRttiContext;
    FRttiType: TRttiType;
    FDispatchInfoCount: Integer;
    FDispatchInfos: TDispatchInfos;
    FComponentNames: TStrings;
    FInstance: TObject;
    FOwned: Boolean;
    function AllocDispID(AKind: TDispatchKind; Value: Pointer;
      AInstance: TObject): TDispID;
  protected
    property Instance: TObject read FInstance;
  public
    { IDispatch }
    function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount: Integer;
      LocaleID: Integer; DispIDs: Pointer): HRESULT; virtual; stdcall;
    function GetTypeInfo(Index: Integer; LocaleID: Integer; out TypeInfo)
      : HRESULT; stdcall;
    function GetTypeInfoCount(out Count: Integer): HRESULT; stdcall;
    function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
      Flags: Word; var Params; VarResult: Pointer; ExcepInfo: Pointer;
      ArgErr: Pointer): HRESULT; virtual; stdcall;
  public
    constructor Create(Instance: TObject; Owned: Boolean = False);
    destructor Destroy; override;
  end;
  
function SA(Obj: TObject; Owned: Boolean): IDispatch;
begin
  Result := TScriptObjectAdapter.Create(Obj, Owned);
end;
  
function SA(Obj: TObject): IDispatch;
begin
  Result := TScriptObjectAdapter.Create(Obj, False);
end;
  
const
  ofDispIDOffset = 100;
  
  { TScriptObjectAdapter }
  
function TScriptObjectAdapter.AllocDispID(AKind: TDispatchKind; Value: Pointer;
  AInstance: TObject): TDispID;
var
  I: Integer;
  dispatchInfo: TDispatchInfo;
begin
  for I := FDispatchInfoCount - 1 downto 0 do
    with FDispatchInfos[I] do
      if (Kind = AKind) and (MethodInfo = Value) then
      begin
        // Already have a dispid for this methodinfo
        Result := ofDispIDOffset + I;
        Exit;
      end;
  if FDispatchInfoCount = Length(FDispatchInfos) then
    SetLength(FDispatchInfos, Length(FDispatchInfos) + 10);
  Result := ofDispIDOffset + FDispatchInfoCount;
  with dispatchInfo do
  begin
    Instance := AInstance;
    Kind := AKind;
    MethodInfo := Value;
  end;
  FDispatchInfos[FDispatchInfoCount] := dispatchInfo;
  Inc(FDispatchInfoCount);
end;
  
constructor TScriptObjectAdapter.Create(Instance: TObject; Owned: Boolean);
begin
  inherited Create;
  FComponentNames := TStringList.Create;
  FInstance := Instance;
  FOwned := Owned;
  FRttiContext := TRttiContext.Create;
  FRttiType := FRttiContext.GetType(FInstance.ClassType);
end;
  
destructor TScriptObjectAdapter.Destroy;
begin
  if FOwned then
    FInstance.Free;
  FRttiContext.Free;
  FComponentNames.Free;
  inherited Destroy;
end;
  
function TScriptObjectAdapter.GetIDsOfNames(const IID: TGUID; Names: Pointer;
  NameCount, LocaleID: Integer; DispIDs: Pointer): HRESULT;
type
  PNames = ^TNames;
  TNames = array [0 .. 100] of POleStr;
  PDispIDs = ^TDispIDs;
  TDispIDs = array [0 .. 100] of Cardinal;
var
  Name: String;
  MethodInfo: TRttiMethod;
  PropertInfo: TRttiProperty;
  ComponentInfo: TComponent;
  lDispId: TDispID;
begin
  Result := S_OK;
  lDispId := -1;
  Name := WideCharToString(PNames(Names)^[0]);
  
  MethodInfo := FRttiType.GetMethod(Name);
  // MethodInfo.Invoke(FInstance, ['']);
  if MethodInfo <> nil then
  begin
    lDispId := AllocDispID(dkMethod, MethodInfo, FInstance);
  end
  else
  begin
    PropertInfo := FRttiType.GetProperty(Name);
    if PropertInfo <> nil then
    begin
      lDispId := AllocDispID(dkProperty, PropertInfo, FInstance);
    end
    else if FInstance is TComponent then
    begin
      ComponentInfo := TComponent(FInstance).FindComponent(Name);
      if ComponentInfo <> nil then
      begin
  
        lDispId := AllocDispID(dkSubComponent, Pointer(FComponentNames.Add(Name)
          ), FInstance);
      end;
    end;
  end;
  if lDispId >= ofDispIDOffset then
  begin
    Result := S_OK;
    PDispIDs(DispIDs)^[0] := lDispId;
  end;
end

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi中动态创建的Panel无法改变颜色的解决办法发布时间:2022-07-18
下一篇:
Delphi中ChromeChromium、Cef3学习笔记(三)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap