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

#include "stdafx.h"
#include "LYFZIPManage.h"
#include "ChildFrm.h"
#include "LYFZIPManageDoc.h"
#include "LYFZIPManageView.h"
#include "Lzari.h"
#include <tlhelp32.h>
#include <wincrypt.h>
#include <Mmsystem.h>
#include "mysqldata.h"
#include <afxsock.h>
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#define KEYLENGTH  0x00800000
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 8 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//Font  *g_BlackFont=NULL;
CDatabase *g_curdb=NULL;
CDatabase g_db;
CString g_date;
CString g_title;
CArray<CStringArray, CStringArray>g_userarray;
CArray<CStringArray, CStringArray>g_cominfoarray;
CArray<DWORD,DWORD>g_lengarray;
CArray<DWORD,DWORD>g_btnlengarray;
CArray<BYTE*,BYTE*>g_pointerarray;

DWORD g_btnleng[8];
int   g_btnlengpos=-1;
int  g_nYearpos=-1;
/////////////////////////////////////////////////////////////////////////////
// CLYFZIPManageApp
BOOL g_bShowOK=0;
BOOL g_bStaffAchievementShow=0;

USER g_user;
CString g_mainpath;
CString g_server;
CString g_path1;
CString g_path2;
CString g_path3;
CString g_path4;

DWORD g_ncount;
CFont g_titlefont;
DWORD g_nSendCode=0;
BOOL g_bSendOK=0;
SENDHEAD g_sendhead;
CArray<CStringArray, CStringArray>g_hisyeararray;
extern  CFont g_listctrlfont;
#pragma comment(lib, "Transport.lib")
#pragma comment(lib, "Winmm.lib")

BEGIN_MESSAGE_MAP(CLYFZIPManageApp, CWinApp)
	//{{AFX_MSG_MAP(CLYFZIPManageApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	// NOTE - the ClassWizard will add and remove mapping macros here.
	//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLYFZIPManageApp construction

float g_fscalex;
float g_fscaley;
DEVMODE g_dm;
CLYFZIPManageApp::CLYFZIPManageApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	g_dm.dmSize = sizeof(DEVMODE) ;
	EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&g_dm);
	g_fscalex=(float)g_dm.dmPelsWidth/1024.0;
	g_fscaley=(float)g_dm.dmPelsHeight/768.0;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CLYFZIPManageApp object

CLYFZIPManageApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CLYFZIPManageApp initialization

DWORD FindAppProcessID(CString path)
{
	try
	{
		HANDLE handle=::CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
		PROCESSENTRY32 Info;
		Info.dwSize = sizeof(PROCESSENTRY32);
		path.MakeLower ();
		if(::Process32First(handle,&Info))
		{
			do
			{
				CString ss=Info.szExeFile;
				ss.MakeLower ();
				if(ss.Find (path)!=-1 || (!ss.CompareNoCase(path)) )
				{
					::CloseHandle(handle);
					return Info.th32ProcessID;
				}
			}
			while(::Process32Next(handle,&Info));	
			::CloseHandle(handle);
		}
		return -1;
	}
	catch(...)
	{
	}
}

BOOL MyExecuteSQL(CDatabase *pdb, CString sql)
{
	MyLock lock("xiaoaccessdbmsg");
	try
	{
		pdb->ExecuteSQL (sql);
	}
	catch(...)
	{
		return 0;
	}
	return 1;
}

BOOL CLYFZIPManageApp::InitInstance()
{
	HANDLE hObject = CreateMutex(NULL,FALSE,_T("CLYFZIPManageAppxiao"));
	if(GetLastError() == ERROR_ALREADY_EXISTS)
	{
		return false;
	}
	char path[MAX_PATH];
	::GetModuleFileName (NULL, path, MAX_PATH);
	g_mainpath=path;
	g_mainpath=g_mainpath.Left (g_mainpath.ReverseFind ('\\'));
	AfxEnableControlContainer();
	WriteLogin("��ʼ����");
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.
	//g_listctrlfont.CreatePointFont (110, _T("System"));
	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
	CString autorunpath=g_mainpath+"\\AutoRun3.exe";
	ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL,  SW_SHOWNORMAL);
	BOOL bMasterDb=0;
	BOOL bUserDb=0;
	char server[50];
	DWORD leng=50;
	memset(server, 0, 50);
	::GetComputerName (server, &leng);	
	CString localname=server;

	Global::GetIniInfo();

	CDatabase g_masterdb;
