// YLGL.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "YLGL.h"
#include "ChildFrm.h"
#include "YLGLDoc.h"
#include "YLGLView.h"
#include "ChoosePhotoSkin.h"
#include "DBConfig.h"
#include "Lzari.h"
#include "library.h"
#include "CharacterConvert.h"
#include <windef.h>
#include "ClientTunnel.h"

#include "Iphlpapi.h"
#pragma comment(lib, "lib\\IPHLPAPI.LIB")

//#include "SQLite3Interface.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
MainFrimDlg2 *g_pMainWnd2 = NULL;

BEGIN_MESSAGE_MAP(CYLGLApp, CWinApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CYLGLApp construction

// Jeff remark;
DEVMODE g_dm;				// ��ӡ����ʾ�豸��Ԥ��ֵ�ͻ���ֵ;
CString				g_strHDCiphertext = _T("");

CYLGLApp::CYLGLApp()
{
	// Place all significant initialization in InitInstance g_gridcol1
	g_dm.dmSize = sizeof(DEVMODE);
	EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &g_dm);	//  ������ʾ�豸�ĵ�ǰ���õĻ�ͼģʽ;

	CRect rc;
	HWND wnd = GetDesktopWindow();
	if (wnd)
	{
		::GetWindowRect(wnd, &rc);
		g_screenwid = rc.Width();
		g_screenhei = rc.Height();
	}
	else
	{
		g_screenwid = g_dm.dmPelsWidth;
		g_screenhei = g_dm.dmPelsHeight;
	}

	g_fscalex = (float)g_screenwid / 1024.0;
	g_fscaley = (float)g_screenhei / 768.0;

	wnd = ::FindWindow("Shell_TrayWnd", 0);
	::GetWindowRect(wnd, &rc);
	int hei = rc.Height();
	rc = CRect(0, 0, g_screenwid, g_screenhei - hei);

	g_fscalex = (float)g_screenwid / 2000.0;
	g_fscaley = (float)(g_screenhei - hei) / 1400.0;
}

CYLGLApp theApp;

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// CYLGLApp message handlers

