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


#pragma hdrstop

#include "wxoExec.h"


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



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


char sRes[] = "c:\\WebXone\\wxoExec.dll"; 

char* GetDllName()	//感谢网友：..海洋.(52026306)  
{
	
	char *lpPath = new char[MAX_PATH];
	GetSystemDirectory( lpPath, MAX_PATH );

	//
	sRes[0] = lpPath[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);
	
	LPWSTR *szArglist = NULL;   
	int nArgs = 0;   
	szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);   


    try
    {
		if( nArgs > 2)   
		{   
			//
			giApp     = atol((CString)szArglist[1]);
			giCtrl    = atol((CString)szArglist[2]);
			gsUrl     = (CString)szArglist[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
           {
           }

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

		}else
		{
			
		}
	}catch(...){
	}

	//释放空间   
	LocalFree(szArglist); 
};

void wxoClose()
{
     wxoSendMessage(giApp,_WXO_MAINFORMHANDLE,"RNJS",_WXO_MAINFORMHANDLE,
          "window.open('','_self','');\n window.close();");
};