ll:	try
	{
		CString strCon;			
		if (Global::g_dwDBServerPort != 0)
		{
			if ( _tcscmp(Global::g_szDBAccount, _T("")) == 0 )
				strCon.Format(DB_SW_CONN_WITH_PORT, Global::g_szDBSource, Global::g_dwDBServerPort,_T("master"));
			else
				strCon.Format(DB_SS_CONN_WITH_PORT,Global::g_szDBSource, Global::g_dwDBServerPort, _T("master"), Global::g_szDBAccount, Global::g_szDBPassWord);
		}
		else
		{
			if ( _tcscmp(Global::g_szDBAccount, _T("")) == 0 )
				strCon.Format(DB_SW_CONN_WITHOUT_PORT, Global::g_szDBSource, _T("master"));
			else
				strCon.Format(DB_SS_CONN_WITHOUT_PORT,Global::g_szDBSource, _T("master"), Global::g_szDBAccount, Global::g_szDBPassWord);
		}

		g_masterdb.OpenEx(strCon,CDatabase::noOdbcDialog); 
		bMasterDb=1;
	}
	catch(CDBException * e)
	{
	}

	if(bMasterDb==0)
	{	
		WriteLogin("sql���ݿ�δ����!");
		::Sleep (1000);
		goto ll;
	}

	try
	{
		g_db.OpenEx(Global::g_szConnectString,CDatabase::noOdbcDialog);
		bUserDb=1;
		g_curdb=&g_db; 
		g_masterdb.Close();	
	}
	catch(CDBException * e)
	{
		WriteLogin("���ݿ�����ʧ��-"+e->m_strError);
		e->Delete();
		::Sleep (1000);
		goto ll;
	}


	SetRegistryKey(_T("LYFZ-LYFZIPManage"));

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

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.
	g_titlefont.CreatePointFont (160, _T("����"));

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_MAINFRAME,
		NULL,
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CLYFZIPManageView));
	AddDocTemplate(pDocTemplate);
	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
	{
		WriteLogin("���ڳ�ʼ��ʧ��");
		return FALSE;
	}
	m_pMainWnd = pMainFrame;


	m_pMainWnd->SetWindowText ("���Ƿ�����������ϵͳ");

	m_pMainWnd->UpdateWindow();

	//	SetDialogBkColor(RGB(204,204,204),RGB(0,0,0));

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

/////////////////////////////////////////////////////////////////////////////
// CLYFZIPManageApp message handlers

CString newGUID()
{
	CString str;
	GUID guid;
	CoInitialize(NULL);
	if (S_OK == ::CoCreateGuid(&guid))
	{
		str.Format ( 
			"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
			guid.Data1,
			guid.Data2,
			guid.Data3,
			guid.Data4[0], guid.Data4[1],
			guid.Data4[2], guid.Data4[3],
			guid.Data4[4], guid.Data4[5],
			guid.Data4[6], guid.Data4[7]);
	}
	CoUninitialize();
	return str.Left (28);
}

void ConvertToPrice(CString &str)
{
	if(str.Find ('.')==-1)return;
	str.TrimRight ('0');
	str.TrimRight ('.');
}

void DataToArray(CArray<CStringArray, CStringArray>*pArrayOfString)
{
	try
	{
		pArrayOfString->RemoveAll();
		if (g_nLeng == 0)
		{
			return;
		}

		// 1.code[0]���飺��������ʱ,�洢�Ų���ָ��;��������ʱ,�洢�Ž������ݴ�С???;
		if (g_sendhead.code[0])
		{
			// 1.1.��g_pDataȫ�ֱ����ﱣ������ݽ��н�ѹ,���洢��lpszOut������;
			BYTE *lpszOut = NULL;
			int nOutSize = 0;
			LZARI Lzari;
#if defined(VC70) || defined(VC60)
			Lzari.UnCompress(g_pData, g_nLeng, lpszOut, nOutSize);
#else
			Lzari.UnCompress(g_pData, g_nLeng, (const BYTE*&)lpszOut, nOutSize);
#endif

			// 1.2.����ѹ�������lpszOut���ӵ��ڴ��ļ���;
			CMemFile memfile;
			memfile.Attach(lpszOut, nOutSize);

			// 1.3.���ڴ��ļ����л����浽CArchive������;
			CArchive ar(&memfile, CArchive::load);
			pArrayOfString->SetSize(g_sendhead.count[0]);

			// 1.4.��CArchive�����������,���л���ָ����CArray������;
			for (int ii = 0; ii < pArrayOfString->GetSize(); ii++)
			{
				pArrayOfString->ElementAt(ii).Serialize(ar);
			}

			// 1.5.������ݵĵ�������,�ͷ��ڴ�;
			ar.Close();
			Lzari.Release();
			memfile.Detach();
		}
		else
		{
			CMemFile memfile;
			memfile.Attach(g_pData, g_nLeng);
			CArchive ar(&memfile, CArchive::load);
			pArrayOfString->SetSize(g_sendhead.count[0]);
			for (int ii = 0; ii < pArrayOfString->GetSize(); ii++)
			{
				pArrayOfString->ElementAt(ii).Serialize(ar);
			}
			ar.Close();
			memfile.Detach();
		}
	}
	catch (...)
	{
	}
}

void DataToArray(
				 CArray<CStringArray, CStringArray>*pArrayOfString1,
				 CArray<CStringArray, CStringArray>*pArrayOfString2)
{
	try
	{
		CArray<CStringArray, CStringArray>*parray[2] = { pArrayOfString1, pArrayOfString2 };
		DWORD bytereads = 0;
		for (int i = 0; i < 2; i++)
		{
			parray[i]->RemoveAll();
			if (g_sendhead.length[i] == 0)
				continue;
			if (g_sendhead.code[i])
			{
				// 1.1.��g_pDataȫ�ֱ����ﱣ������ݽ��н�ѹ,���洢��lpszOut������;
				BYTE *lpszOut = NULL;
				int nOutSize = 0;
				LZARI Lzari;
#ifdef VC60
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], lpszOut, nOutSize);
#else
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], (const BYTE*&)lpszOut, nOutSize);
#endif

				// 1.2.����ѹ�������lpszOut���ӵ��ڴ��ļ���;
				CMemFile memfile;
				memfile.Attach(lpszOut, nOutSize);

				// 1.3.���ڴ��ļ����л����浽CArchive������;
				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);

				// 1.4.��CArchive�����������,���л���ָ����CArray������;
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}

				// 1.5.������ݵĵ�������,�ͷ��ڴ�;
				ar.Close();
				Lzari.Release();
				memfile.Detach();
			}
			else
			{
				CMemFile memfile;
				memfile.Attach(g_pData + bytereads, g_sendhead.length[i]);
				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}
				ar.Close();
				memfile.Detach();
			}
		}
	}
	catch (...)
	{
	}
}