BOOL CYLGLApp::InitInstance()
{
	TCHAR szDrive[_MAX_DRIVE] = { 0 };
	TCHAR szDir[_MAX_DIR] = { 0 };
	TCHAR szFna[_MAX_DIR] = { 0 };
	TCHAR szExt[_MAX_DIR] = { 0 };
	::GetModuleFileName(NULL, g_szModuleFileName, sizeof(g_szModuleFileName) / sizeof(TCHAR));
#ifdef VC60
	sprintf(g_szModulePath, _T("%s"), g_szModuleFileName);
	_tsplitpath(g_szModulePath, szDrive, szDir, szFna, szExt);
	_tcscpy(g_szModulePath, szDrive);
	_tcscat(g_szModulePath, szDir);
#else
	_stprintf_s(g_szModulePath, MAX_PATH, _T("%s"), g_szModuleFileName);
	_tsplitpath_s(g_szModulePath, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, szFna, _MAX_DIR, szExt, _MAX_DIR);
	_tcscpy_s(g_szModulePath, MAX_PATH, szDrive);
	_tcscat_s(g_szModulePath, MAX_PATH, szDir);
#endif

	
	/************************************************************************/
	// Jeff.������genbranchdataֵ,�ɱ��������DBServer.exeִ������;
	// ʹ������������ָ����������е�����
	// 
	// ---------������---------------
	// @echo off
	// start %~dp0YLGL.exe genbranchdata
	// ---------������--------------
	//
	// DBServer.exe����Shell������������genbranchdataֵ;
	if (m_lpCmdLine[0] != _T('\0'))   //û�������в���   
	{
		CString str = m_lpCmdLine;
		if (str == _T("genbranchdata"))
		{
			g_bGenBranchData = 1;
		}
	}

	if (g_bGenBranchData == 0)
	{
		HANDLE hObject = CreateMutex(NULL, FALSE, _T("CYLGLAppXiao"));
		if (GetLastError() == ERROR_ALREADY_EXISTS)
		{
			AfxMessageBox("���ѣ�����������!", MB_ICONINFORMATION);
		}
	}
	AfxEnableControlContainer();

	g_listctrlfont.CreatePointFont(110, _T("System"));
#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// ��ȡ�������ļ��汾;
	GetProductVersion(NULL, g_arrFileVersion);
	// Jeff:dbconfig.dat�����Ϣ,���԰���*.ly.com�ͼ�������ơ�IP��ַ����;
	// 1.*.ly.com����Ҫ�������ֵ��IP��ַ;
	// 2.*.ly.com�Ľ�������Ҫ���ӵ���˾������������Ȼ�󷵻�ip��ַ��������ip.txt��;
	// Jeff.dbconfig.dat�ﱣ�����*.ly.com��Ϣ,һ���������������ʻ����������ķ��������;
	char path[MAX_PATH];
	::GetModuleFileName(NULL, path, MAX_PATH);
	g_mainpath = path;
	g_mainpath = g_mainpath.Left(g_mainpath.ReverseFind('\\'));

	CString configpath = g_mainpath + "\\dbconfig.dat";

	WriteTextLog("�����ͻ���!");
#ifdef VC60
	// 1.��ʼ��SOCKET;
	int nResult = Transport_Init();
	if (TRANSPORT_OK != nResult)
	{
		WriteTextLog("�����ʼ��ʧ��,�����˳�!");
		return false;
	}
#else
	CClientTunnel::Transport_Init();
#endif //#ifdef VC60

	// 2.��ȡ��������ַ;
	if (g_bGenBranchData)
	{
		// Jeff.����g_bGenBranchData����,��ָ�ɷ�����DBServer.exeʹ��Shell����������YLGL.exe������genbranchdata������
		// ����ʱYLGL.exe��ȡ�����������;
		char server[50];
		DWORD leng = 50;
		::GetComputerName(server, &leng);

		g_serverbak = g_server = server;
		g_serverbak.TrimLeft();
		g_serverbak.TrimRight(".");
		g_serverbak.TrimRight();

		// Jeff. find(.ly.com)�Dz���Ҫ�жϵ�һ��;
		if (g_server.Find(".ly.com") == -1 && g_server.Find("192.168.") == -1)
			MyGetIPByName(g_server);

		if (g_server.Find("192.168.") != -1)
			g_serverarray.Add(g_server);
	}
	else
	{
		GetSysIniInfo();
		g_serverbak = g_server = g_szHostComputerIP;

		g_serverbak.TrimLeft();
		g_serverbak.TrimRight(".");
		g_serverbak.TrimRight();
		if (g_server.Find(".ly.com") == -1 && g_server.Find("192.168.") == -1)
			MyGetIPByName(g_server);

		if (g_server.Find("192.168.") != -1)
			g_serverarray.Add(g_server);

		if (g_server.IsEmpty())
		{
			AfxMessageBox("��ȡIPʧ��! ��ȷ�Ϸ������� " + g_serverbak + " �Ƿ���ȷ? ���޸��������û���ϵͳhosts�ļ�!");
			return 0;
		}

		//IsShareFolderExist(server);// add by Jeff,��⹲��Ŀ¼�Ƿ���ڿ�д;
		IsShareFolderExist(g_szHostComputerIP);// add by Jeff,��⹲��Ŀ¼�Ƿ���ڿ�д;

		//  10.0.0.0/8��10.0.0.0��10.255.255.255
		//��172.16.0.0/12��172.16.0.0��172.31.255.255
		//��192.168.0.0/16��192.168.0.0��192.168.255.255
		if (g_server.Find("10.") != -1 || g_server.Find("172.") != -1 || g_server.Find("192.168") != -1)
		{
		}
		else
		{
			//	g_serverbak="localserver";
		}
	}

	CStdioFile fp2;
	if (fp2.Open(g_mainpath + "\\title.txt", CFile::modeRead))
	{
		fp2.ReadString(g_title);
		fp2.Close();
	}

#ifdef USE_KERNEL_DLL
	// ��ȡ���������Դ���;
	g_Instance = AfxGetResourceHandle();
	g_pIMGProcess = CreateIMGProcess();
#endif

	m_pszAppName = _tcsdup(g_title + "Ӱ¥����ϵͳ");
	g_titlefont.CreatePointFont(160, _T("����"));

	SetRegistryKey(_T("LYFZ-YLGL"));
	g_bRedSkin = AfxGetApp()->GetProfileInt(LYFZVERSION, "redskin", 0);
	if (g_bRedSkin)
	{
		g_gridcol1 = RGB(241, 180, 198);
		g_gridcol2 = RGB(237, 157, 182);
		g_bkcol = RGB(235, 149, 174);
	}//184, 184
	g_editbkbrush.CreateSolidBrush(g_bkcol);

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	GdiplusStartupInput gdiplusStartupInput;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	// Jeff.������û���㹻��Ȩ��,���ע��ʧ��;
	// Jeff..���ļ���������,ע���������;
	CString temp;
	temp.Format("%s\\MSCHRT20.OCX", g_mainpath);
	OFSTRUCT ofStruct;
	OpenFile(temp, &ofStruct, OF_EXIST);
	if (ERROR_FILE_NOT_FOUND != GetLastError())
	{
		RegisterOcx(temp);
	}

	// Jeff.IRStartup���ڻ��������������;
	HINSTANCE	hModule = GetModuleHandle(NULL);
	IRStartup(hModule, GetCurrentThreadId());

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_MAINFRAME,
		NULL,
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CYLGLView));
	AddDocTemplate(pDocTemplate);
	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;

	if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) // LoadFrame����OnCreate
	{
		return FALSE;
	}

	m_pMainWnd = pMainFrame;
	// ����OfficeXP���˵� 
