// stdafx.cpp : ֻ������׼�����ļ���Դ�ļ�
// AlipayTestCase.pch ����ΪԤ����ͷ
// stdafx.obj ������Ԥ����������Ϣ

#include "stdafx.h"

// dll���;
HMODULE g_hPayApi = NULL;
GetFailMsg					g_GetFailMsg				= NULL;
GetReturnMsg				g_GetReturnMsg				= NULL;
Alipay_SetCommonParam		g_Alipay_SetCommonParam		= NULL;
Alipay_PrecreateContent		g_Alipay_PrecreateContent	= NULL;
Alipay_PayContent			g_Alipay_PayContent			= NULL;
wxpay_SetCommParam			g_wxpay_SetCommParam		= NULL;
wxpay_PayContent			g_wxpay_PayContent			= NULL;

BOOL LoadPayApiLibrary( IN LPCTSTR lpLibrary /* = NULL */)
{
	if (g_hPayApi)
		return TRUE;

	if (lpLibrary == NULL || lpLibrary[0] == '\0')
	{
		g_hPayApi = LoadLibrary(_T("Alipay.dll"));
	}
	else
	{
		if (!PathFileExists(lpLibrary))
		{
			AfxMessageBox(_T("���ض�̬��ʧ��"));
			return FALSE;
		}
		g_hPayApi = LoadLibrary(lpLibrary);
	}

	if (g_hPayApi == NULL)
	{
		DWORD dwError = GetLastError();
		WriteTextLog(_T("����ʧ�ܴ����룺%d"), dwError);
		return FALSE;
	}

	g_GetFailMsg = (GetFailMsg)GetProcAddress(g_hPayApi, "GetFailMsg");
	g_GetReturnMsg = (GetReturnMsg)GetProcAddress(g_hPayApi, "GetReturnMsg");
	g_Alipay_SetCommonParam = (Alipay_SetCommonParam)GetProcAddress(g_hPayApi, "Alipay_SetCommonParam");
	g_Alipay_PrecreateContent = (Alipay_PrecreateContent)GetProcAddress(g_hPayApi, "Alipay_PrecreateContent");
	g_Alipay_PayContent = (Alipay_PayContent)GetProcAddress(g_hPayApi, "Alipay_PayContent");
	g_wxpay_SetCommParam = (wxpay_SetCommParam)GetProcAddress(g_hPayApi, "wxpay_SetCommParam");
	g_wxpay_PayContent = (wxpay_PayContent)GetProcAddress(g_hPayApi, "wxpay_PayContent");

	return TRUE;
}


void FreePayApiLibrary()
{
	if (g_hPayApi)
	{
		if (FreeLibrary(g_hPayApi))
		{
			g_GetFailMsg = NULL;
			g_GetReturnMsg = NULL;
			g_Alipay_SetCommonParam = NULL;
			g_Alipay_PrecreateContent = NULL;
			g_Alipay_PayContent = NULL;
			g_wxpay_SetCommParam = NULL;
			g_wxpay_PayContent = NULL;

			g_hPayApi = NULL;
		}
	}	
}


/************************************************************************/
/*  ������WriteTextLog[7/28/2016 IT];
/*  ������д�ı���־;
/*  ������;
/*  	[IN] ��;
/*  ���أ�void;
/*  ע�⣺;
/*  ʾ����;
/*
/*  �޸ģ�;
/*  ���ڣ�;
/*  ���ݣ�;
/************************************************************************/
void WriteTextLog(const TCHAR *format, ...)
{
	try
	{
		// ��������־·��;
		static TCHAR szlogpath[MAX_PATH] = { 0 };
		if (szlogpath[0] == _T('\0'))
		{
			TCHAR szDrive[_MAX_DRIVE] = { 0 };
			TCHAR szDir[_MAX_DIR] = { 0 };
			TCHAR szFna[_MAX_DIR] = { 0 };
			TCHAR szExt[_MAX_DIR] = { 0 };
			TCHAR szModulePath[MAX_PATH] = { 0 };
			::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
			_tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
			_tcscpy_s(szModulePath, szDrive);
			_tcscat_s(szModulePath, szDir);
			_stprintf_s(szlogpath, _T("%s\\lyfzPayApi.txt"), szModulePath);
		}

		// �򿪻򴴽��ļ�;
		CStdioFile fp;
		if (PathFileExists(szlogpath))
		{
			if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
			{
				return;
			}

			ULONGLONG length = fp.GetLength();
			if (length > 10 * 1024 * 1024) // 10M;
			{
				fp.Close();
				::DeleteFile(szlogpath);
				return;
			}
			fp.SeekToEnd();
		}
		else
		{
			if (!fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite))
				return;
		}

		// ��ʽ��ǰ������������;
		TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
		_tsetlocale(LC_CTYPE, _T("chs"));//�趨����;

										 // ��ʽ����־����;
		va_list		args = NULL;
		int			len = 0;
		TCHAR		*buffer = NULL;
		va_start(args, format);
		if (args == NULL)
			return;
		// _vscprintf doesn't count. terminating '\0'
		len = _vsctprintf(format, args);
		if (len == -1)
		{
			goto clear;
		}

		len++;
		buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
		_vstprintf_s(buffer, len, format, args); // C4996
												 // Note: vsprintf is deprecated; consider using vsprintf_s instead

												 // ����־�������뵽�ļ���;
		fp.WriteString(CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")));
		fp.WriteString(buffer);
		fp.WriteString(_T("\n"));

		// �ر��ļ����ͷ���Դ�����û�ԭ��������;
		free(buffer);
	clear:
		_tsetlocale(LC_CTYPE, old_locale);
		free(old_locale);//��ԭ�����趨;
		fp.Close();
	}
	catch (CException *e)
	{
		e->ReportError();
		e->Delete();
	}
}