#include "StdAfx.h"
#include "Global.h"
#include "CritSection.h"
#include <locale.h>


namespace Global
{
	// ����̨���;
	BOOL g_bStdOut = FALSE;
	// �������;
	INT g_nDesktopWidth = 0;
	// ����߶�;
	INT g_nDesktopHeight = 0;
	// �������߶�;
	INT g_nTrayWndHeight = 0;
	// x����̶�;
	double g_fScaleX = 0.0;
	// y����̶�;
	double g_fScaleY = 0.0;
	// ��ӡ����ʾ�豸��Ԥ��ֵ�ͻ���ֵ;
	DEVMODE g_devMode = {0};	

	TCHAR g_szModulePath[MAX_PATH] = _T("");			// ����Ŀ¼;
	TCHAR g_szModuleFileName[MAX_PATH] = _T("");		// ��������;
	TCHAR g_szConnectString[MAX_PATH] = _T("");			// DB���Ӵ�;
	// ��������Ϣ;
	TCHAR g_szSvrAddress[MAX_PATH] = _T("");
	TCHAR g_szDBSource[MAX_PATH] = _T("");				// ���ݿ�Դ(��������IP��������);
	TCHAR g_szDBSourcePort[MAX_PATH] = _T("");			// ���ݿ�Դ�˿�;
	DWORD g_dwDBServerPort = 0;							// ���ݿ�Դ�˿�;
	TCHAR g_szDBAccount[MAX_PATH] = _T("");				// ���ݿ��¼�û�;
	TCHAR g_szDBPassWord[MAX_PATH] = _T("");			// ���ݿ��¼����;
	TCHAR g_szDBName[MAX_PATH] = _T("");				// ���ݿ�����;	
	TCHAR g_szStudioID[64] = _T("");					// Ӱ¥ID;
	DWORD g_dwSvrPort = 0;
	DWORD g_dwDBPoolMaxCount = 1;						// ���ݿ����ӳ���������;
	DWORD g_dwDBPoolDef = 1;							// ���ݿ����ӳ�Ĭ�϶�����;

	ModConfig g_ModConfig;
	/************************************************************************/
	/*  ������[5/20/2016 IT];
	/*  ������;
	/*  ������;
	/*  	[IN] ��;
	/*  	[OUT] ��;
	/*  	[IN/OUT] ��;
	/*  ���أ�void;
	/*  ע�⣺;
	/*  ʾ����;
	/*
	/*  �޸ģ�;
	/*  ���ڣ�;
	/*  ���ݣ�;
	/************************************************************************/
	int GetIniInfo(const TCHAR *szPath, const TCHAR *szIniName)
	{
		TCHAR szDrive[_MAX_DRIVE] = {0};
		TCHAR szDir[_MAX_DIR] = {0};
		TCHAR szFName[_MAX_FNAME] = {0};
		TCHAR szExt[_MAX_EXT] = {0};
		::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
		_stprintf_s(g_szModulePath, MAX_PATH, _T("%s"), g_szModulePath);

		_tsplitpath_s(g_szModulePath, szDrive, szDir, szFName, szExt);
		_tcscpy_s(g_szModulePath, szDrive);
		_tcscat_s(g_szModulePath, szDir);

		// -----------------------------------------------------//
		TCHAR szIniPath[MAX_PATH] = {0};
		if (szPath != NULL && szIniName != NULL)
			_stprintf_s(szIniPath, _T("%s%s"), szPath, szIniName);
		else
			_stprintf_s(szIniPath, _T("%smod.ini"), g_szModulePath);

		HANDLE hFile = CreateFile(szIniPath, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL);
		if (ERROR_FILE_NOT_FOUND == GetLastError())
		{
			_stprintf_s(g_ModConfig.szModSavePath, _T("%smodules_svn.cfg"), g_szModulePath);
			WritePrivateProfileString(_T("Mod"), _T("DIR"), g_ModConfig.szModSavePath, szIniPath);
			return -1;
		}
		CloseHandle(hFile);

		// ��ȡ����������Ϣ;
		GetPrivateProfileString(_T("Mod"), _T("DIR"), _T(""), g_ModConfig.szModSavePath, MAX_PATH, szIniPath);
		if ( _tcslen(g_ModConfig.szModSavePath) == 0 )
			_stprintf_s(g_ModConfig.szModSavePath, _T("%smodules_svn.cfg"), g_szModulePath);
		
		if (!g_bStdOut)
		{
			g_bStdOut = GetPrivateProfileInt(_T("NetWorkInfo"), _T("StdOut"), 0, szIniPath);
			if (g_bStdOut)
			{
				AllocConsole();                             // ���ٿ���̨;
				SetConsoleTitle(_T("�������"));			// ���ÿ���̨���ڱ���;
				freopen("CONOUT$", "w+t", stdout);			// �ض������;
				freopen("CONIN$", "r+t", stdin);			// �ض�������;

				HWND hWnd = NULL;
			again:
				hWnd = ::FindWindow(NULL, _T("�������"));
				if (hWnd)
				{
					if (!::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE))
					{
						_tprintf(_T("ǰ������ʧ��\n"));
					}
					else
					{
						_tprintf(_T("ǰ�����óɹ�\n"));
					}
				}
				else
				{
					goto again;
				}
			}
		}