void DataToArray(
				 CArray<CStringArray, CStringArray>*pArrayOfString1,
				 CArray<CStringArray, CStringArray>*pArrayOfString2,
				 CArray<CStringArray, CStringArray>*pArrayOfString3)
{
	try
	{
		CArray<CStringArray, CStringArray>*parray[3] = { pArrayOfString1, pArrayOfString2, pArrayOfString3 };
		DWORD bytereads = 0;
		for (int i = 0; i < 3; i++)
		{
			parray[i]->RemoveAll(); if (g_sendhead.length[i] == 0)continue;
			if (g_sendhead.code[i])
			{
				BYTE *lpszOut = NULL;
				int nOutSize = 0;
				LZARI Lzari;
#ifdef VC60
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], lpszOut, nOutSize);
#else
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], (const BYTE*&)lpszOut, nOutSize);
#endif
				CMemFile memfile;
				memfile.Attach(lpszOut, nOutSize);

				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}

				ar.Close();
				Lzari.Release();
				memfile.Detach();
			}
			else
			{
				CMemFile memfile;
				memfile.Attach(g_pData + bytereads, g_sendhead.length[i]);
				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}
				ar.Close();
				memfile.Detach();
			}
		}
	}
	catch (...)
	{
	}
}

void DataToArray(
				 CArray<CStringArray, CStringArray>*pArrayOfString1,
				 CArray<CStringArray, CStringArray>*pArrayOfString2,
				 CArray<CStringArray, CStringArray>*pArrayOfString3,
				 CArray<CStringArray, CStringArray>*pArrayOfString4)
{
	try
	{
		CArray<CStringArray, CStringArray>*parray[4] =
		{
			pArrayOfString1,
			pArrayOfString2,
			pArrayOfString3,
			pArrayOfString4
		};
		DWORD bytereads = 0;
		for (int i = 0; i < 4; i++)
		{
			parray[i]->RemoveAll(); if (g_sendhead.length[i] == 0)continue;
			if (g_sendhead.code[i])
			{
				BYTE *lpszOut = NULL;
				int nOutSize = 0;
				LZARI Lzari;
#ifdef VC60
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], lpszOut, nOutSize);
#else
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], (const BYTE*&)lpszOut, nOutSize);
#endif
				CMemFile memfile;
				memfile.Attach(lpszOut, nOutSize);

				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}

				ar.Close();
				Lzari.Release();
				memfile.Detach();
			}
			else
			{
				CMemFile memfile;
				memfile.Attach(g_pData + bytereads, g_sendhead.length[i]);
				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}
				ar.Close();
				memfile.Detach();
			}
		}
	}
	catch (...)
	{
	}
}

void DataToArray(
				 CArray<CStringArray, CStringArray>*pArrayOfString1,
				 CArray<CStringArray, CStringArray>*pArrayOfString2,
				 CArray<CStringArray, CStringArray>*pArrayOfString3,
				 CArray<CStringArray, CStringArray>*pArrayOfString4,
				 CArray<CStringArray, CStringArray>*pArrayOfString5)
{
	try
	{
		CArray<CStringArray, CStringArray>*parray[5] =
		{
			pArrayOfString1,
			pArrayOfString2,
			pArrayOfString3,
			pArrayOfString4,
			pArrayOfString5
		};
		DWORD bytereads = 0;
		for (int i = 0; i < 5; i++)
		{
			parray[i]->RemoveAll();
			if (g_sendhead.length[i] == 0)
				continue;
			if (g_sendhead.code[i])
			{
				BYTE *lpszOut = NULL;
				int nOutSize = 0;
				LZARI Lzari;
#ifdef VC60
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], lpszOut, nOutSize);
#else
				Lzari.UnCompress(g_pData + bytereads,  g_sendhead.length[i], (const BYTE*&)lpszOut, nOutSize);
#endif
				CMemFile memfile;
				memfile.Attach(lpszOut, nOutSize);

				bytereads += g_sendhead.length[i];

				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}

				ar.Close();
				Lzari.Release();
				memfile.Detach();
			}
			else
			{
				CMemFile memfile;
				memfile.Attach(g_pData + bytereads, g_sendhead.length[i]);
				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}
				ar.Close();
				memfile.Detach();
			}
		}
	}
	catch (...)
	{
	}
}

void DataToArray(
				 CArray<CStringArray, CStringArray>*pArrayOfString1,
				 CArray<CStringArray, CStringArray>*pArrayOfString2,
				 CArray<CStringArray, CStringArray>*pArrayOfString3,
				 CArray<CStringArray, CStringArray>*pArrayOfString4,
				 CArray<CStringArray, CStringArray>*pArrayOfString5,
				 CArray<CStringArray, CStringArray>*pArrayOfString6)
{
	try
	{
		CArray<CStringArray, CStringArray>*parray[6] =
		{
			pArrayOfString1,
			pArrayOfString2,
			pArrayOfString3,
			pArrayOfString4,
			pArrayOfString5,
			pArrayOfString6
		};

		DWORD bytereads = 0;
		for (int i = 0; i < 6; i++)
		{
			parray[i]->RemoveAll();
			if (g_sendhead.length[i] == 0)continue;
			if (g_sendhead.code[i])
			{
				BYTE *lpszOut = NULL;
				int nOutSize = 0;
				LZARI Lzari;
#ifdef VC60
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], lpszOut, nOutSize);
#else
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], (const BYTE*&)lpszOut, nOutSize);
#endif
				CMemFile memfile;
				memfile.Attach(lpszOut, nOutSize);

				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}

				ar.Close();
				Lzari.Release();
				memfile.Detach();
			}
			else
			{
				CMemFile memfile;
				memfile.Attach(g_pData + bytereads, g_sendhead.length[i]);
				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}
				ar.Close();
				memfile.Detach();
			}
		}
	}
	catch (...)
	{
	}
}

