Commit 1f3dff76 by wuyang.zou

1、调用 FreemudWrapper.dll时 将动态库 依赖库的目录 设置到环境变量的开头;防止加载 SSL库出现失败;

2、将加载DLL时 环境变量相关日志输出到 C:\freemud\fmclient\log\FreemudWrapper.txt 中
3、本次修改后的FreemudWrapper.dll 版本号是:1.0.1.1
parent dd07b17b
No preview for this file type
......@@ -2,15 +2,24 @@
#include <Windows.h>
#include <Winbase.h>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <fstream>
#include "NetDataLog.h"
#pragma comment(lib, "Kernel32.lib")
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
/*C++标准string流*/
std::stringstream ss;
/*缓存Buff:4*1024=4K * 2 [2 Memory page] */
const int BUFF_SIZE = 1024*8;
#define SBKPAYDLL "sbkpay.dll"
#define SBKPAYCONFIG "sbkpay.ini"
#define FMPDLL "fmclient.dll"
//#define FMCRYPT "fmcrypt.dll"
#define LIBCURL "libcurl.dll"
......@@ -19,7 +28,6 @@ EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define SSLEAY32 "ssleay32.dll"
#define ZLIB1 "zlib1.dll"
#define SQLITE "sqlite3.dll"
#define FMPDLLR "fmclientR.dll"
//#define FMCRYPTR "fmcryptR.dll"
#define LIBCURLR "libcurlR.dll"
......@@ -30,8 +38,8 @@ EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define SQLITER "sqlite3R.dll"
#define SBKPAYDLLR "sbkpayR.dll"
void DoCopyFile(const char *dir);
void replace_all(std::string& str, const std::string& from, const std::string& to);
EXTERN_C typedef void (_stdcall *PFN_START)(char *, char *);
EXTERN_C typedef int (_stdcall *PFN_GETVALUE)(char *, char *);
......@@ -40,11 +48,15 @@ EXTERN_C typedef int (_stdcall *PFN_GETRSACRET)(int ,const char *,const char *);
int init_sign = 0;
//! 初始化日志对象;
NetDataLog netDataLog("\C:\\Freemud\\fmclient\\log","FreemudWrapperDll",1024*2,40,0);
HMODULE GetInstance()
{
//! 初始化变量
char current_dir[MAX_PATH] = { 0 };
char chBuf[10240]={0};
char chBuf[BUFF_SIZE]={0};
HMODULE hMod = NULL;
char mod[MAX_PATH] = { 0 };
GetModuleFileNameA((HMODULE)&__ImageBase, mod, MAX_PATH);
char *ch = strrchr(mod,'\\');
......@@ -55,33 +67,83 @@ HMODULE GetInstance()
if(init_sign == 0)
DoCopyFile(mod);
init_sign = 1;
DWORD dwSize =GetEnvironmentVariable("path", chBuf, 10240);
std::string currpath(chBuf);
char dllPath[MAX_PATH] = { 0 };
char conFilePath[MAX_PATH] = { 0 };
sprintf(conFilePath, "%s%s", mod, SBKPAYCONFIG);
printf("SbkPayWrapper: Set config file path: %s\n", conFilePath);
GetPrivateProfileString("ALL", "DLLPATH", mod, dllPath, MAX_PATH, conFilePath);
printf("SbkPayWrapper: Get dll path: %s\n", dllPath);
currpath += (std::string(";") + std::string(mod)) + std::string(";") + std::string(dllPath);
char instance[MAX_PATH] = { 0 };
sprintf(instance, "%s%s", mod, SBKPAYDLL);
SetEnvironmentVariable("path", currpath.data());
HMODULE hMod = LoadLibraryA(instance);
//! 获取当前环境变量[path]路径
std::string dstEnvPath;
DWORD dwSize =GetEnvironmentVariable("path", chBuf, BUFF_SIZE);
if ( dwSize > 0 && dwSize > BUFF_SIZE) {
char *tmp = new char[dwSize + 1];
ZeroMemory(tmp, dwSize + 1);
dwSize = GetEnvironmentVariable("path", tmp, dwSize);
dstEnvPath = tmp;
delete []tmp;
} else if (dwSize > 0 && dwSize <= BUFF_SIZE) {
dstEnvPath = chBuf;
}
//! 校验当前环境变量:case<1>: 当前环境变量不为空: 将DLL动态库添加到当前的环境变量[path]中;
if (!dstEnvPath.empty()) {
ss.str("");
ss << "Current path env: " << dstEnvPath.c_str() << std::endl;
OutputDebugString(ss.str().c_str());
netDataLog.addLog(ss.str().c_str());
//获取配置文件中Dll 配置DLL 路径;
char dllPath[MAX_PATH] = { 0 };
char conFilePath[MAX_PATH] = { 0 };
sprintf(conFilePath, "%s%s", mod, SBKPAYCONFIG);
printf("SbkPayWrapper: Set config file path: %s\n", conFilePath);
GetPrivateProfileString("ALL", "DLLPATH", mod, dllPath, MAX_PATH, conFilePath);
printf("SbkPayWrapper: Get dll path: %s\n", dllPath);
ss.str("");
ss <<"FreemudWrapper GetInstance():: mod path: "<< mod <<" ; dllConfigPath: "<<conFilePath <<" ; DLLPATH: "<<dllPath<< std::endl;
netDataLog.addLog(ss.str().c_str());
//! Remove duplicated module path
std::string mod_image_path = mod;
std::string mod_config_path = dllPath;
replace_all(mod_config_path, "\\\\", "\\" );
replace_all(dstEnvPath, mod_image_path, "");
replace_all(dstEnvPath, ";;", "");
replace_all(dstEnvPath, mod_config_path, "");
replace_all(dstEnvPath, ";;", "");
//! Append moudle path
// 优先设置fmClient目录下dll库到环境变量的开头;
std::string dstEnvNewPath = "";
if ( strcmp( mod_image_path.c_str(), mod_config_path.c_str() ) == 0 ) {
dstEnvNewPath = mod_image_path ;
dstEnvNewPath += ";";
dstEnvNewPath += dstEnvPath;
} else {
dstEnvNewPath = mod_image_path;
dstEnvNewPath += ";";
dstEnvNewPath += mod_config_path;
dstEnvNewPath += ";";
dstEnvNewPath += dstEnvPath;
}
int setEnvRet = SetEnvironmentVariable("path", dstEnvNewPath.c_str());
ss.str("");
ss << "New path env: " << " setEnvRet: "<<setEnvRet << " path env: "<< dstEnvNewPath << std::endl;
OutputDebugString(ss.str().c_str());
netDataLog.addLog(ss.str().c_str());
//! 获取DLL动态库的模块句柄;
char instance[MAX_PATH] = { 0 };
sprintf(instance, "%s%s", mod, SBKPAYDLL);
hMod = LoadLibraryA(instance); //[zwy] LoadLibraryA -> LoadLibrary
ss.str("");
ss << " LoadLibraryA(instance) hMod: " << hMod << std::endl;
netDataLog.addLog(ss.str().c_str());
}
//! 校验当前环境变量:case<2>: 当前环境变量为空;
else {
ss.str("");
ss << "Failed getting path env, returned value: " << dwSize << ", error: " << GetLastError() << std::endl;
OutputDebugString(ss.str().c_str());
netDataLog.addLog(ss.str().c_str());
}
//! 返回DLL动态库句柄;
return hMod;
// printf("Current directory: %s\nModule path: %s\n", current_dir, mod);
// printf("Changing current to: %s\n", mod);
// SetCurrentDirectory(mod);
// char instance[MAX_PATH] = { 0 };
// sprintf(instance, "%s%s", mod, SBKPAYDLL);
////if(init_sign == 0)
......@@ -91,10 +153,27 @@ HMODULE GetInstance()
////HMODULE hModw = LoadLibraryW(L"C:\\Users\\dingda.li\\Desktop\\sbkpay\\sbkpay.dll");
// printf("Changing current to: %s\n", current_dir);
// SetCurrentDirectory(current_dir);
// return hMod;
}
/*
* 函数名: replace_all
* 功能:将 第一个参数(str)对象中的字符串中出现第二个参数(from)的字符串,替换成第三个参数(to);
* 参数:1、待替换的原生字符串;2、需要被替换的字符串;3、替换成的目标字符串
* 返回:void;
* 备注:通过位移被替换的字符串长度 来避免重复替换的可能;
*/
void replace_all(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
void DoCopyFile(const char *dir)
{
char inskt[MAX_PATH] = { 0 };
......
No preview for this file type
......@@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FreemudWrapper", "FreemudWrapper.vcxproj", "{3AE5812C-93BD-444F-9766-DD042450BFF9}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FreemudWrapperLoader", "..\FreemudWrapperLoader\FreemudWrapperLoader.vcxproj", "{DEB441B8-8A5B-48E8-BA8E-44E50CF272A0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
......@@ -13,6 +15,10 @@ Global
{3AE5812C-93BD-444F-9766-DD042450BFF9}.Debug|Win32.Build.0 = Debug|Win32
{3AE5812C-93BD-444F-9766-DD042450BFF9}.Release|Win32.ActiveCfg = Release|Win32
{3AE5812C-93BD-444F-9766-DD042450BFF9}.Release|Win32.Build.0 = Release|Win32
{DEB441B8-8A5B-48E8-BA8E-44E50CF272A0}.Debug|Win32.ActiveCfg = Debug|Win32
{DEB441B8-8A5B-48E8-BA8E-44E50CF272A0}.Debug|Win32.Build.0 = Debug|Win32
{DEB441B8-8A5B-48E8-BA8E-44E50CF272A0}.Release|Win32.ActiveCfg = Release|Win32
{DEB441B8-8A5B-48E8-BA8E-44E50CF272A0}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
No preview for this file type
......@@ -38,16 +38,26 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IncludePath>$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>freemudwrapper.def</ModuleDefinitionFile>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
<ProjectReference>
<UseLibraryDependencyInputs>true</UseLibraryDependencyInputs>
</ProjectReference>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
......@@ -65,11 +75,13 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="FreemudWrapper.cpp" />
<ClCompile Include="NetDataLog.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="freemudwrapper.def" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="NetDataLog.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="resource1.h" />
</ItemGroup>
......
......@@ -18,6 +18,9 @@
<ClCompile Include="FreemudWrapper.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="NetDataLog.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="freemudwrapper.def">
......@@ -31,6 +34,9 @@
<ClInclude Include="resource1.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="NetDataLog.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="FreemudWrapper.rc">
......
#include "NetDataLog.h"
NetDataLog::NetDataLog(string strDir,string filename,int maxsize,int filecount,int timeFormat)
{
m_strDir = strDir;
m_LogFileName = m_strDir+string("\\")+filename;
m_MaxFileSize = maxsize;
m_FileCount = filecount;
m_timeFormat = timeFormat;
//判断日志目录是否存在
if(!checkFolderExist(m_strDir.c_str()))
{
_mkdir(m_strDir.c_str());
}
string strname = m_LogFileName+".txt";
if (!m_outputFile) {
m_outputFile = new fstream;
}
if (!m_outputFile->is_open()){
m_outputFile->open(strname,ofstream::out|ofstream::app); //打开日志文件
}
}
NetDataLog::~NetDataLog()
{
if(m_outputFile){
delete m_outputFile;
m_outputFile = NULL;
}
}
//********************************
//函数名:NetDataLog::checkFolderExist
//描 述:测试目录是否存在
//参 数:strPath 目录名
//返回值:存在返回真
//*************************************
bool NetDataLog::checkFolderExist( const string & strPath)
{
if(_access(strPath.data(),0) == 0)
return true;
else
return false;
}
//********************************
//函数名:NetDataLog::addLog
//描 述:向文件中添加日志信息
//参 数 log 为信息内容
//返回值:void
//*************************************
void NetDataLog::addLog(string log)
{
string currentTime = getCurrentTime(); //获取本地时间
if(m_timeFormat == NETLOG)
*m_outputFile<<"["<<currentTime<<"] "<<log<<endl;
else
*m_outputFile<<currentTime<<" "<<log<<endl;
//判断文件大小
fileSizeLimit();
}
//********************************
//函数名:NetDataLog::fileSizeLimit
//描 述:判断文件大小是否达到最大值
//参 数:无
//返回值:void
//*************************************
void NetDataLog::fileSizeLimit()
{
int filesize = getCurrentLogFileSize();
if(filesize>=m_MaxFileSize*1024)
fileOffset();
}
//********************************
//函数名:NetDataLog::fileOffset
//描 述:实现文件名的偏移
//参 数:无
//返回值:void
//*************************************
void NetDataLog::fileOffset()
{
m_outputFile->close(); //关闭当前文件
char filename[100]={0};
char newfilename[100] = {0};
for(int i = m_FileCount-1;i > 0;i--)
{
memset(filename,0,100);
sprintf(filename,"%s%d.txt",m_LogFileName.data(),i);
if(checkFolderExist(filename)) //存在
{
if(i == m_FileCount-1)
{
remove(filename);//删除文件
continue;
}
//文件名序号向后偏移
memset(newfilename,0,100);
sprintf(newfilename,"%s%d.txt",m_LogFileName.data(),i+1);
rename(filename,newfilename);
}
}
memset(filename,0,100);
sprintf(filename,"%s.txt",m_LogFileName.data());
sprintf(newfilename,"%s%d.txt",m_LogFileName.data(),1);
rename(filename,newfilename);
m_outputFile->open(filename,ofstream::out|ofstream::app); //打开日志文件
}
//********************************
//函数名:NetDataLog::getCurrentLogFileSize
//描 述:计算当前日记文件的大小
//参 数:无
//返回值:文件大小(KB)
//*************************************
int NetDataLog::getCurrentLogFileSize()
{
long long filepos = m_outputFile->tellp(); //保存当前文件位置
m_outputFile->seekp(0,ios_base::end); //移动到文件尾
long long filesize = m_outputFile->tellp();
m_outputFile->seekp(filepos,ios_base::beg); //恢复文件位置
return filesize/1024;
}
//获取文件名
string NetDataLog::getLogFileName()
{
return m_LogFileName+".txt";
}
//设置文件个数
void NetDataLog::setFileCount(int count)
{
m_FileCount = count;
}
//设置文件名
void NetDataLog::setFileName(string filename)
{
m_LogFileName = m_strDir+string("\\")+filename;
}
//设置文件大小
void NetDataLog::setMaxFileSize(int maxsize)
{
m_MaxFileSize = maxsize;
}
//********************************
//函数名:NetDataLog::getCurrentTime
//描 述:获取本地时间
//返回值:时间字符串
//*************************************
string NetDataLog::getCurrentTime()
{
time_t seconds = time(NULL); //获取时间
struct tm *p;
p = localtime(&seconds);//获取本地时间
char strTime[100] = {0};
if(m_timeFormat == NETLOG)
sprintf(strTime,"%d\\%d\\%d %d.%d.%d",1900+p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
else
sprintf(strTime,"%02d-%02d %02d:%02d:%02d",1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
return string(strTime);
}
\ No newline at end of file
#pragma once
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <direct.h>
#include <string>
#include <io.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
using namespace std;
enum TIMEFORMAT
{
NETLOG = 0, // [yyyy\mm\dd hh.MM.ss]
LOGINLOG=1, // mm-dd hh:MM:ss
};
class NetDataLog
{
public:
NetDataLog(string strDir = "log",string filename = "LOG",int maxfilesize=0,int filecount=0,int timeformat=0);
~NetDataLog();
void addLog(string log); //添加日志记录到日志文件
void fileSizeLimit(); //判断文件大小是否达到限定值
int getCurrentLogFileSize();//获取当前日志文件的大小
string getLogFileName(); //获取日志文件名称
void setMaxFileSize(int);//设置文件最大大小
void setFileName(string); //设置日志文件名
void setFileCount(int); //设置日志文件的个数
void setLogDir(string strDir); //设置日志文件目录
private:
void fileOffset(); //文件名称进行偏移
bool checkFolderExist(const string &strPath);
string getCurrentTime();
private:
string m_LogFileName; //文件名
int m_MaxFileSize; //文件大小
int m_FileCount; //文件个数
fstream *m_outputFile; //输出文件流
string m_strDir; //目录
int m_timeFormat;
};
\ No newline at end of file
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment