123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- #include "stdafx.h"
- #include "Global.h"
- std::string g_strDefaultTip = "指纹机采集程序";
- std::vector<STGeneralLogData> g_vtGeneralLogData;
- std::vector<STSuperLogData> g_vtSuperLogData;
- HWND g_hwnd = NULL;
- TCHAR g_ModulePath[_MAX_PATH] = {0};
- TCHAR g_ModuleFileName[_MAX_PATH] = {0};
- TCHAR g_szDBServer[MAX_PATH+1] = {0};
- DWORD g_dwDBServerPort = 0;
- TCHAR g_szDBAccount[MAX_PATH+1] = {0};
- TCHAR g_szDBPassWord[MAX_PATH+1] = {0};
- TCHAR g_szDBName[MAX_PATH+1] = {0};
- long g_lLicense = 4335;
- //////////////////////////////////////////////////////////////////////////
- typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
- LPFN_ISWOW64PROCESS fnIsWow64Process;
- BOOL IsWow64()
- {
- BOOL bIsWow64 = FALSE;
- fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
- GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
- if (NULL != fnIsWow64Process)
- {
- if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
- {
- // handle error
- }
- }
- return bIsWow64;
- }
- //////////////////////////////////////////////////////////////////////////
- //wchar* to char*
- int WChar2Char(char* pDest, const wchar_t* pSource)
- {
- if(pSource == NULL || pDest == NULL)
- return -1;
- int nLen = ::WideCharToMultiByte(CP_ACP, NULL, pSource, wcslen(pSource), NULL, 0, NULL, NULL);
- // Unicode版对应的strlen是wcslen
- ::WideCharToMultiByte(CP_ACP, NULL, pSource, wcslen(pSource), pDest, nLen, NULL, NULL);
- // 最后加上'\0'
- pDest[nLen] = '\0';
- return nLen;
- }
- //char* to wchar*
- int Char2WChar(wchar_t* pDest, const char* pSource)
- {
- if(pSource == NULL || pDest == NULL)
- return -1;
- int nLen = ::MultiByteToWideChar(CP_ACP, 0, pSource, -1, NULL, 0);
- ::MultiByteToWideChar(CP_ACP, 0, pSource, -1, pDest, nLen);
- return nLen;
- }
- /************************************************************************/
- /* 函数:[2/6/2017 IT];
- /* 描述:注册ocx控件;
- /* 参数:;
- /* [IN] lpszOCXfile:ocx控件路径;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL RegisterOcx(IN LPCTSTR lpszOCXfile)
- {
- // 参数有效性判断 ;
- if ( lpszOCXfile == NULL )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数lpszOCXfile 指针无效\n"));
- #endif
- return FALSE;
- }
- if ( !PathFileExists(lpszOCXfile) )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数lpszOCXfile 路径无效\n"));
- #endif
- return FALSE;
- }
- HMODULE hModule = LoadLibrary(lpszOCXfile);
- if ( hModule == NULL)
- {
- #ifdef _DEBUG
- DWORD dwError = GetLastError();
- CString strError = _T("");
- strError.Format(_T("加载ocx时出错:%d\n"), dwError);
- OutputDebugString(strError);
- #endif
- return FALSE;
- }
- // ocx入口点方法指针;
- FARPROC lpDllEntryPoint = NULL;
- //获取注册函数DllRegisterServer地址;
- lpDllEntryPoint = GetProcAddress(hModule, "DllRegisterServer");
- if ( lpDllEntryPoint == NULL )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数DllRegisterServer 指针无效\n"));
- #endif
- FreeLibrary(hModule);
- return FALSE;
- }
- // 开始调用注册函数注册ocx控件;
- if ( FAILED((*lpDllEntryPoint)()) )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数DllRegisterServer 注册失败\n"));
- #endif
- FreeLibrary(hModule);
- return FALSE;
- }
- FreeLibrary(hModule);
- return TRUE;
- }
- /************************************************************************/
- /* 函数:[2/6/2017 IT];
- /* 描述:卸载ocx控件;
- /* 参数:;
- /* [IN] lpszOCXfile:ocx控件路径;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL UnRegisterOcx(IN LPCTSTR lpszOCXfile)
- {
- // 参数有效性判断 ;
- if ( lpszOCXfile == NULL )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数lpszOCXfile 指针无效\n"));
- #endif
- return FALSE;
- }
- if ( !PathFileExists(lpszOCXfile) )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数lpszOCXfile 路径无效\n"));
- #endif
- return FALSE;
- }
- HMODULE hModule = LoadLibrary(lpszOCXfile);
- if ( hModule == NULL)
- {
- #ifdef _DEBUG
- DWORD dwError = GetLastError();
- CString strError = _T("");
- strError.Format(_T("加载ocx时出错:%d\n"), dwError);
- OutputDebugString(strError);
- #endif
- return FALSE;
- }
- // ocx入口点方法指针;
- FARPROC lpDllEntryPoint = NULL;
- //获取卸载函数DllUnregisterServer地址;
- lpDllEntryPoint = GetProcAddress(hModule, "DllUnregisterServer");
- if ( lpDllEntryPoint == NULL )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数DllUnregisterServer 指针无效\n"));
- #endif
- FreeLibrary(hModule);
- return FALSE;
- }
- // 开始调用卸载函数卸载ocx控件;
- if ( FAILED((*lpDllEntryPoint)()) )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数DllUnregisterServer 注册失败\n"));
- #endif
- FreeLibrary(hModule);
- return FALSE;
- }
- FreeLibrary(hModule);
- return TRUE;
- }
- /************************************************************************/
- /* 函数:[2/6/2017 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- BOOL IsRegisterOcx(IN LPCTSTR lpszOCXfile)
- {
- // 参数有效性判断 ;
- if ( lpszOCXfile == NULL )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数lpszOCXfile 指针无效\n"));
- #endif
- return FALSE;
- }
- if ( !PathFileExists(lpszOCXfile) )
- {
- #ifdef _DEBUG
- OutputDebugString(_T("参数lpszOCXfile 路径无效\n"));
- #endif
- return FALSE;
- }
- // 载入TypeLib
- ITypeLib *pTypeLib = NULL;
- #ifdef UNICODE
- HRESULT hr = LoadTypeLib(lpszOCXfile, &pTypeLib);
- #else
- // 多字节需要转换成UNICODE编码;
- // ....
- HRESULT hr = LoadTypeLib(lpszOCXfile, &pTypeLib);
- #endif
- if (FAILED(hr))
- {
- #ifdef _DEBUG
- OutputDebugString(_T("载入TypeLib失败\n"));
- #endif
- return FALSE;
- }
- // 成功载入后,获取guid;
- CString strGuid = _T("");
- // 获取类型信息;
- ITypeInfo *pTypeInfo = NULL;
- hr = pTypeLib->GetTypeInfo(0, &pTypeInfo); // 索引0方法,1事件,2控制;
- if (FAILED(hr)) {
- pTypeLib->Release();
- return FALSE;
- }
- // 获取类型属性;
- TYPEATTR* pTypeAttr = NULL;
- hr = pTypeInfo->GetTypeAttr(&pTypeAttr);
- if (FAILED(hr)) {
- pTypeLib->Release();
- return FALSE;
- }
- // 找到guid;
- if (pTypeAttr->typekind != TKIND_DISPATCH)
- {
- pTypeInfo->ReleaseTypeAttr(pTypeAttr);
- pTypeLib->Release();
- return FALSE;
- }
-
- strGuid.Format(_T("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
- pTypeAttr->guid.Data1,pTypeAttr->guid.Data2,pTypeAttr->guid.Data3
- ,pTypeAttr->guid.Data4[0],pTypeAttr->guid.Data4[1],pTypeAttr->guid.Data4[2],pTypeAttr->guid.Data4[3],
- pTypeAttr->guid.Data4[4],pTypeAttr->guid.Data4[5],pTypeAttr->guid.Data4[6],pTypeAttr->guid.Data4[7]);
- pTypeInfo->ReleaseTypeAttr(pTypeAttr);
- pTypeLib->Release();
- // 再到注册表里查找是否被注册过了;
- CString strRegKey = _T("");
- if ( IsWow64() )
- strRegKey.Format(_T("SOFTWARE\\CLASSES\\WOW6432NODE\\INTERFACE\\%s"), strGuid);
- else
- strRegKey.Format(_T("SOFTWARE\\CLASSES\\INTERFACE\\%s"), strGuid);
- HKEY hKey;
- return (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,strRegKey,0,KEY_READ,&hKey));
- }
- /************************************************************************/
- /* 函数: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]").GetString());
- // 打开或创建文件;
- 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();
- }
- }
|