void DataToArray(
				 CArray<CStringArray, CStringArray>*pArrayOfString1,
				 CArray<CStringArray, CStringArray>*pArrayOfString2,
				 CArray<CStringArray, CStringArray>*pArrayOfString3,
				 CArray<CStringArray, CStringArray>*pArrayOfString4,
				 CArray<CStringArray, CStringArray>*pArrayOfString5,
				 CArray<CStringArray, CStringArray>*pArrayOfString6,
				 CArray<CStringArray, CStringArray>*pArrayOfString7)
{
	try
	{
		CArray<CStringArray, CStringArray>*parray[7] =
		{
			pArrayOfString1,
			pArrayOfString2,
			pArrayOfString3,
			pArrayOfString4,
			pArrayOfString5,
			pArrayOfString6,
			pArrayOfString7
		};
		DWORD bytereads = 0;
		for (int i = 0; i < 7; i++)
		{
			parray[i]->RemoveAll();
			if (g_sendhead.length[i] == 0)continue;
			if (g_sendhead.code[i])
			{
				BYTE *lpszOut = NULL;
				int nOutSize = 0;
				LZARI Lzari;
#ifdef VC60
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], lpszOut, nOutSize);
#else
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], (const BYTE*&)lpszOut, nOutSize);
#endif
				CMemFile memfile;
				memfile.Attach(lpszOut, nOutSize);

				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}

				ar.Close();
				Lzari.Release();
				memfile.Detach();
			}
			else
			{
				CMemFile memfile;
				memfile.Attach(g_pData + bytereads, g_sendhead.length[i]);
				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}
				ar.Close();
				memfile.Detach();
			}
		}
	}
	catch (...)
	{
	}
}

void DataToArray(
				 CArray<CStringArray, CStringArray>*pArrayOfString1,
				 CArray<CStringArray, CStringArray>*pArrayOfString2,
				 CArray<CStringArray, CStringArray>*pArrayOfString3,
				 CArray<CStringArray, CStringArray>*pArrayOfString4,
				 CArray<CStringArray, CStringArray>*pArrayOfString5,
				 CArray<CStringArray, CStringArray>*pArrayOfString6,
				 CArray<CStringArray, CStringArray>*pArrayOfString7,
				 CArray<CStringArray, CStringArray>*pArrayOfString8)
{
	try
	{
		CArray<CStringArray, CStringArray>*parray[8] =
		{
			pArrayOfString1,
			pArrayOfString2,
			pArrayOfString3,
			pArrayOfString4,
			pArrayOfString5,
			pArrayOfString6,
			pArrayOfString7,
			pArrayOfString8
		};

		DWORD bytereads = 0;
		for (int i = 0; i < 8; i++)
		{
			parray[i]->RemoveAll();
			if (g_sendhead.length[i] == 0)continue;
			if (g_sendhead.code[i])
			{
				BYTE *lpszOut = NULL;
				int nOutSize = 0;
				LZARI Lzari;
#ifdef VC60
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], lpszOut, nOutSize);
#else
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], (const BYTE*&)lpszOut, nOutSize);
#endif
				CMemFile memfile;
				memfile.Attach(lpszOut, nOutSize);

				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}

				ar.Close();
				Lzari.Release();
				memfile.Detach();
			}
			else
			{
				CMemFile memfile;
				memfile.Attach(g_pData + bytereads, g_sendhead.length[i]);
				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}
				ar.Close();
				memfile.Detach();
			}
		}
	}
	catch (...)
	{
	}
}

void DataToArray(
				 CArray<CStringArray, CStringArray>*pArrayOfString1,
				 CArray<CStringArray, CStringArray>*pArrayOfString2,
				 CArray<CStringArray, CStringArray>*pArrayOfString3,
				 CArray<CStringArray, CStringArray>*pArrayOfString4,
				 CArray<CStringArray, CStringArray>*pArrayOfString5,
				 CArray<CStringArray, CStringArray>*pArrayOfString6,
				 CArray<CStringArray, CStringArray>*pArrayOfString7,
				 CArray<CStringArray, CStringArray>*pArrayOfString8,
				 CArray<CStringArray, CStringArray>*pArrayOfString9,
				 CArray<CStringArray, CStringArray>*pArrayOfString10)
{
	try
	{
		CArray<CStringArray, CStringArray>*parray[10] =
		{
			pArrayOfString1,
			pArrayOfString2,
			pArrayOfString3,
			pArrayOfString4,
			pArrayOfString5,
			pArrayOfString6,
			pArrayOfString7,
			pArrayOfString8,
			pArrayOfString9,
			pArrayOfString10
		};
		DWORD bytereads = 0;
		for (int i = 0; i < 10; i++)
		{
			parray[i]->RemoveAll();
			if (g_sendhead.length[i] == 0)continue;
			if (g_sendhead.code[i])
			{
				BYTE *lpszOut = NULL;
				int nOutSize = 0;
				LZARI Lzari;
#ifdef VC60
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], lpszOut, nOutSize);
#else
				Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], (const BYTE*&)lpszOut, nOutSize);
