unit wxoExec;

interface

uses
     //自编
     wxoUnit,

     //系统
     ExtCtrls,Forms,
     SysUtils,Dialogs,Messages,Windows;
type
     TWXOCustom = class
     end;

     TWXOMonitor = class(TWXOCustom)
          Timer : TTimer;
     public
          constructor Create;
          procedure OnTimerTick(Sender: TObject);
     end;



var
     _WXO_MainFormHandle      : THandle;     //用于保存主窗体句柄
     _WXO_ApplicationHandle   : THandle;     //用于保存应用句柄

     //
     oMonitor  : TWXOMonitor;                //WXO监控对象（心跳包）

     
procedure wxoPrepare(frmHd,appHd:THandle);
procedure wxoSendMessage(DestHandle,LocalHandle:THandle;Head:string;Value:LongInt;Text:string);
procedure wxoClose;

implementation

//uses Unit1;

procedure wxoClose;
begin
     wxoSendMessage(giApp,_WXO_MainFormHandle,'RNJS',_WXO_MainFormHandle,'window.open('''',''_self'','''');'#13#10'window.close();');
end;

function GetDllName:string;
var
     Buf: array[0..MAX_PATH] of char;
begin
     GetSystemDirectory(Buf, MAX_PATH);
     Result := Trim(Buf);
     //
     Result    := Result[1] +':\WebXone\wxoExec.dll';
end;

procedure wxoSendMessage(DestHandle,LocalHandle:THandle;Head:string;Value:LongInt;Text:string);
type
     TwxoSM = procedure(DestHandle,LocalHandle:THandle;Head:PAnsiChar;Value:LongInt;Text:PAnsiChar);cdecl;
var
     Hinst     : Thandle;
     wxoSM     : TwxoSM;
begin
     Hinst     := LoadLibrary(PChar(GetDllName));//Load一个Dll,按文件名找。
     if Hinst>0 then begin
          wxoSM     := GetProcAddress(Hinst,'wxoSendMessage');//按函数名找，大小写敏感。如果你知道自动化对象的本质就清楚了。
          //此处感谢网友sgnah！
          wxoSM(DestHandle,LocalHandle,PAnsiChar(AnsiString(Head)),Value,PAnsiChar(AnsiString(Text)));//找到函数入口指针就调用。
     end;
     Freelibrary(Hinst);
end;




procedure wxoPrepare(frmHd,appHd:THandle);
type
     TPrepare = procedure (Handle,AppHandle,ParHandle:THandle);cdecl;
var
     iHandle   : THandle;
     Prepare   : TPrepare;
begin
     try
          //<增加一个监控时钟
          oMonitor  := TWXOMonitor.Create;
          //>


          //设置giApp初始值
          giApp     := 0;

          if ParamCount>2 then begin
               //从执行参数中得到数值
               giApp     := StrToIntDef(ParamStr(1),0);
               giCtrl    := StrToIntDef(ParamStr(2),0);
               gsUrl     := ParamStr(3);
               //ShowMessage(IntToStr(ParamCount)+' '+gsUrl);

               //保存值以备用
               _WXO_MAINFORMHANDLE      := frmHd;
               _WXO_ApplicationHandle   := appHd;
               //ShowMessage(IntToStr(_WXO_MAINFORMHANDLE));

               //根据值进行控制
               if (giApp>0)and(giCtrl>0) then begin
                    //
                    iHandle     := LoadLibrary(PChar(GetDllName));//Load一个Dll,按文件名找。
                    if iHandle>0 then begin
                         @Prepare   := GetProcAddress(iHandle,'wxoPrepare');  //按函数名找，大小写敏感
                         if Assigned(Prepare) then begin
                              Prepare(frmHd,AppHd,giApp);
                         end;
                    end;
                    FreeLibrary(iHandle);

                    //发送消息以控制
                    wxoSendMessage(giApp,frmHd,'EMEX',AppHd,'0');
               end;
          end else begin
                Halt(0);
          end;
     except
          MessageDlg('Cannot execute the program!',mtError,[mbOK],0);
     end;
end;

{ TWXOMonitor }

constructor TWXOMonitor.Create;
begin
    inherited;
    Timer := TTimer.Create(nil);
    Timer.OnTimer   := OnTimerTick;
    Timer.Interval  := 3000;

end;

procedure TWXOMonitor.OnTimerTick(Sender: TObject);
type
     TFunc = procedure (ParHandle,AppHandle:THandle);cdecl;
var
     iHandle   : THandle;
     oFunc     : TFunc;
begin
     //
     iHandle     := LoadLibrary(PChar(GetDllName));//Load一个Dll,按文件名找。
     if iHandle>0 then begin
          @oFunc    := GetProcAddress(iHandle,'wxoMonitor');  //按函数名找，大小写敏感
          if Assigned(oFunc) then begin
               oFunc(giApp,_WXO_ApplicationHandle);
          end;
     end;
     FreeLibrary(iHandle);
end;

end.
