//---------------------------------------------------------------------------


#pragma hdrstop

#include <vcl.h>
#include "wxoExec.h"


//---------------------------------------------------------------------------

#pragma package(smart_init)


unsigned int giApp;
unsigned int giCtrl;
AnsiString gsUrl;
unsigned int _WXO_MAINFORMHANDLE;
unsigned int _WXO_ApplicationHandle;

char* GetDllName()
{
     //默认返回值
     char* sRes = "c:\\WebXone\\wxoExec.dll";
     //
     char* str = new char[200];
     GetSystemDirectory(str, 200);

     //
     sRes[0]   = str[0];
     //
     return sRes;
};

void wxoSendMessage(
          unsigned int DestHandle,
          unsigned int LocalHandle,
          char* sHead,
          int Value,
          char* Text)
{
     //把要调用的方法定义为一种类型，就无需在后面用到时多次进行调制类型转换
     typedef int (__cdecl *_Verify)(unsigned int DestHandle,
          unsigned int LocalHandle,
          char* sHead,
          long Value,
          char* Text);

     HINSTANCE hDLL;
     _Verify Verify;
     hDLL = LoadLibrary(GetDllName());
     if (hDLL){
          Verify = (_Verify)GetProcAddress(hDLL, "wxoSendMessage");
          if (Verify){
               Verify(DestHandle, LocalHandle, sHead, Value, Text);
          }else
          {
               MessageBox(0,"SM false","",0);
          }
     }else
     {
          MessageBox(0,"load sm false","",0);
     }
     FreeLibrary(hDLL);

};


void wxoPrepare(unsigned int frmHd,unsigned int appHd)
{
     //把要调用的方法定义为一种类型，就无需在后面用到时多次进行调制类型转换
     typedef int (__cdecl *_Verify)(unsigned int FormHd, unsigned int AppHd, unsigned int ParHd);

     try
     {
          if (ParamCount()>2)
          {
               //从执行参数中得到数值
               giApp     = StrToIntDef(ParamStr(1),0);
               giCtrl    = StrToIntDef(ParamStr(2),0);
               gsUrl     = ParamStr(3);

               //保存值以备用
               _WXO_MAINFORMHANDLE      = frmHd;
               _WXO_ApplicationHandle   = appHd;


               if ((giApp>0)&&(giCtrl>0)){
                    HINSTANCE hDLL;
                    _Verify Verify;
                    hDLL = LoadLibrary(GetDllName());
                    if (hDLL){
                         Verify = (_Verify)GetProcAddress(hDLL, "wxoPrepare");
                         if (Verify){
                              Verify(frmHd, appHd,giApp);
                         }else
                         {
                              MessageBox(0,"GetProcAddress false","",0);
                         }
                    }else
                    {
                         MessageBox(0,"load asdfdsf false","",0);
                    }
                    FreeLibrary(hDLL);
               }else
               {
                    MessageBox(0,"giApp false","",0);
               }

               //发送消息以控制
               wxoSendMessage(giApp,
                         frmHd,
                         "EMEX",
                         appHd,
                         "0");

          }else
          {
               //MessageBox(0,"param count load false","",0);
          }
     }catch(...){
     }

};

void wxoClose()
{
     wxoSendMessage(giApp,_WXO_MAINFORMHANDLE,"RNJS",_WXO_MAINFORMHANDLE,
          "window.open('''',''_self'','''');'#13#10'window.close();");
};

