123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- #include "StdAfx.h"
- #include "Global.h"
- //#include "CritSection.h"
- #include <locale.h>
- namespace Global
- {
- // 控制台输出;
- BOOL g_bStdOut = FALSE;
-
- 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; // 数据库连接池默认对象数;
- DWORD g_dwBalance = 100;
-
-
- /************************************************************************/
- /* 函数:[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));
- sprintf(g_szModulePath, _T("%s"), g_szModulePath);
-
- _tsplitpath(g_szModulePath, szDrive, szDir, szFName, szExt);
- strcpy(g_szModulePath, szDrive);
- strcat(g_szModulePath, szDir);
-
- // -----------------------------------------------------//
- TCHAR szIniPath[MAX_PATH] = {0};
- if (szPath != NULL && szIniName != NULL)
- sprintf(szIniPath, _T("%s%s"), szPath, szIniName);
- else
- sprintf(szIniPath, _T("%sServiceInfo.ini"), g_szModulePath);
-
- HANDLE hFile = CreateFile(szIniPath, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL);
- if (ERROR_FILE_NOT_FOUND == GetLastError())
- {
- return -1;
- }
- CloseHandle(hFile);
-
- // 获取服务器端信息;
- // 获取服务器端信息;
- GetPrivateProfileString(_T("DatabaseInfo"), _T("dbSource"), _T(""), g_szDBSource, MAX_PATH, szIniPath);
- g_dwDBServerPort = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbServerPort"), 0, szIniPath);
- GetPrivateProfileString(_T("DatabaseInfo"), _T("dbAccount"), _T(""), g_szDBAccount, MAX_PATH, szIniPath);
- GetPrivateProfileString(_T("DatabaseInfo"), _T("dbPassWord"), _T(""), g_szDBPassWord, MAX_PATH, szIniPath);
- GetPrivateProfileString(_T("DatabaseInfo"), _T("dbName"), _T(""), g_szDBName, MAX_PATH, szIniPath);
- g_dwDBPoolMaxCount = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolMaxObj"), 50, szIniPath);
- g_dwDBPoolDef = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolDef"), 3, szIniPath);
- g_dwSvrPort = GetPrivateProfileInt(_T("NetWorkInfo"), _T("TCPChatPort"), 5678, szIniPath);
-
- 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
- {
- // 解析出日志路径;
- 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));
- _splitpath(szModulePath, szDrive, szDir, szFna, szExt);
- strcpy(szModulePath, szDrive);
- strcat(szModulePath, szDir);
- }
-
- sprintf(szlogpath, _T("%s客户端日志.txt"), szModulePath);
-
- // 打开或创建文件;
- CStdioFile fp;
- if (PathFileExists(szlogpath))
- {
- if ( !fp.Open(szlogpath, CFile::modeWrite) )
- {
- 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;
- static TCHAR buffer[8192] = {0};
- va_start( args, format );
- memset(buffer, 0, 8192);
- vsprintf( buffer, format, args ); // C4996
-
- // 将日志内容输入到文件中;
- fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
- fp.WriteString(buffer);
- fp.WriteString(_T("\n"));
- _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(szBuf, _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;
- }
- };
|