|  | @@ -0,0 +1,631 @@
 | 
	
		
			
				|  |  | +#include "stdafx.h"
 | 
	
		
			
				|  |  | +#include "Global.h"
 | 
	
		
			
				|  |  | +#include <SetupAPI.h>
 | 
	
		
			
				|  |  | +#include <InitGuid.h>
 | 
	
		
			
				|  |  | +#include <WinIoCtl.h>
 | 
	
		
			
				|  |  | +#include <direct.h>
 | 
	
		
			
				|  |  | +#include <fcntl.h>
 | 
	
		
			
				|  |  | +#include <share.h>
 | 
	
		
			
				|  |  | +#include <sys\stat.h>
 | 
	
		
			
				|  |  | +#pragma comment(lib, "SetupAPI.lib")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +namespace Global
 | 
	
		
			
				|  |  | +{
 | 
	
		
			
				|  |  | +	//////////////////////////////////////////////////////////////////////////
 | 
	
		
			
				|  |  | +	// 全局变量;
 | 
	
		
			
				|  |  | +	TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
 | 
	
		
			
				|  |  | +	TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
 | 
	
		
			
				|  |  | +	TCHAR g_szFna[MAX_PATH] = { 0 };
 | 
	
		
			
				|  |  | +	TCHAR g_szConfig[MAX_PATH] = { 0 };
 | 
	
		
			
				|  |  | +	TCHAR g_szLogPath[MAX_PATH] = { 0 };
 | 
	
		
			
				|  |  | +	TCHAR g_szPython27Dir[MAX_PATH] = {0};
 | 
	
		
			
				|  |  | +	TCHAR g_szTVPort[MAX_PATH] = {0};
 | 
	
		
			
				|  |  | +	TCHAR g_szTVReboot[MAX_PATH] = {0};
 | 
	
		
			
				|  |  | +	TCHAR g_szTVShutdown[MAX_PATH] = {0};
 | 
	
		
			
				|  |  | +	BOOL  g_bWatchTVPort = FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	bool g_bEnableLog = false;
 | 
	
		
			
				|  |  | +	COleDateTime g_lastTime;
 | 
	
		
			
				|  |  | +	time_t g_time = time(NULL);
 | 
	
		
			
				|  |  | +	ThreadSection g_critSection;
 | 
	
		
			
				|  |  | +	FILE *gp_log_fp = NULL;
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +	//////////////////////////////////////////////////////////////////////////
 | 
	
		
			
				|  |  | +	bool MKDIR(LPCTSTR dir) 
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		//////////////////////////////////////////////////////////////////////////
 | 
	
		
			
				|  |  | +		// 创建目录;
 | 
	
		
			
				|  |  | +		int nleft = 0;
 | 
	
		
			
				|  |  | +		int nIndex = -1;
 | 
	
		
			
				|  |  | +		string strdir = dir;
 | 
	
		
			
				|  |  | +		strdir = strdir.substr(0, strdir.find_last_of(_T("\\")));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if (strdir.at(strdir.size() - 1) != _T('\\'))
 | 
	
		
			
				|  |  | +			strdir.append(_T("\\"));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 共享路径和硬盘盘符;
 | 
	
		
			
				|  |  | +		if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
 | 
	
		
			
				|  |  | +			nleft = strdir.find_first_of(_T("\\"), 2) + 1;	// 去除共享主机名;
 | 
	
		
			
				|  |  | +		else if (strdir.at(2) == _T('\\'))
 | 
	
		
			
				|  |  | +			nleft = 3;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		do
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if (nIndex != string::npos)
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				if (_mkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
 | 
	
		
			
				|  |  | +				{
 | 
	
		
			
				|  |  | +					WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
 | 
	
		
			
				|  |  | +					return false;
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				nleft += nIndex + 1;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		} while (nIndex != -1);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return true;
 | 
	
		
			
				|  |  | +	};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// 全局函数;
 | 
	
		
			
				|  |  | +	/************************************************************************/
 | 
	
		
			
				|  |  | +	/*  函数:WriteTextLog[7/28/2009 Jeff];
 | 
	
		
			
				|  |  | +	/*  描述:写文本日志;
 | 
	
		
			
				|  |  | +	/*  参数:;
 | 
	
		
			
				|  |  | +	/*  	[IN] :;
 | 
	
		
			
				|  |  | +	/*  返回:void;
 | 
	
		
			
				|  |  | +	/*  注意:;
 | 
	
		
			
				|  |  | +	/*  示例:;
 | 
	
		
			
				|  |  | +	/*
 | 
	
		
			
				|  |  | +	/*  修改:;
 | 
	
		
			
				|  |  | +	/*  日期:;
 | 
	
		
			
				|  |  | +	/*  内容:;
 | 
	
		
			
				|  |  | +	/************************************************************************/
 | 
	
		
			
				|  |  | +	void WriteTextLog(const TCHAR* format, ...)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		static ThreadSection _critSection;
 | 
	
		
			
				|  |  | +		AutoThreadSection aSection(&_critSection);
 | 
	
		
			
				|  |  | +		// 将日志内容输入到文件中;
 | 
	
		
			
				|  |  | +		// 获取今年年份;
 | 
	
		
			
				|  |  | +		__time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
 | 
	
		
			
				|  |  | +		struct tm gmtm = { 0 };
 | 
	
		
			
				|  |  | +		localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 解析出日志路径;
 | 
	
		
			
				|  |  | +		TCHAR szlogpath[MAX_PATH] = { 0 };
 | 
	
		
			
				|  |  | +		_stprintf_s(szlogpath, _T("%srunner-log %02d%02d.txt"), g_szCurModuleDir, gmtm.tm_mon + 1, gmtm.tm_mday);
 | 
	
		
			
				|  |  | +		// 打开或创建文件;
 | 
	
		
			
				|  |  | +		FILE* fp = NULL;
 | 
	
		
			
				|  |  | +		//MKDIR(g_szLogPath);
 | 
	
		
			
				|  |  | +		//if (_taccess(szlogpath, 0) != -1)
 | 
	
		
			
				|  |  | +#ifndef UNICODE
 | 
	
		
			
				|  |  | +		if (_access(szlogpath, 0) != -1)
 | 
	
		
			
				|  |  | +#else
 | 
	
		
			
				|  |  | +		if (_taccess(szlogpath, 0) != -1)
 | 
	
		
			
				|  |  | +#endif
 | 
	
		
			
				|  |  | +		{ // 存在;
 | 
	
		
			
				|  |  | +			if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
 | 
	
		
			
				|  |  | +				// 移动到末尾;
 | 
	
		
			
				|  |  | +				fseek(fp, 0, SEEK_END);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		else
 | 
	
		
			
				|  |  | +		{ // 不存在;
 | 
	
		
			
				|  |  | +			_tfopen_s(&fp, szlogpath, _T("w+"));
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if (fp == NULL)
 | 
	
		
			
				|  |  | +			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);
 | 
	
		
			
				|  |  | +		// _vscprintf doesn't count. terminating '\0'
 | 
	
		
			
				|  |  | +		len = _vsctprintf(format, args) + 1;
 | 
	
		
			
				|  |  | +		buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
 | 
	
		
			
				|  |  | +		_vstprintf_s(buffer, len, format, args);
 | 
	
		
			
				|  |  | +		_ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 关闭文件,释放资源并设置回原语言区域;
 | 
	
		
			
				|  |  | +		free(buffer);
 | 
	
		
			
				|  |  | +		fclose(fp);
 | 
	
		
			
				|  |  | +		_tsetlocale(LC_CTYPE, old_locale);
 | 
	
		
			
				|  |  | +		free(old_locale); //还原区域设定;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	void WritePythonLog(LPCTSTR file, const TCHAR* msg)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		AutoThreadSection aSection(&g_critSection);
 | 
	
		
			
				|  |  | +		// 将日志内容输入到文件中;
 | 
	
		
			
				|  |  | +		// 获取今年年份;
 | 
	
		
			
				|  |  | +		//__time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
 | 
	
		
			
				|  |  | +		//struct tm gmtm = { 0 };
 | 
	
		
			
				|  |  | +		//localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		int nErr = -1;
 | 
	
		
			
				|  |  | +		// 打开或创建文件;
 | 
	
		
			
				|  |  | +		FILE* fp = NULL;
 | 
	
		
			
				|  |  | +		
 | 
	
		
			
				|  |  | +#ifndef UNICODE
 | 
	
		
			
				|  |  | +		if (_access(file, 0) != -1)
 | 
	
		
			
				|  |  | +#else
 | 
	
		
			
				|  |  | +		if (_taccess(file, 0) != -1)
 | 
	
		
			
				|  |  | +#endif
 | 
	
		
			
				|  |  | +		{ // 存在;
 | 
	
		
			
				|  |  | +			if (0 == _tfopen_s(&fp, file, _T("a+")))
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				// 移动到末尾;
 | 
	
		
			
				|  |  | +				fseek(fp, 0, SEEK_END);
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			else
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				WriteTextLog(_T("打开文件失败:%s,%d"), file, GetLastError());
 | 
	
		
			
				|  |  | +				return;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		else
 | 
	
		
			
				|  |  | +		{ // 不存在;
 | 
	
		
			
				|  |  | +			MKDIR(file);
 | 
	
		
			
				|  |  | +			if (0 !=  _tfopen_s(&fp, file, _T("w+")) )
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				WriteTextLog(_T("创建文件失败:%s,%d"), file, GetLastError());
 | 
	
		
			
				|  |  | +				return;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if (fp == NULL)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			WriteTextLog(_T("文件句柄空:%s,%d"), file, GetLastError());
 | 
	
		
			
				|  |  | +			return;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 格式化前设置语言区域;
 | 
	
		
			
				|  |  | +		TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
 | 
	
		
			
				|  |  | +		_tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
 | 
	
		
			
				|  |  | +		//_ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, msg);
 | 
	
		
			
				|  |  | +		_ftprintf(fp,,msg);
 | 
	
		
			
				|  |  | +		// 关闭文件,释放资源并设置回原语言区域;
 | 
	
		
			
				|  |  | +		fclose(fp);
 | 
	
		
			
				|  |  | +		_tsetlocale(LC_CTYPE, old_locale);
 | 
	
		
			
				|  |  | +		free(old_locale); //还原区域设定;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// 从注册表获取 Python27路径;
 | 
	
		
			
				|  |  | +	BOOL Python27Dir(LPTSTR lpPython27Dir, int nBufferLen)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		HKEY hKey;
 | 
	
		
			
				|  |  | +		int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node\\Python\\PythonCore\\2.7\\InstallPath"), 0, KEY_QUERY_VALUE, &hKey);
 | 
	
		
			
				|  |  | +		if (ret != ERROR_SUCCESS)
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		//读取KEY
 | 
	
		
			
				|  |  | +		DWORD dwType = REG_SZ; //数据类型
 | 
	
		
			
				|  |  | +		DWORD cbData = nBufferLen;
 | 
	
		
			
				|  |  | +		ret = RegQueryValueEx(hKey, _T(""), NULL, &dwType, (LPBYTE)lpPython27Dir, &cbData);
 | 
	
		
			
				|  |  | +		if (ret != ERROR_SUCCESS)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			RegCloseKey(hKey);
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		RegCloseKey(hKey);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return TRUE;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	void ClosePythonLog()
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		AutoThreadSection aSection(&g_critSection);
 | 
	
		
			
				|  |  | +		if ( gp_log_fp )
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			WriteTextLog(_T("关闭文件:%s"), g_szLogPath);
 | 
	
		
			
				|  |  | +			fclose(gp_log_fp);
 | 
	
		
			
				|  |  | +			gp_log_fp = NULL;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// 去除空格;
 | 
	
		
			
				|  |  | +	std::string& trim(std::string& str)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		int nIndex = 0;
 | 
	
		
			
				|  |  | +		while ((nIndex = str.find_first_of(' ')) != std::string::npos)
 | 
	
		
			
				|  |  | +			str.erase(nIndex, 1);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return str;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// hModule 模块句柄 NULL表示当前模块;
 | 
	
		
			
				|  |  | +	bool GetVersion(IN const TCHAR* fname, OUT WORD* pdwFileVersion, OUT WORD* pdwProductVerion)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		VS_FIXEDFILEINFO* pVi = NULL;
 | 
	
		
			
				|  |  | +		DWORD dwHandle = 0;
 | 
	
		
			
				|  |  | +		int size = GetFileVersionInfoSize(fname, &dwHandle);
 | 
	
		
			
				|  |  | +		if (size > 0)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			BYTE* buffer = new BYTE[size];
 | 
	
		
			
				|  |  | +			memset(buffer, 0, size);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if (GetFileVersionInfo(fname, 0, size, buffer))
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				if (VerQueryValue(buffer, _T("\\"), (LPVOID*)& pVi, (PUINT)& size))
 | 
	
		
			
				|  |  | +				{
 | 
	
		
			
				|  |  | +					pdwFileVersion[0] = HIWORD(pVi->dwFileVersionMS);
 | 
	
		
			
				|  |  | +					pdwFileVersion[1] = LOWORD(pVi->dwFileVersionMS);
 | 
	
		
			
				|  |  | +					pdwFileVersion[2] = HIWORD(pVi->dwFileVersionLS);
 | 
	
		
			
				|  |  | +					pdwFileVersion[3] = LOWORD(pVi->dwFileVersionLS);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +					pdwProductVerion[0] = HIWORD(pVi->dwProductVersionMS);
 | 
	
		
			
				|  |  | +					pdwProductVerion[1] = LOWORD(pVi->dwProductVersionMS);
 | 
	
		
			
				|  |  | +					pdwProductVerion[2] = HIWORD(pVi->dwProductVersionLS);
 | 
	
		
			
				|  |  | +					pdwProductVerion[3] = LOWORD(pVi->dwProductVersionLS);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +					delete[] buffer;
 | 
	
		
			
				|  |  | +					return true;
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			delete[] buffer;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return false;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	BOOL GetVersion(IN HMODULE hModule, OUT DWORD(&dwFVArray)[4], OUT DWORD(&dwPVArray)[4])
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		TCHAR fname[MAX_PATH];
 | 
	
		
			
				|  |  | +		VS_FIXEDFILEINFO* pVi;
 | 
	
		
			
				|  |  | +		DWORD dwHandle;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if (GetModuleFileName(hModule, fname, MAX_PATH))
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if (nSize > 0)
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				BYTE* pBuffer = new BYTE[nSize];
 | 
	
		
			
				|  |  | +				memset(pBuffer, 0, nSize);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				if (GetFileVersionInfo(fname, 0, nSize, pBuffer))
 | 
	
		
			
				|  |  | +				{
 | 
	
		
			
				|  |  | +					if (VerQueryValue(pBuffer, _T("\\"), (LPVOID*)& pVi, (PUINT)& nSize))
 | 
	
		
			
				|  |  | +					{
 | 
	
		
			
				|  |  | +						dwFVArray[0] = HIWORD(pVi->dwFileVersionMS);
 | 
	
		
			
				|  |  | +						dwFVArray[1] = LOWORD(pVi->dwFileVersionMS);
 | 
	
		
			
				|  |  | +						dwFVArray[2] = HIWORD(pVi->dwFileVersionLS);
 | 
	
		
			
				|  |  | +						dwFVArray[3] = LOWORD(pVi->dwFileVersionLS);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +						dwPVArray[0] = HIWORD(pVi->dwProductVersionMS);
 | 
	
		
			
				|  |  | +						dwPVArray[1] = LOWORD(pVi->dwProductVersionMS);
 | 
	
		
			
				|  |  | +						dwPVArray[2] = HIWORD(pVi->dwProductVersionLS);
 | 
	
		
			
				|  |  | +						dwPVArray[3] = LOWORD(pVi->dwProductVersionLS);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +						delete[]pBuffer;
 | 
	
		
			
				|  |  | +						return TRUE;
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				if (pBuffer)
 | 
	
		
			
				|  |  | +					delete[]pBuffer;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return FALSE;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	WCHAR* ASCII2UNICODE(IN LPCCH lpASCIIStr)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		if (lpASCIIStr == NULL)
 | 
	
		
			
				|  |  | +			return NULL;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 获取宽字符字节数;
 | 
	
		
			
				|  |  | +		int cchWideChar = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, NULL, 0);
 | 
	
		
			
				|  |  | +		if (cchWideChar == 0)
 | 
	
		
			
				|  |  | +			return NULL;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 转换成宽字符串;
 | 
	
		
			
				|  |  | +		WCHAR* pWideChar = new WCHAR[cchWideChar + 1];
 | 
	
		
			
				|  |  | +		memset(pWideChar, 0, sizeof(WCHAR) * (cchWideChar + 1));
 | 
	
		
			
				|  |  | +		int nWriteNum = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, pWideChar, cchWideChar);
 | 
	
		
			
				|  |  | +		if (nWriteNum != cchWideChar)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			if (pWideChar)
 | 
	
		
			
				|  |  | +				delete[] pWideChar;
 | 
	
		
			
				|  |  | +			return NULL;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return pWideChar;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	BOOL UNICODE2UTF8(IN LPWCH lpUNICODEStr, OUT string& strResult)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		if (lpUNICODEStr == NULL)
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 获取多字节字符字节数;
 | 
	
		
			
				|  |  | +		int cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
 | 
	
		
			
				|  |  | +		if (cbMultiByte == 0)
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 转换成多字节字符;
 | 
	
		
			
				|  |  | +		CHAR* pResult = new CHAR[cbMultiByte];
 | 
	
		
			
				|  |  | +		memset(pResult, 0, cbMultiByte);
 | 
	
		
			
				|  |  | +		int nWriteNum = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, pResult, cbMultiByte, NULL, NULL);
 | 
	
		
			
				|  |  | +		if (nWriteNum != cbMultiByte)
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		strResult = pResult;
 | 
	
		
			
				|  |  | +		if (pResult)
 | 
	
		
			
				|  |  | +			delete[] pResult;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return TRUE;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	BOOL ASCII2UTF8(IN LPCCH lpASCIIStr, OUT string& strResult)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		// 将ASCII字符串转成UNICODE字符串;
 | 
	
		
			
				|  |  | +		WCHAR* pWideChar = ASCII2UNICODE(lpASCIIStr);
 | 
	
		
			
				|  |  | +		if (pWideChar == NULL)
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 再将UICODE转成UTF8;
 | 
	
		
			
				|  |  | +		BOOL bResult = UNICODE2UTF8(pWideChar, strResult);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if (pWideChar)
 | 
	
		
			
				|  |  | +			delete[] pWideChar;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return bResult;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	string EnCode_UTF8URL(IN const CHAR* pText)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		string tt = "";
 | 
	
		
			
				|  |  | +		string dd = "";
 | 
	
		
			
				|  |  | +		ASCII2UTF8(pText, tt);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		size_t len = tt.length();
 | 
	
		
			
				|  |  | +		for (size_t i = 0; i < len; i++)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			if (isalnum((BYTE)tt.at(i)))
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				char tempbuff[2] = { 0 };
 | 
	
		
			
				|  |  | +				sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
 | 
	
		
			
				|  |  | +				dd.append(tempbuff);
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			else if (isspace((BYTE)tt.at(i)))
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				dd.append("+");
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			else
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				char tempbuff[4];
 | 
	
		
			
				|  |  | +				sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
 | 
	
		
			
				|  |  | +				dd.append(tempbuff);
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		return dd;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	void EnCode_UTF8URL(IN const CHAR* pText, OUT string& strResult)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		string tt = "";
 | 
	
		
			
				|  |  | +		ASCII2UTF8(pText, tt);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		size_t len = tt.length();
 | 
	
		
			
				|  |  | +		for (size_t i = 0; i < len; i++)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			if (isalnum((BYTE)tt.at(i)))
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				char tempbuff[2] = { 0 };
 | 
	
		
			
				|  |  | +				sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
 | 
	
		
			
				|  |  | +				strResult.append(tempbuff);
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			else if (isspace((BYTE)tt.at(i)))
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				strResult.append("+");
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			else
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				char tempbuff[4];
 | 
	
		
			
				|  |  | +				sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
 | 
	
		
			
				|  |  | +				strResult.append(tempbuff);
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	//做为解Url使用
 | 
	
		
			
				|  |  | +	char CharToInt(char ch)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		if (ch >= '0' && ch <= '9')return (char)(ch - '0');
 | 
	
		
			
				|  |  | +		if (ch >= 'a' && ch <= 'f')return (char)(ch - 'a' + 10);
 | 
	
		
			
				|  |  | +		if (ch >= 'A' && ch <= 'F')return (char)(ch - 'A' + 10);
 | 
	
		
			
				|  |  | +		return -1;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	char StrToBin(IN char(&str)[2])
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		char tempWord[2];
 | 
	
		
			
				|  |  | +		char chn;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		tempWord[0] = CharToInt(str[0]);                         //make the B to 11 -- 00001011
 | 
	
		
			
				|  |  | +		tempWord[1] = CharToInt(str[1]);                         //make the 0 to 0  -- 00000000
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		chn = (tempWord[0] << 4) | tempWord[1];                //to change the BO to 10110000
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return chn;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	CHAR* UNICODE2ASCII(IN LPWCH lpUNICODEStr)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		if (lpUNICODEStr == NULL)
 | 
	
		
			
				|  |  | +			return NULL;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 获取多字节字符字节数;
 | 
	
		
			
				|  |  | +		int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
 | 
	
		
			
				|  |  | +		if (cbMultiByte == 0)
 | 
	
		
			
				|  |  | +			return NULL;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 转换成多字节字符;
 | 
	
		
			
				|  |  | +		CHAR *pMultiByteStr = new CHAR[cbMultiByte + 1];
 | 
	
		
			
				|  |  | +		memset(pMultiByteStr, 0, cbMultiByte + 1);
 | 
	
		
			
				|  |  | +		int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
 | 
	
		
			
				|  |  | +		if (nWriteNum != cbMultiByte)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			if (pMultiByteStr)
 | 
	
		
			
				|  |  | +				delete[]pMultiByteStr;
 | 
	
		
			
				|  |  | +			return NULL;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		pMultiByteStr[cbMultiByte] = '\0';
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return pMultiByteStr;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	BOOL UNICODE2ASCII(IN LPWCH lpUNICODEStr, OUT LPCH pASCIIStr, IN CONST INT& nASCIIStrLen)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		if (lpUNICODEStr == NULL)
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 获取多字节字符字节数;
 | 
	
		
			
				|  |  | +		int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
 | 
	
		
			
				|  |  | +		if (cbMultiByte == 0 || cbMultiByte >= nASCIIStrLen)
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 转换成多字节字符;
 | 
	
		
			
				|  |  | +		memset((void*)pASCIIStr, 0, nASCIIStrLen);
 | 
	
		
			
				|  |  | +		int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pASCIIStr, cbMultiByte, NULL, NULL);
 | 
	
		
			
				|  |  | +		if (nWriteNum != cbMultiByte)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return TRUE;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	/************************************************************************/
 | 
	
		
			
				|  |  | +	/*  函数:[7/26/2016 IT];
 | 
	
		
			
				|  |  | +	/*  描述:;
 | 
	
		
			
				|  |  | +	/*  参数:;
 | 
	
		
			
				|  |  | +	/*  	[IN] :;
 | 
	
		
			
				|  |  | +	/*  	[OUT] :;
 | 
	
		
			
				|  |  | +	/*  	[IN/OUT] :;
 | 
	
		
			
				|  |  | +	/*  返回:void;
 | 
	
		
			
				|  |  | +	/*  注意:;
 | 
	
		
			
				|  |  | +	/*  示例:;
 | 
	
		
			
				|  |  | +	/*
 | 
	
		
			
				|  |  | +	/*  修改:;
 | 
	
		
			
				|  |  | +	/*  日期:;
 | 
	
		
			
				|  |  | +	/*  内容:;
 | 
	
		
			
				|  |  | +	/************************************************************************/
 | 
	
		
			
				|  |  | +	string DeCode_URLUNICODE(IN const CHAR* pURLText)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		string str = pURLText;
 | 
	
		
			
				|  |  | +		string strResult = "";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		INT nIndex = 0;
 | 
	
		
			
				|  |  | +		string strTemp = "";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		while (str.find("\\u", 0, 2) != string::npos)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			nIndex = str.find("\\u", 0, 2);
 | 
	
		
			
				|  |  | +			strResult.append(str.substr(0, nIndex));
 | 
	
		
			
				|  |  | +			strTemp = str.substr(nIndex + 2, 4);
 | 
	
		
			
				|  |  | +			str = str.substr(nIndex + 2 + 4);
 | 
	
		
			
				|  |  | +			CHAR szReturn[10] = { 0 };
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			union  __UNION_VAR_INT {
 | 
	
		
			
				|  |  | +				BYTE   ch[2];
 | 
	
		
			
				|  |  | +				int	   value;
 | 
	
		
			
				|  |  | +			}unionVarInt;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			unionVarInt.ch[0] = (CharToInt(strTemp.at(2)) << 4) | (CharToInt(strTemp.at(3)) & 0x00FF);
 | 
	
		
			
				|  |  | +			unionVarInt.ch[1] = (CharToInt(strTemp.at(0)) << 4) | (CharToInt(strTemp.at(1)) & 0x00FF);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			WCHAR szWide[2] = { 0 };
 | 
	
		
			
				|  |  | +			szWide[0] = unionVarInt.value;
 | 
	
		
			
				|  |  | +			UNICODE2ASCII(szWide, szReturn, 10);
 | 
	
		
			
				|  |  | +			strResult.append(szReturn);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		strResult.append(str);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return strResult;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// 通过注册表查找系统当前串口信息;
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +	BOOL IsValidString(LPCTSTR lpszString)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		if (lpszString == NULL)
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		do
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			// ASCII可显示的字符;
 | 
	
		
			
				|  |  | +			if (*lpszString < 32 || *lpszString > 126)
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				return FALSE;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		} while (*++lpszString);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return TRUE;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	BOOL Python27Dir()
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		HKEY hKey;
 | 
	
		
			
				|  |  | +		int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node\\Python\\PythonCore\\2.7\\InstallPath"), 0, KEY_QUERY_VALUE, &hKey);
 | 
	
		
			
				|  |  | +		if (ret != ERROR_SUCCESS)
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		//读取KEY
 | 
	
		
			
				|  |  | +		DWORD dwType = REG_SZ; //数据类型
 | 
	
		
			
				|  |  | +		DWORD cbData = MAX_PATH;
 | 
	
		
			
				|  |  | +		ret = RegQueryValueEx(hKey, _T(""), NULL, &dwType, (LPBYTE)g_szPython27Dir, &cbData);
 | 
	
		
			
				|  |  | +		if (ret != ERROR_SUCCESS)
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			RegCloseKey(hKey);
 | 
	
		
			
				|  |  | +			return FALSE;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		RegCloseKey(hKey);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return TRUE;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	void GetResourceCfg()
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		// 配置文件;
 | 
	
		
			
				|  |  | +		Python27Dir();
 | 
	
		
			
				|  |  | +		TCHAR szConfig[MAX_PATH] = {0};
 | 
	
		
			
				|  |  | +		_stprintf_s(szConfig, _T("%sLib\\site-packages\\ssat_sdk\\config\\resource_run.cfg"), g_szPython27Dir);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		// 电视串口号;
 | 
	
		
			
				|  |  | +		GetPrivateProfileString(_T("COMM"), _T("serial_communicator_port"), NULL, g_szTVPort, MAX_PATH, szConfig);
 | 
	
		
			
				|  |  | +		// 重启关键字;
 | 
	
		
			
				|  |  | +		g_bWatchTVPort = GetPrivateProfileInt(_T("Abnormal"), _T("Watch"), 0, szConfig);
 | 
	
		
			
				|  |  | +		// 重启关键字;
 | 
	
		
			
				|  |  | +		GetPrivateProfileString(_T("Abnormal"), _T("reboot"), NULL, g_szTVReboot, MAX_PATH, szConfig);
 | 
	
		
			
				|  |  | +		// 关机关键字;
 | 
	
		
			
				|  |  | +		GetPrivateProfileString(_T("Abnormal"), _T("shutdown"), NULL, g_szTVShutdown, MAX_PATH, szConfig);
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	void Split(std::string str1, std::string str2, std::vector<std::string> &vt)
 | 
	
		
			
				|  |  | +	{
 | 
	
		
			
				|  |  | +		if ( str1.size() == 0 || str2.size() == 0 )
 | 
	
		
			
				|  |  | +			return;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		int npos = str1.find(str2);
 | 
	
		
			
				|  |  | +		while( std::string::npos != npos )
 | 
	
		
			
				|  |  | +		{
 | 
	
		
			
				|  |  | +			vt.push_back(str1.substr(0, npos));
 | 
	
		
			
				|  |  | +			str1 = str1.substr(npos + str2.size());
 | 
	
		
			
				|  |  | +			npos = str1.find(str2);
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if ( str1.size() )
 | 
	
		
			
				|  |  | +			vt.push_back(str1);
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +} // namespace Global
 |