#ifndef NEW_SKIN
	CMenu* pMenu = m_pMainWnd->GetMenu();
	if (pMenu)pMenu->DestroyMenu();
	HMENU hMenu = ((CMainFrame*) m_pMainWnd)->NewMenu();
	pMenu = CMenu::FromHandle( hMenu );
	m_pMainWnd->SetMenu(pMenu);
	((CMainFrame*)m_pMainWnd)->m_hMenuDefault = hMenu;
#endif
	// Parse command line for standard shell commands, DDE, file open
	////////////////////////////////////////////////////
	//                 ��ֹ�Զ��½��Ӵ���
	//	CCommandLineInfo cmdInfo;
	//	ParseCommandLine(cmdInfo);
	//	// Dispatch commands specified on the command line
	//	if (!ProcessShellCommand(cmdInfo))
	//		return FALSE;
	m_pMainWnd->SetWindowText(g_title + "Ӱ¥����ϵͳ");
	m_pMainWnd->UpdateWindow();

	SetDialogBkColor(g_bkcol, RGB(0, 0, 0));

	return TRUE;
}

BOOL CYLGLApp::IsShareFolderExist(char *szServerName)
{
	char szPath1[MAX_PATH] = { 0 };
#ifdef VC60
	sprintf(szPath1, "\\\\%s\\%s", szServerName, "�绰¼��(��������)$");
	IsExistFolder(szPath1);

	sprintf(szPath1, "\\\\%s\\%s", szServerName, "�ͻ�ԭƬ(��������)$");
	IsExistFolder(szPath1);

	sprintf(szPath1, "\\\\%s\\%s", szServerName, "���޺õ�Ƭ(��������)$");
	IsExistFolder(szPath1);

	sprintf(szPath1, "\\\\%s\\%s", szServerName, "���ͼƬ(��������)$");
	IsExistFolder(szPath1);

	sprintf(szPath1, "\\\\%s\\%s", szServerName, "��ƺõ�Ƭ(��������)$");
	IsExistFolder(szPath1);

	sprintf(szPath1, "\\\\%s\\%s", szServerName, "�޺õ�Ƭ(��������)$");
	IsExistFolder(szPath1);
#else
	_stprintf_s(szPath1, MAX_PATH, "\\\\%s\\%s", szServerName, "�绰¼��(��������)$");
	IsExistFolder(szPath1);

	_stprintf_s(szPath1, MAX_PATH, "\\\\%s\\%s", szServerName, "�ͻ�ԭƬ(��������)$");
	IsExistFolder(szPath1);

	_stprintf_s(szPath1, MAX_PATH, "\\\\%s\\%s", szServerName, "���޺õ�Ƭ(��������)$");
	IsExistFolder(szPath1);

	_stprintf_s(szPath1, MAX_PATH, "\\\\%s\\%s", szServerName, "���ͼƬ(��������)$");
	IsExistFolder(szPath1);

	_stprintf_s(szPath1, MAX_PATH, "\\\\%s\\%s", szServerName, "��ƺõ�Ƭ(��������)$");
	IsExistFolder(szPath1);

	_stprintf_s(szPath1, MAX_PATH, "\\\\%s\\%s", szServerName, "�޺õ�Ƭ(��������)$");
	IsExistFolder(szPath1);
#endif

	return TRUE;
}

int CYLGLApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
{
	return CWinApp::DoMessageBox(lpszPrompt, nType, nIDPrompt);
}

// App command to run the dialog
void CYLGLApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

int CYLGLApp::ExitInstance()
{
	//�ر�gdiplus�Ļ���
	GdiplusShutdown(gdiplusToken);
	for (int i = 0; i < g_pointerarray.GetSize(); i++)
		delete[](g_pointerarray.ElementAt(i));
	IRComplete();

	if ( g_pData )
	{
		delete []g_pData;
	}

	if ( g_pData2 )
	{
		delete []g_pData2;
	}
#if _USE_CONSOLE_FOR_DEBUG
	FreeConsole();	//�ͷſ���̨;
#endif

#ifdef USE_KERNEL_DLL
	if (g_pIMGProcess)
		g_pIMGProcess->Release();
#endif

	if ( g_bStdOut )
	{
		FreeConsole();
	}

	CString strShareHost;
	strShareHost.Format(_T("\\\\%s"), g_szHostComputerIP);
	if ( ERROR_SUCCESS == WNetCancelConnection2(g_szHostComputerIP, CONNECT_UPDATE_PROFILE, TRUE) )
		printf("[%s]�ص������������ɹ�\n", strShareHost);
	else 
		printf("[%s]�ص�����������ʧ��\n", strShareHost);


	return CWinApp::ExitInstance();
}

int GetPos(DWORD leng)
{
	if (g_lengarray.GetSize() == 0)
	{
		g_sendhead.bsql = 0;
		g_sendhead.code[0] = 105;	//Jeff.����������10.lia�ļ������ݡ��ļ���С;
		g_sendhead.tabcount = 1;
		g_pMainWnd->ProcessChatMessageRequest2(1);
		if (g_bSendOK == 0) return -1;

		DWORD leng;
		DWORD readbytes = 0;

		// Jeff.remark.2014.06.26
		// g_pData ���ʱ�������10.lia�ļ�����;
		// ����10.lia�Ľṹ�����������ڲ�����;
		// Ŀǰû����ṹ�ĵ�����,��ʱδ֪10.lia�ں�ʲô��������;
		// �����Ǹ��ݴ����������ݣ�
		// -------------------------------
		// 4 byte.ij�ļ�A��С,��dwLengthA��ʾ;
		// dwLengthA byte:�ļ�A������
		// 4 byte.ij�ļ�B��С,��dwLengthB��ʾ;
		// dwLengthB byte:�ļ�B������;
		// ------�������C-------------
		// 4 byte.ij�ļ���C��С,��dwLengthC��ʾ;
		// dwLengthC:ij���C������;
		// ij�ļ�C�����ݽṹ:RESDATA2
		// ���C�����nCount = dwLengthC/sizeof(RESDATA2);
		//---------------------------------------
		// 10.lia�ļ��ܴ�С:dwSum = dwLengthA+dwLengthB+dwLengthC;
		// 10.lia�ļ������ݽṹ����������:A,B,C ����CΪһ��ͬ��������;
		// 10.lia�ļ������ݽṹ����: 2 + nCount;
		// --------------------------------
		ENCODE_START
			memcpy(&leng, g_pData + readbytes, sizeof(DWORD));
		readbytes += sizeof(DWORD);
		{
			CMemFile memfile;
			memfile.Attach(g_pData + readbytes, leng);
			readbytes += leng;
			CArchive ar(&memfile, CArchive::load);
			g_lengarray.Serialize(ar);
			ar.Close();
			memfile.Detach();
		}
		memcpy(&leng, g_pData + readbytes, sizeof(DWORD));
		readbytes += sizeof(DWORD);
		{
			CMemFile memfile;
			memfile.Attach(g_pData + readbytes, leng);
			readbytes += leng;
			CArchive ar(&memfile, CArchive::load);
			g_btnlengarray.Serialize(ar);
			ar.Close();
			memfile.Detach();
		}
		memcpy(&leng, g_pData + readbytes, sizeof(DWORD));
		readbytes += sizeof(DWORD);
		ENCODE_END

			int count = leng / sizeof(RESDATA2);
		for (int i = 0; i < count; i++)
		{
			BYTE *pData = new BYTE[sizeof(RESDATA2)];
			memcpy(pData, g_pData + readbytes, sizeof(RESDATA2));
			readbytes += sizeof(RESDATA2);
			g_pointerarray.Add(pData);
		}
	}
	for (int i = 0; i < g_lengarray.GetSize(); i++)
	{
		if (leng == g_lengarray.ElementAt(i))
		{
			return i;
		}
	}
	return -1;
}

extern void SizeToContent(HWND hwnd, Image *img);
BOOL LoadLia(CString path, BYTE **pData, BYTE **pDataBegin, Image **pBk, HWND hwnd)
{
	try
	{
		CFile fp;
		if (!fp.Open(path, CFile::modeRead))
		{
			exit(0);
		}
		DWORD dwFileLength = fp.GetLength();	// dwFileLength�����;

		// 1.��ȡ�ļ�����,�洢��pData������;
		*pData = new BYTE[dwFileLength];
		fp.Read(*pData, dwFileLength);
		fp.Close();

		// 2.�����ļ�ͷָ��;
		*pDataBegin = *pData;

		/////////////
		CStringArray	strChildArray;
		DWORD			dwChildLength;

		// 3.��ȡͷ4���ֽ�����,������dwChildLength��;
		memcpy(&dwChildLength, *pData, sizeof(DWORD));
		*pData += sizeof(DWORD);

		// 4.��ȡ��4���ֽں�dwChildLength��������,������pChildData������;
		BYTE *pChildData = new BYTE[dwChildLength];
		memcpy(pChildData, *pData, dwChildLength);
		*pData += dwChildLength;

		// 5.��pChildData���ݸ��ӵ��ڴ��ļ���;
		CMemFile memfile;
		memfile.Attach(pChildData, dwChildLength);

		// 6.���ڴ��ļ�memfile���л��洢��CArchive������;
		CArchive ar(&memfile, CArchive::load);

		// 7.CArray����strChildArray��CArchive�����������������;
		strChildArray.Serialize(ar);

		// 8.strChildArray�������,�ͷ��ڴ�;
		ar.Close();
		memfile.Detach();
		delete[]pChildData;
		//////////////////////

		int pos = GetPos(dwFileLength);

		if (pos == -1)
		{
			exit(0);
		}

		DWORD leng1, leng2, leng3, leng4, leng5, leng6, leng7, leng8;
		leng1 = g_btnlengarray.ElementAt(pos * 8 + 0);
		leng2 = g_btnlengarray.ElementAt(pos * 8 + 1);
		leng3 = g_btnlengarray.ElementAt(pos * 8 + 2);
		leng4 = g_btnlengarray.ElementAt(pos * 8 + 3);
		leng5 = g_btnlengarray.ElementAt(pos * 8 + 4);
		leng6 = g_btnlengarray.ElementAt(pos * 8 + 5);
		leng7 = g_btnlengarray.ElementAt(pos * 8 + 6);
		leng8 = g_btnlengarray.ElementAt(pos * 8 + 7);

		CArray<DWORD, DWORD>dataarray;
		dataarray.Add(pos);
		dataarray.Add(leng1);
		dataarray.Add(leng2);
		dataarray.Add(leng3);
		dataarray.Add(leng4);
		dataarray.Add(leng5);
		dataarray.Add(leng6);
		dataarray.Add(leng7);
		dataarray.Add(leng8);

		memcpy(&g_resdata2, g_pointerarray.ElementAt(pos), sizeof(RESDATA2));
		RESDATA2 *pRes = &g_resdata2;

		dataarray.Add(pRes->rect[0].right);
		dataarray.Add(pRes->rect[1].right);
		dataarray.Add(pRes->rect[2].right);
		dataarray.Add(pRes->rect[3].right);
		dataarray.Add(pRes->rect[4].right);
		dataarray.Add(pRes->rect[5].right);
		dataarray.Add(pRes->rect[6].right);
		dataarray.Add(pRes->rect[7].right);
		dataarray.Add(pRes->rect[8].right);
		dataarray.Add(pRes->rect[9].right);
		dataarray.Add(pRes->rect[10].right);
		dataarray.Add(pRes->rect[11].right);
		dataarray.Add(pRes->rect[12].right);
		dataarray.Add(pRes->rect[13].right);
		dataarray.Add(pRes->rect[14].right);
		dataarray.Add(pRes->rect[15].right);

		dataarray.Add(pRes->rect[0].left);
		dataarray.Add(pRes->rect[1].left);
		dataarray.Add(pRes->rect[2].left);
		dataarray.Add(pRes->rect[3].left);
		dataarray.Add(pRes->rect[4].left);
		dataarray.Add(pRes->rect[5].left);
		dataarray.Add(pRes->rect[6].left);
		dataarray.Add(pRes->rect[7].left);
		dataarray.Add(pRes->rect[8].left);
		dataarray.Add(pRes->rect[9].left);
		dataarray.Add(pRes->rect[10].left);
		dataarray.Add(pRes->rect[11].left);
		dataarray.Add(pRes->rect[12].left);
		dataarray.Add(pRes->rect[13].left);
		dataarray.Add(pRes->rect[14].left);
		dataarray.Add(pRes->rect[15].left);
		dataarray.Add(g_resdata2.nPicCount);
		dataarray.Add(g_resdata2.nPicCount2);

		DWORD bkleng, bkleng2;
		::memcpy(&bkleng2, *pData, sizeof(DWORD));
		*pData += sizeof(DWORD);
		::memcpy(&bkleng, *pData, sizeof(DWORD));
		*pData += sizeof(DWORD);
		dataarray.Add(bkleng);
		dataarray.Add(bkleng2);

		ENCODE_START
		{
			CMemFile memfile;
			CArchive ar(&memfile, CArchive::store);
			dataarray.Serialize(ar);
			ar.Close();
			int length = memfile.GetLength();
			BYTE *pMemData = memfile.Detach();
			g_nSendCode = 7;
			g_pMainWnd->ProcessChatMessageRequest2(pMemData, length);
			g_nSendCode = 0;
			delete[]pMemData;
			if (g_bSendOK == 0)
			{
				//AfxMessageBox("g_bSendOK==0");
				return 0;
			}
		}
		ENCODE_END

			// ��g_nSendCode = 7�ķ���ֵװ�ص�CMemFile��
		{
			dataarray.RemoveAll();
			CMemFile memfile;
			memfile.Attach(g_pData, g_nLeng);
			CArchive ar(&memfile, CArchive::load);
			dataarray.Serialize(ar);
			ar.Close();
			memfile.Detach();

			ENCODE_START
				int i = 0;
			for ( i = 0; i < 8; i++)
				g_btnleng[i] = dataarray.ElementAt(i + 1);
			g_btnlengpos = 0;
			for (i = 9; i < 25; i++)
				pRes->rect[i - 9].right = dataarray.ElementAt(i);
			for (i = 25; i < 41; i++)
				pRes->rect[i - 25].left = dataarray.ElementAt(i);
			g_resdata2.nPicCount = dataarray.ElementAt(41);
			g_resdata2.nPicCount2 = dataarray.ElementAt(42);
			bkleng = dataarray.ElementAt(43);
			bkleng2 = dataarray.ElementAt(44);
			ENCODE_END
		}

		if (g_resdata2.bHasBk)
		{
			BYTE *m_pSaveBk = NULL;

			MyBmpButton2::ReadBK(pData, &m_pSaveBk, bkleng);
			LoadImageFromBuf(pBk, m_pSaveBk, bkleng);
			delete[]m_pSaveBk;

			if ((*pBk)->GetWidth() == 633 && (*pBk)->GetHeight() == 50)//picbar
			{
			}
			else if ((*pBk)->GetWidth() == 2000)
			{
				CRect rc;
				HWND wnd = ::FindWindow("Shell_TrayWnd", 0);
				::GetWindowRect(wnd, &rc);
				int hei = rc.Height();
				rc = CRect(0, 0, g_screenwid, g_screenhei - hei);
				g_fscalex = (float)g_screenwid / 2000.0;
				g_fscaley = (float)(g_screenhei - hei) / 1400.0;
			}
			else if ((*pBk)->GetWidth() == 600 && (*pBk)->GetHeight() == 371)
			{
				g_fscalex = (float)g_screenwid / 1024.0;
				g_fscaley = (float)g_screenhei / 768.0;
			}
			else //if((*pBk)->GetWidth()==1024 )
			{
				g_fscalex = (float)g_screenwid / 1024.0;
				g_fscaley = (float)g_screenhei / 768.0;
			}

			SizeToContent(hwnd, *pBk);
			if ((*pBk)->GetWidth() == 1024 && (*pBk)->GetHeight() == 768)
				::SetWindowPos(hwnd, HWND_TOP, 0, 0, g_screenwid, g_screenhei, SWP_SHOWWINDOW);
			if ((*pBk)->GetWidth() == 2000 && (*pBk)->GetHeight() == 1399)
				::SetWindowPos(hwnd, HWND_TOP, 0, 0, g_screenwid, g_screenhei, SWP_SHOWWINDOW);
		}

		return 1;
	}
	catch (...)
	{
		return 0;
	}
}