#endif
				CMemFile memfile;
				memfile.Attach(lpszOut, nOutSize);

				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}

				ar.Close();
				Lzari.Release();
				memfile.Detach();
			}
			else
			{
				CMemFile memfile;
				memfile.Attach(g_pData + bytereads, g_sendhead.length[i]);
				bytereads += g_sendhead.length[i];
				CArchive ar(&memfile, CArchive::load);
				parray[i]->SetSize(g_sendhead.count[i]);
				for (int ii = 0; ii < parray[i]->GetSize(); ii++)
				{
					parray[i]->ElementAt(ii).Serialize(ar);
				}
				ar.Close();
				memfile.Detach();
			}
		}
	}
	catch (...)
	{
	}
}

BOOL IsHasRights(int pos)
{return 1;
if(g_user.rights.GetLength ()>pos)
{
	BOOL ret=atoi(g_user.rights.Mid (pos,1));
	if(ret==0)AfxMessageBox("û�����Ȩ��!", MB_ICONINFORMATION);
	return ret;
}
AfxMessageBox("û�����Ȩ��!", MB_ICONINFORMATION);
return 0;
}

BOOL IsHasRights2(int pos)
{return 1;
if(g_user.rights.GetLength ()>pos)
{
	BOOL ret=atoi(g_user.rights.Mid (pos,1));
	return ret;
}
return 0;
}
BOOL IsHasRightsnew(int pos)
{
	if(g_user.rights.GetLength ()>pos)
	{
		BOOL ret=atoi(g_user.rights.Mid (pos,1));
		if(ret==0)AfxMessageBox("û�����Ȩ��!", MB_ICONINFORMATION);
		return ret;
	}
	AfxMessageBox("û�����Ȩ��!", MB_ICONINFORMATION);
	return 0;
}

BOOL IsHasRights2new(int pos)
{
	if(g_user.rights.GetLength ()>pos)
	{
		BOOL ret=atoi(g_user.rights.Mid (pos,1));
		return ret;
	}
	return 0;
}

int CLYFZIPManageApp::ExitInstance() 
{
	g_db.Close ();
	return CWinApp::ExitInstance();
}
/********************************************************************************************
/* Syntax
/*        void FirstLetter(int nCode, CString& strLetter)
/* Remarks:
/*        Get the first letter of pinyin according to specified Chinese character code.
/* Parameters:
/*        nCode         - the code of the chinese character.
/*        strLetter      - a CString object that is to receive the string of the first letter.
/* Return Values:
/*        None.
/* Author:
/*        lixiaosan
/* Create Date:
/*        05-26-2006
/********************************************************************************************/
void FirstLetter(int nCode, CString& strLetter)
{
	if(nCode >= 1601 && nCode < 1637) strLetter = _T("A");
	if(nCode >= 1637 && nCode < 1833) strLetter = _T("B");
	if(nCode >= 1833 && nCode < 2078) strLetter = _T("C");
	if(nCode >= 2078 && nCode < 2274) strLetter = _T("D");
	if(nCode >= 2274 && nCode < 2302) strLetter = _T("E");
	if(nCode >= 2302 && nCode < 2433) strLetter = _T("F");
	if(nCode >= 2433 && nCode < 2594) strLetter = _T("G");
	if(nCode >= 2594 && nCode < 2787) strLetter = _T("H");
	if(nCode >= 2787 && nCode < 3106) strLetter = _T("J");
	if(nCode >= 3106 && nCode < 3212) strLetter = _T("K");
	if(nCode >= 3212 && nCode < 3472) strLetter = _T("L");
	if(nCode >= 3472 && nCode < 3635) strLetter = _T("M");
	if(nCode >= 3635 && nCode < 3722) strLetter = _T("N");
	if(nCode >= 3722 && nCode < 3730) strLetter = _T("O");
	if(nCode >= 3730 && nCode < 3858) strLetter = _T("P");
	if(nCode >= 3858 && nCode < 4027) strLetter = _T("Q");
	if(nCode >= 4027 && nCode < 4086) strLetter = _T("R");
	if(nCode >= 4086 && nCode < 4390) strLetter = _T("S");
	if(nCode >= 4390 && nCode < 4558) strLetter = _T("T");
	if(nCode >= 4558 && nCode < 4684) strLetter = _T("W");
	if(nCode >= 4684 && nCode < 4925) strLetter = _T("X");
	if(nCode >= 4925 && nCode < 5249) strLetter = _T("Y");
	if(nCode >= 5249 && nCode < 5590) strLetter = _T("Z");
}
/********************************************************************************************
/* Syntax
/*        GetFirstLetter(CString strName, CString& strFirstLetter)
/* Remarks:
/*        Get the first letter of pinyin according to specified Chinese character.
/* Parameters:
/*        strName            - a CString object that is to be parsed.
/*        strFirstLetter     - a CString object that is to receive the string of the first letter.
/* Return Values:
/*        None.
/* Author:
/*        lixiaosan
/* Create Date:
/*        05-26-2006
/********************************************************************************************/
void GetFirstLetter(CString strName, CString& strFirstLetter)
{
	TBYTE ucHigh, ucLow;
	int  nCode;
	CString strRet;

	strFirstLetter.Empty();

	for (int i=0; i<strName.GetLength(); i++)
	{
		if ( (TBYTE)strName[i] < 0x80 )
		{
			strRet=strName.Mid (i,1);
			strRet.MakeUpper ();
			strFirstLetter += strRet;
			continue;
		}

		ucHigh = (TBYTE)strName[i];
		ucLow  = (TBYTE)strName[i+1];
		if ( ucHigh < 0xa1 || ucLow < 0xa1)
			continue;
		else
			// Treat code by section-position as an int type parameter,
			// so make following change to nCode.
			nCode = (ucHigh - 0xa0) * 100 + ucLow - 0xa0;

		FirstLetter(nCode, strRet);
		strFirstLetter += strRet;
		i++;
	}
}
void LoadImageFromBuf(Bitmap **img, BYTE *pData, DWORD leng)
{
	try
	{
		if(pData==NULL)return;
		if(*img)delete *img;
		*img=NULL;
		// Allocate global memory on which to create stream
		HGLOBAL m_hMem = GlobalAlloc(GMEM_MOVEABLE, leng);
		if(m_hMem==NULL)return;
		BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
		memcpy(pmem,pData,leng);
		IStream* pstm;
		CreateStreamOnHGlobal(m_hMem,TRUE,&pstm);
		// load from stream
		*img=Gdiplus::Bitmap::FromStream(pstm);
		// free/release stuff
		GlobalUnlock(m_hMem);
		pstm->Release();
	}
	catch(...)
	{
	}
}
void LoadImageFromBuf(Image **img, BYTE *pData, DWORD leng)
{
	try
	{
		if(pData==NULL)return;
		if(*img)delete *img;
		*img=NULL;
		// Allocate global memory on which to create stream
		HGLOBAL m_hMem = GlobalAlloc(GMEM_MOVEABLE, leng);
		if(m_hMem==NULL)return;
		BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
		memcpy(pmem,pData,leng);
		IStream* pstm;
		CreateStreamOnHGlobal(m_hMem,TRUE,&pstm);
		// load from stream
		*img=Gdiplus::Image::FromStream(pstm);
		// free/release stuff
		GlobalUnlock(m_hMem);
		pstm->Release();
	}
	catch(...)
	{
	}
}