		return 0;
	}

	/************************************************************************/
	/*  ������WriteTextLog[7/28/2016 IT];
	/*  ������д�ı���־;
	/*  ������;
	/*  	[IN] ��;
	/*  ���أ�void;
	/*  ע�⣺;
	/*  ʾ����;
	/*
	/*  �޸ģ�;
	/*  ���ڣ�;
	/*  ���ݣ�;
	/************************************************************************/
	void WriteTextLog(const TCHAR *format, ...)
	{
		try
		{
			static ThreadSection _critSection;
			AutoThreadSection aSection(&_critSection);
			// ��������־·��;
			TCHAR szlogpath[MAX_PATH] = {0};
			static TCHAR szModulePath[MAX_PATH] = {0};
			static TCHAR szFna[_MAX_DIR] = { 0 };
			if ( szModulePath[0] == _T('\0') )
			{
				TCHAR szDrive[_MAX_DRIVE] = { 0 };
				TCHAR szDir[_MAX_DIR] = { 0 };
				TCHAR szExt[_MAX_DIR] = { 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%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]"));

			// �򿪻򴴽��ļ�;
			CStdioFile fp;
			if (PathFileExists(szlogpath))
			{
				if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
				{
					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 );
			// _vscprintf doesn't count. terminating '\0'
			len = _vsctprintf_p( 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();
		}
	}

	/************************************************************************/
	/*  ������[10/27/2016 IT];
	/*  ������;
	/*  ������;
	/*  	[IN] ��;
	/*  	[OUT] ��;
	/*  	[IN/OUT] ��;
	/*  ���أ�void;
	/*  ע�⣺;
	/*  ʾ����;
	/*
	/*  �޸ģ�;
	/*  ���ڣ�;
	/*  ���ݣ�;
	/************************************************************************/
	TCHAR* GetGuidString()
	{
		GUID guid;
		static TCHAR szBuf[64] = { 0 };
		memset(szBuf, 0, 64 * sizeof(TCHAR));
		CoInitialize(NULL);
		if (S_OK == ::CoCreateGuid(&guid))
		{
			_stprintf_s(szBuf, 64, _T("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"),
				guid.Data1,
				guid.Data2,
				guid.Data3,
				guid.Data4[0],
				guid.Data4[1],
				guid.Data4[2],
				guid.Data4[3],
				guid.Data4[4],
				guid.Data4[5],
				guid.Data4[6],
				guid.Data4[7]
			);
		}
		CoUninitialize();

		return szBuf;
	}


	//�����Զ�����ע�����
	INT AddAutoSysRun(LPBYTE lpExeFile,CONST DWORD &cbExeFile, LPCTSTR lpKeyName, INT iAutoRunSystem)
	{
		HKEY hKey = NULL;
		DWORD dwErrorCode = 0;

		//����ע������иü����ȡ�����򴴽�
		dwErrorCode = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
			_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
			0,
			NULL,
			REG_OPTION_NON_VOLATILE,
			KEY_ALL_ACCESS,	// 
			//KEY_READ,
			NULL, //Security
			&hKey,
			NULL);
		if (dwErrorCode == ERROR_SUCCESS)
		{
			if (iAutoRunSystem)
			{
				dwErrorCode = RegSetValueEx(hKey, lpKeyName, NULL, REG_SZ, lpExeFile, cbExeFile);
			}
			else
			{
				dwErrorCode = RegDeleteValue(hKey, lpKeyName);
				dwErrorCode = RegDeleteKey(hKey, lpKeyName);
			}
		}

		if (NULL != hKey)
			RegCloseKey(hKey);
		hKey = NULL;

		return 0;
	}
};