// 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(); } }