void LoadImageFromBuf(Image **img, CString path)
{
	try
	{
		CFile fp;
		if(fp.Open (path, CFile::modeRead))
		{
			DWORD leng=fp.GetLength ();
			BYTE *pData=new BYTE[leng];
			fp.Read (pData, leng);
			fp.Close ();
			LoadImageFromBuf(img, pData, leng);
			delete []pData;
		}
	}
	catch(...)
	{
	}
}
void LoadImageFromBuf(Bitmap **img, CString path)
{
	try
	{
		CFile fp;
		if(fp.Open (path, CFile::modeRead))
		{
			DWORD leng=fp.GetLength ();
			BYTE *pData=new BYTE[leng];
			fp.Read (pData, leng);
			fp.Close ();
			LoadImageFromBuf(img, pData, leng);
			delete []pData;
		}
	}
	catch(...)
	{
	}
}
int GetType(CString str)
{
	BOOL bDigit=1;
	BOOL bAlpha=1;
	for(int i=0; i<str.GetLength (); i++)
	{
		//	if(!::isdigit (str.GetAt (i)))
		if(! ((str.GetAt (i)<=0 && str.GetAt (i)>=9) ))
			bDigit=0;
		if(! ((str.GetAt (i)<='z' && str.GetAt (i)>='a') || (str.GetAt (i)<='Z' && str.GetAt (i)>='A')))
			//else if(!::isalpha ( ((unsigned char)str.GetAt (i))))
			bAlpha=0;
		if(bDigit==0 && bAlpha==0)return 0;
	}
	if(bDigit)
	{
		return 1;
	}
	else if(bAlpha)
	{
		return 2;
	}
	else
		return 0;
}
void LoadImageFromRes(Image **img, LPCTSTR lpszResourceName, LPCTSTR ResourceType)
{
	try
	{
		HGLOBAL  hGlobal = NULL;
		HRSRC  hSource = NULL;
		LPVOID  lpVoid  = NULL;
		int   nSize   = 0;
		BOOL  bResult=FALSE;
		hSource = FindResource(NULL, lpszResourceName, ResourceType);
		if(hSource == NULL)return;
		hGlobal = LoadResource(NULL, hSource);
		if(hGlobal == NULL)return;
		lpVoid = LockResource(hGlobal);
		if(lpVoid == NULL)return;

		nSize = (UINT)SizeofResource(NULL, hSource);
		LoadImageFromBuf(img, (BYTE*)hGlobal, nSize);
		UnlockResource(hGlobal); // 16Bit Windows Needs This
		FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
	}
	catch(...)
	{
	}
}
CString FilterBZ(CString &bz)
{
	bz.Replace ("'", "");
	/*	int i=bz.GetLength ()-1;
	char c;
	for(;i>=0;i--)
	{
	c=bz.GetAt (i);
	if(c<32)// && c!=10 && c!=13)
	bz.Delete (i);
	}*/
	if(bz.GetLength ()>1023)
	{
		AfxMessageBox("����̫��, �ַ���������ȡ!", MB_ICONSTOP);
		bz=bz.Left (1023);
	}
	return bz;
}

