stdafx.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // stdafx.cpp : 只包括标准包含文件的源文件
  2. // AlipayTestCase.pch 将作为预编译头
  3. // stdafx.obj 将包含预编译类型信息
  4. #include "stdafx.h"
  5. // dll句柄;
  6. HMODULE g_hPayApi = NULL;
  7. GetFailMsg g_GetFailMsg = NULL;
  8. GetReturnMsg g_GetReturnMsg = NULL;
  9. Alipay_SetCommonParam g_Alipay_SetCommonParam = NULL;
  10. Alipay_PrecreateContent g_Alipay_PrecreateContent = NULL;
  11. Alipay_PayContent g_Alipay_PayContent = NULL;
  12. wxpay_SetCommParam g_wxpay_SetCommParam = NULL;
  13. wxpay_PayContent g_wxpay_PayContent = NULL;
  14. BOOL LoadPayApiLibrary( IN LPCTSTR lpLibrary /* = NULL */)
  15. {
  16. if (g_hPayApi)
  17. return TRUE;
  18. if (lpLibrary == NULL || lpLibrary[0] == '\0')
  19. {
  20. g_hPayApi = LoadLibrary(_T("Alipay.dll"));
  21. }
  22. else
  23. {
  24. if (!PathFileExists(lpLibrary))
  25. {
  26. AfxMessageBox(_T("加载动态库失败"));
  27. return FALSE;
  28. }
  29. g_hPayApi = LoadLibrary(lpLibrary);
  30. }
  31. if (g_hPayApi == NULL)
  32. {
  33. DWORD dwError = GetLastError();
  34. WriteTextLog(_T("加载失败错误码:%d"), dwError);
  35. return FALSE;
  36. }
  37. g_GetFailMsg = (GetFailMsg)GetProcAddress(g_hPayApi, "GetFailMsg");
  38. g_GetReturnMsg = (GetReturnMsg)GetProcAddress(g_hPayApi, "GetReturnMsg");
  39. g_Alipay_SetCommonParam = (Alipay_SetCommonParam)GetProcAddress(g_hPayApi, "Alipay_SetCommonParam");
  40. g_Alipay_PrecreateContent = (Alipay_PrecreateContent)GetProcAddress(g_hPayApi, "Alipay_PrecreateContent");
  41. g_Alipay_PayContent = (Alipay_PayContent)GetProcAddress(g_hPayApi, "Alipay_PayContent");
  42. g_wxpay_SetCommParam = (wxpay_SetCommParam)GetProcAddress(g_hPayApi, "wxpay_SetCommParam");
  43. g_wxpay_PayContent = (wxpay_PayContent)GetProcAddress(g_hPayApi, "wxpay_PayContent");
  44. return TRUE;
  45. }
  46. void FreePayApiLibrary()
  47. {
  48. if (g_hPayApi)
  49. {
  50. if (FreeLibrary(g_hPayApi))
  51. {
  52. g_GetFailMsg = NULL;
  53. g_GetReturnMsg = NULL;
  54. g_Alipay_SetCommonParam = NULL;
  55. g_Alipay_PrecreateContent = NULL;
  56. g_Alipay_PayContent = NULL;
  57. g_wxpay_SetCommParam = NULL;
  58. g_wxpay_PayContent = NULL;
  59. g_hPayApi = NULL;
  60. }
  61. }
  62. }
  63. /************************************************************************/
  64. /* 函数:WriteTextLog[7/28/2016 IT];
  65. /* 描述:写文本日志;
  66. /* 参数:;
  67. /* [IN] :;
  68. /* 返回:void;
  69. /* 注意:;
  70. /* 示例:;
  71. /*
  72. /* 修改:;
  73. /* 日期:;
  74. /* 内容:;
  75. /************************************************************************/
  76. void WriteTextLog(const TCHAR *format, ...)
  77. {
  78. try
  79. {
  80. // 解析出日志路径;
  81. static TCHAR szlogpath[MAX_PATH] = { 0 };
  82. if (szlogpath[0] == _T('\0'))
  83. {
  84. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  85. TCHAR szDir[_MAX_DIR] = { 0 };
  86. TCHAR szFna[_MAX_DIR] = { 0 };
  87. TCHAR szExt[_MAX_DIR] = { 0 };
  88. TCHAR szModulePath[MAX_PATH] = { 0 };
  89. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  90. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  91. _tcscpy_s(szModulePath, szDrive);
  92. _tcscat_s(szModulePath, szDir);
  93. _stprintf_s(szlogpath, _T("%s\\lyfzPayApi.txt"), szModulePath);
  94. }
  95. // 打开或创建文件;
  96. CStdioFile fp;
  97. if (PathFileExists(szlogpath))
  98. {
  99. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  100. {
  101. return;
  102. }
  103. ULONGLONG length = fp.GetLength();
  104. if (length > 10 * 1024 * 1024) // 10M;
  105. {
  106. fp.Close();
  107. ::DeleteFile(szlogpath);
  108. return;
  109. }
  110. fp.SeekToEnd();
  111. }
  112. else
  113. {
  114. if (!fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite))
  115. return;
  116. }
  117. // 格式化前设置语言区域;
  118. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  119. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  120. // 格式化日志内容;
  121. va_list args = NULL;
  122. int len = 0;
  123. TCHAR *buffer = NULL;
  124. va_start(args, format);
  125. if (args == NULL)
  126. return;
  127. // _vscprintf doesn't count. terminating '\0'
  128. len = _vsctprintf(format, args);
  129. if (len == -1)
  130. {
  131. goto clear;
  132. }
  133. len++;
  134. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  135. _vstprintf_s(buffer, len, format, args); // C4996
  136. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  137. // 将日志内容输入到文件中;
  138. fp.WriteString(CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")));
  139. fp.WriteString(buffer);
  140. fp.WriteString(_T("\n"));
  141. // 关闭文件,释放资源并设置回原语言区域;
  142. free(buffer);
  143. clear:
  144. _tsetlocale(LC_CTYPE, old_locale);
  145. free(old_locale);//还原区域设定;
  146. fp.Close();
  147. }
  148. catch (CException *e)
  149. {
  150. e->ReportError();
  151. e->Delete();
  152. }
  153. }