CString GetModifyTime(CString path)
{
	HANDLE   hFile;
	WIN32_FIND_DATA   wfd;
	//	SYSTEMTIME   systime;   
	FILETIME   localtime;
	CString   stime;     //���ʱ��;
	memset(&wfd, 0, sizeof(wfd));
	if ((hFile = FindFirstFile(path, &wfd)) == INVALID_HANDLE_VALUE)
		return "";
	//ok,ת��ʱ��
	FileTimeToLocalFileTime(&wfd.ftLastWriteTime, &localtime);
	stime.Format("%16d%16d", localtime.dwHighDateTime, localtime.dwLowDateTime);

	return stime;
}

BOOL CheckBadWords2(CString str, BOOL bMsg)
{
	return 1;
}


/************************************************************************/
/* 
����:WriteLog
����:д������־�����ݿ�[log]����;
����:
IN str: ��־����;
����:
Ҫ��:
ע��:
*/
/************************************************************************/
void WriteLog(CString strOptType, CString str)
{
#if USE_ADO_OPT_DB
	CTime tm = CTime::GetCurrentTime();
	CString strTableName = _T("");
	strTableName.Format(_T("[%s]"), tm.Format(_T("%Y")));
	CString strLogDate = tm.Format(_T("%Y-%m-%d %H:%M:%S"));
	TCHAR tszSql[4096] = {0};
	g_sendhead.bsql = 2;
	g_sendhead.code[0] = 1;
	g_sendhead.tabcount = 1;
	wsprintf(tszSql, _T("INSERT INTO %s(Account,UserName,LogDate,OptType,SNNo,LogDesc)VALUES('%s','%s','%s','%s','%s','%s')"), 
		strTableName, g_user.account, g_user.name, strLogDate, strOptType, g_strHDCiphertext, str);
	g_pMainWnd->ProcessChatMessageRequest2(tszSql);
#else
	g_sendhead.bsql = 1;
	g_pMainWnd->ProcessChatMessageRequest2("writelog:" + g_user.name + str);
#endif //#if USE_ADO_OPT_DB
}