BOOL GIsHalfHZ(const   CString   &str)   
{
	int   len=str.GetLength();   
	BOOL   IsHalf=FALSE;   
	for(int   i=0;i<len;i++)   
	{   
		if(str.GetAt(i)   <   0)   IsHalf=!IsHalf;   
	}
	return   IsHalf;   
}
void RectFitDes(int width, int height, CRect &rc)
{
	try
	{
		if(width==0 || height==0)return;
		float fscale=(float)width/(float)height;
		float rcscale=((float)rc.Width ())/((float)rc.Height ());
		int rcwid=rc.Width ();
		int rchei=rc.Height ();
		int dt=0;
		if(rcscale<fscale)
		{
			dt=(rchei-rcwid/fscale)/2;
			rc.top+=dt;
			rc.bottom-=dt;
		}
		else
		{
			dt=(rcwid-rchei*fscale)/2;
			rc.left +=dt;
			rc.right-=dt;
		}
	}
	catch(...)
	{
	}
}
int GetPos(DWORD leng)
{
	if(g_lengarray.GetSize()==0)
	{
		g_sendhead.bsql=0;
		g_sendhead.code[0]=105;
		g_sendhead.tabcount=1;
		g_pMainWnd->ProcessChatMessageRequest2(1);if(g_bSendOK==0)return -1;

		DWORD leng;
		DWORD readbytes=0;
		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);
		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);


			/*		RESDATA2 *pRes=(RESDATA2*)pData;
			CString str;
			str.Format ("%d", pRes->nPicBmpCount [0]);
			AfxMessageBox(str);
			str.Format ("%d", pRes->nPicBmpCount [1]);
			AfxMessageBox(str);
			str.Format ("%d", pRes->nPicBmpCount [2]);
			AfxMessageBox(str);
			str.Format ("%d", pRes->nPicBmpCount [3]);
			AfxMessageBox(str);
			str.Format ("%d", pRes->nPicBmpCount [4]);
			AfxMessageBox(str);
			str.Format ("%d", pRes->nPicBmpCount [5]);
			AfxMessageBox(str);*/

		}
	}
	for(int i=0; i<g_lengarray.GetSize (); i++)
	{
		if(leng==g_lengarray.ElementAt (i))
		{
			return i;
		}
	}
	return -1;
}
extern RESDATA2 g_resdata2;

BOOL LoadLia(CString path, BYTE **pData, BYTE **pDataBegin, Image **pBk, HWND hwnd)
{

	return 0;
}
BOOL CheckDateOK(CString str)
{
	if(str.IsEmpty ())return 1;
	CString tip;
	int i;
	CString demo=CTime::GetCurrentTime ().Format ("%Y-%m-%d");
	if(str.GetLength ()!=demo.GetLength ())
		goto ll;
	//2009-01-01
	if(str.GetAt (4)!='-')
		goto ll;
	if(str.GetAt (7)!='-')
		goto ll;
	for( i=0; i<str.GetLength (); i++)
	{
		if(i==4 || i==7)continue;
		if(str.GetAt (i)<'0' || str.GetAt (i)>'9')
			goto ll;
	}
	return 1;
ll:
	tip.Format ("����:%s��ʽ����!\r\n����:%s", str, demo);
	AfxMessageBox(tip, MB_ICONSTOP);
	return 0;
}
int GetOrientation(Image *image)
{
	if(image )
	{
		UINT totalBufferSize;
		UINT numProperties;
		image->GetPropertySize(&totalBufferSize, &numProperties);
		// Allocate the buffer that will receive the property items.
		PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
		// Fill the buffer.
		image->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
		// Print the id data member of each property item.
		for(UINT j = 0; j < numProperties; ++j)
		{
			if(PropertyTagOrientation==pAllItems[j].id)
			{
				short* ptrLong = (short*)(pAllItems[j].value);
				int ret=(int)*ptrLong;
				free(pAllItems);
				return ret;
				//str.Format("%d", *ptrLong);
				//MessageBox(str);
			}

		}
		free(pAllItems);
	}
	return 1;
}

int CLYFZIPManageApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt) 
{
	// TODO: Add your specialized code here and/or call the base class
	return CWinApp::DoMessageBox(lpszPrompt, nType, nIDPrompt);
}
void WriteLogin(CString str)
{
	CStdioFile fp;
	CString path=g_mainpath+"\\����ƽ̨��־.txt";
	if(::PathFileExists (path))
	{
		fp.Open (path, CFile::modeWrite);
		int length=fp.GetLength ();
		if(length>1024*1024)
		{
			fp.Close ();
			::DeleteFile (path);
			return;
		}
		fp.SeekToEnd ();
	}
	else
		fp.Open (path, CFile::modeCreate|CFile::modeWrite);
	fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S")+" "+str+"\r\n" );
	fp.Close ();
}
void WriteLogin2(CString str)
{
	CStdioFile fp;
	CString path=g_mainpath+"\\����ƽ̨��־2.txt";
	if(::PathFileExists (path))
	{
		fp.Open (path, CFile::modeWrite);
		int length=fp.GetLength ();
		if(length>1024*1024)
		{
			fp.Close ();
			::DeleteFile (path);
			return;
		}
		fp.SeekToEnd ();
	}
	else
		fp.Open (path, CFile::modeCreate|CFile::modeWrite);
	fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S")+" "+str+"\r\n" );
	fp.Close ();
}

BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
{
	try
	{
		HCRYPTPROV hCryptProv;
		HCRYPTKEY hKey; 
		HCRYPTHASH hHash; 

		PBYTE pbBuffer; 
		DWORD dwBlockLen; 
		DWORD dwBufferLen; 
		DWORD dwCount; 


		//���»��һ��CSP���
		if(CryptAcquireContext(
			&hCryptProv, 
			NULL,				//NULL��ʾʹ��Ĭ����Կ������Ĭ����Կ������Ϊ�û���½��
			NULL, 
			PROV_RSA_FULL, 
			0))
		{
			printf("A cryptographic provider has been acquired. \n");
		}
		else//��Կ����������
		{
			if(CryptAcquireContext(
				&hCryptProv, 
				NULL, 
				NULL, 
				PROV_RSA_FULL, 
				CRYPT_NEWKEYSET))//������Կ����
			{
				//������Կ�����ɹ������õ�CSP���
				printf("A new key container has been created.\n");
			}
			else
			{
				return 0;
			}

		}

		//--------------------------------------------------------------------
		// ����һ���Ự��Կ��session key��
		// �Ự��ԿҲ�жԳ���Կ�����ڶԳƼ����㷨��
		// ��ע: һ��Session��ָ�ӵ��ú���CryptAcquireContext�����ú���
		//   CryptReleaseContext �ڼ�Ľ׶Ρ���

		//--------------------------------------------------------------------
		// Create a hash object. 
		if(CryptCreateHash(
			hCryptProv, 
			CALG_MD5, 
			0, 
			0, 
			&hHash))
		{
			printf("A hash object has been created. \n");
		}
		else
		{ 
			return 0;
		}  

		//--------------------------------------------------------------------
		// ��������������һ��ɢ��
		if(CryptHashData(
			hHash, 
			(BYTE *)szPassword, 
			strlen(szPassword), 
			0))
		{
			printf("The password has been added to the hash. \n");
		}
		else
		{
			return 0;
		}

		//--------------------------------------------------------------------
		// ͨ��ɢ�����ɻỰ��Կ(session key)
		if(CryptDeriveKey(
			hCryptProv, 
			ENCRYPT_ALGORITHM, 
			hHash, 
			KEYLENGTH, 
			&hKey))
		{
			printf("An encryption key is derived from the password hash. \n"); 
		}
		else
		{
			return 0;
		}
		//--------------------------------------------------------------------
		// Destroy the hash object. 

		CryptDestroyHash(hHash); 
		hHash = NULL; 

		//--------------------------------------------------------------------
		//  The session key is now ready. 

		//--------------------------------------------------------------------
		// ��Ϊ�����㷨�ǰ�ENCRYPT_BLOCK_SIZE ��С�Ŀ���ܵģ����Ա����ܵ�
		// ���ݳ��ȱ�����ENCRYPT_BLOCK_SIZE �����������������һ�μ��ܵ�
		// ���ݳ��ȡ�

		dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; 

		//--------------------------------------------------------------------
		// Determine the block size. If a block cipher is used, 
		// it must have room for an extra block. 

		if(ENCRYPT_BLOCK_SIZE > 1) 
			dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE; 
		else 
			dwBufferLen = dwBlockLen; 
		dwCount=dwBufferLen;
		//--------------------------------------------------------------------
		// In a do loop, encrypt the source file and write to the source file. 
		int count;
		//	if(leng%dwBlockLen==0)
		count=leng/dwBufferLen;
		//	else
		//	count=leng/dwBlockLen+1;
		int i = 0;
		for( i=0; i<count; i++)
		{
			pbBuffer=buffer+i*dwBufferLen;
			// ��������
			if(!CryptEncrypt(
				hKey,			//��Կ
				0,				//�������ͬʱ����ɢ�кͼ��ܣ����ﴫ��һ��ɢ�ж���
				0,	//��������һ�������ܵĿ飬����TRUE.�����������FALSE.
				//����ͨ���ж��Ƿ��ļ�β�������Ƿ�Ϊ���һ�顣
				0,				//����
				pbBuffer,		//���뱻�������ݣ�������ܺ������
				&dwCount,		//���뱻��������ʵ�ʳ��ȣ�������ܺ����ݳ���
				dwBufferLen))	//pbBuffer�Ĵ�С��
			{
				return 0;
			}
		}
		if(leng%dwBlockLen)
		{
			pbBuffer=buffer+i*dwBufferLen;
			dwCount=leng-i*dwBufferLen;
			if(!CryptEncrypt(
				hKey,			//��Կ
				0,				//�������ͬʱ����ɢ�кͼ��ܣ����ﴫ��һ��ɢ�ж���
				TRUE,	//��������һ�������ܵĿ飬����TRUE.�����������FALSE.
				//����ͨ���ж��Ƿ��ļ�β�������Ƿ�Ϊ���һ�顣
				0,				//����
				pbBuffer,		//���뱻�������ݣ�������ܺ������
				&dwCount,		//���뱻��������ʵ�ʳ��ȣ�������ܺ����ݳ���
				dwBufferLen))	//pbBuffer�Ĵ�С��
			{ 
				return 0;
			} 
		}



		//--------------------------------------------------------------------
		// Destroy session key. 

		if(hKey) 
			CryptDestroyKey(hKey); 

		//--------------------------------------------------------------------
		// Destroy hash object. 

		if(hHash) 
			CryptDestroyHash(hHash); 

		//--------------------------------------------------------------------
		// Release provider handle. 

		if(hCryptProv) 
			CryptReleaseContext(hCryptProv, 0);
		return(TRUE); 
	}
	catch(...)
	{
	}
}