123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645 |
- #include "stdafx.h"
- #include "Global.h"
- #include <tchar.h>
- #include <direct.h>
- #include <SetupAPI.h>
- #include <InitGuid.h>
- #include <WinIoCtl.h>
- #pragma comment(lib, "SetupAPI.lib")
- //////////////////////////////////////////////////////////////////////////
- // dll相关;
- HMODULE g_hdll = NULL;
- irc_IsAppRunning g_IsAppRunning = NULL;
- irc_StartIRApp g_StartIRApp = NULL;
- irc_CloseApp g_CloseApp = NULL;
- irc_Connect g_Connect = NULL;
- irc_DisConnect g_DisConnect = NULL;
- irc_loadSignalDataSet g_loadSignalDataSet = NULL;
- irc_getDeviceNames g_getDeviceNames = NULL;
- irc_getSignalsName g_getSignalsName = NULL;
- irc_sendSignal g_sendSignal = NULL;
- irc_sendSignals g_sendSignals = NULL;
- irc_sendRepeatsSignal g_sendRepeatsSignal = NULL;
- int g_nIRControl = 0;
- BOOL LoadLogLibarary()
- {
- if (g_hdll == NULL)
- {
- g_hdll = (HMODULE)LoadLibrary(_T("IRControl.dll"));
- if (!g_hdll)
- return FALSE;
- }
- g_sendRepeatsSignal = (irc_sendRepeatsSignal)GetProcAddress(g_hdll, "sendRepeatsSignal");
- if (!g_sendRepeatsSignal)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_sendSignals = (irc_sendSignals)GetProcAddress(g_hdll, "sendSignals");
- if (!g_sendSignals)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_sendSignal = (irc_sendSignal)GetProcAddress(g_hdll, "sendSignal");
- if (!g_sendSignal)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_loadSignalDataSet = (irc_loadSignalDataSet)GetProcAddress(g_hdll, "loadSignalDataSet");
- if (!g_loadSignalDataSet)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_IsAppRunning = (irc_IsAppRunning)GetProcAddress(g_hdll, "IsAppRunning");
- if (!g_IsAppRunning)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_StartIRApp = (irc_StartIRApp)GetProcAddress(g_hdll, "StartIRApp");
- if (!g_StartIRApp)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_CloseApp = (irc_CloseApp)GetProcAddress(g_hdll, "CloseApp");
- if (!g_CloseApp)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_getDeviceNames = (irc_getDeviceNames)GetProcAddress(g_hdll, "getDeviceNames");
- if (!g_getDeviceNames)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_Connect = (irc_Connect)GetProcAddress(g_hdll, "Connect");
- if (!g_Connect)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_getSignalsName = (irc_getSignalsName)GetProcAddress(g_hdll, "getSignalsName");
- if (!g_getSignalsName)
- {
- FreeLogLibarary();
- return FALSE;
- }
- g_DisConnect = (irc_DisConnect)GetProcAddress(g_hdll, "DisConnect");
- if (!g_DisConnect)
- {
- FreeLogLibarary();
- return FALSE;
- }
- // 导出全局变量;
- g_nIRControl = *(int*)GetProcAddress(g_hdll, "nIRControl");
- if (!g_CloseApp)
- {
- FreeLogLibarary();
- return FALSE;
- }
- return TRUE;
- }
- void FreeLogLibarary()
- {
- if (g_hdll)
- {
- if (FreeLibrary(g_hdll))
- {
- g_hdll = NULL;
- g_IsAppRunning = NULL;
- g_StartIRApp = NULL;
- g_CloseApp = NULL;
- g_Connect = NULL;
- g_getDeviceNames = NULL;
- g_getSignalsName = NULL;
- g_loadSignalDataSet = NULL;
- g_sendSignal = NULL;
- g_sendSignals = NULL;
- g_sendRepeatsSignal = NULL;
- g_DisConnect = NULL;
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- 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 };
- STConfig g_Config;
- //////////////////////////////////////////////////////////////////////////
- // 全局函数;
- /************************************************************************/
- /* 函数:WriteTextLog[7/28/2009 Jeff];
- /* 描述:写文本日志;
- /* 参数:;
- /* [IN] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void WriteTextLog(const TCHAR* format, ...)
- {
- // 解析出日志路径;
- TCHAR szlogpath[MAX_PATH] = { 0 };
- _stprintf_s(szlogpath, _T("%s%s.txt"), g_szCurModuleDir, g_szFna);
- // 打开或创建文件;
- FILE* fp = NULL;
- //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);
- // 将日志内容输入到文件中;
- // 获取今年年份;
- __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
- struct tm gmtm = { 0 };
- localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
- _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);//还原区域设定;
- }
- int ReadReg(char* path, char* key, char* value)
- {
- HKEY hKey;
- int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_QUERY_VALUE, &hKey);
- if (ret != ERROR_SUCCESS)
- {
- return 1;
- }
- //读取KEY
- DWORD dwType = REG_SZ; //数据类型
- DWORD cbData = 256;
- ret = RegQueryValueEx(hKey, key, NULL, &dwType, (LPBYTE)value, &cbData);
- if (ret != ERROR_SUCCESS)
- {
- RegCloseKey(hKey);
- return 1;
- }
- RegCloseKey(hKey);
- return 0;
- }
- // 读取配置文件-预留;
- void GetConfig()
- {
- TCHAR szConfigpath[MAX_PATH] = { 0 };
- _stprintf_s(szConfigpath, _T("%sConfig.ini"), Global::g_szCurModuleDir);
- _tcscpy_s(g_szConfig, szConfigpath);
- TCHAR szValue[MAX_PATH] = { 0 };
- // 读取配置内容;
- g_Config.bGenerics = GetPrivateProfileInt(_T("ir-device"), _T("generics"), 0, szConfigpath);
- GetPrivateProfileString(_T("ir-device"), _T("redratpath"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.redratpath = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("ir-device"), _T("signal"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.signaldir = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("ir-device"), _T("use-signal"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.use_signal = szValue;
- memset(szValue, 0, MAX_PATH);
- }
- BOOL GetOrientation(IN Image* pImg)
- {
- if (pImg == NULL) return FALSE;
- UINT totalBufferSize;
- UINT numProperties;
- pImg->GetPropertySize(&totalBufferSize, &numProperties);
- // Allocate the buffer that will receive the property items.
- PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
- // Fill the buffer.
- pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
- // Print the id data member of each property item.
- for (UINT j = 0; j < numProperties; ++j)
- {
- if (PropertyTagOrientation == pAllItems[j].id)
- {
- short* ptrLong = (short*)(pAllItems[j].value);
- int ret = (int)*ptrLong;
- free(pAllItems);
- return ret;
- }
- }
- free(pAllItems);
- return TRUE;
- }
- /************************************************************************/
- /*
- 函数:GetEncoderClsid
- 描述:获取GDI+支持的图像格式编码器种类,以及所有种类编码器信息;
- 参数:
- IN: format 要获取的图像格式;
- OUT: pClsid 返回符合条件的图像编码器信息;
- 返回:成功返回编码器索引,否则返回-1;
- */
- /************************************************************************/
- int GetEncoderClsid(IN CONST WCHAR* format, OUT CLSID* pClsid)
- {
- // GDI+支持的图像编码器数量;
- UINT numEncoders = 0;
- // GDI+所有图像格式编码器详细信息所需要的空间大小;
- UINT nSize = 0;
- ImageCodecInfo* pImageCodecInfo = NULL;
- // 2.获取GDI+支持的所有图像格式编码器详细信息所需要的空间大小;
- GetImageEncodersSize(&numEncoders, &nSize);
- if (nSize == 0)
- return -1;
- //3.为ImageCodecInfo数组分配足额空间;
- pImageCodecInfo = (ImageCodecInfo*)(malloc(nSize));
- if (pImageCodecInfo == NULL)
- return -1;
- //4.获取所有的图像编码器信息;
- GetImageEncoders(numEncoders, nSize, pImageCodecInfo);
- //5.查找符合的图像编码器的Clsid;
- for (UINT i = 0; i < numEncoders; ++i)
- {
- if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
- {
- *pClsid = pImageCodecInfo[i].Clsid;
- free(pImageCodecInfo);
- return i; // Success
- }
- }
- //6.释放步骤3分配的内存;
- free(pImageCodecInfo);
- return -1;
- }
- BOOL LoadImgFromFile(IN Image** pImg, LPCTSTR lpPath)
- {
- if (!PathFileExists(lpPath))
- return FALSE;
- if (*pImg)
- delete* pImg;
- *pImg = NULL;
- #ifdef UNICODE
- * pImg = Image::FromFile(lpPath);
- #else
- BSTR strtmp = _bstr_t(lpPath);
- *pImg = Image::FromFile(strtmp, TRUE);
- SysFreeString(strtmp);
- #endif
- return (*pImg ? TRUE : FALSE);
- }
- BOOL LoadImgFromBuffer(IN Image** pImg, IN BYTE* pBuffer, IN CONST INT& nBufLen)
- {
- if (pBuffer == NULL)
- return FALSE;
- if (*pImg)
- delete* pImg;
- *pImg = NULL;
- HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
- if (hMemery == NULL)
- return FALSE;
- BYTE* pMem = (BYTE*)GlobalLock(hMemery);
- memcpy(pMem, pBuffer, nBufLen);
- IStream* pstream = NULL;
- CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
- *pImg = Image::FromStream(pstream);
- GlobalUnlock(hMemery);
- pstream->Release();
- return (*pImg ? TRUE : FALSE);
- }
- // 先以只读方式从文件中读取二进制流出来,可以做到不独占文件;
- BOOL LoadImgFromBuffer(IN Image** pImg, LPCTSTR lpPath)
- {
- if (!PathFileExists(lpPath))
- return FALSE;
- if (*pImg)
- delete* pImg;
- *pImg = NULL;
- return LoadImgFromFile(pImg, lpPath);
- CFile fp;
- CFileException e;
- BOOL bRet = FALSE;
- if (fp.Open(lpPath, CFile::modeRead, &e))
- {
- DWORD dwLength = (DWORD)fp.GetLength();
- BYTE* pData = new BYTE[dwLength];
- fp.Read(pData, dwLength);
- fp.Close();
- bRet = LoadImgFromBuffer(pImg, pData, dwLength);
- if (pData)
- delete[]pData;
- }
- return bRet;
- }
- /************************************************************************/
- /* 函数:LoadImgFromResource[9/21/2016 IT];
- /* 描述:从资源中加载;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- Image* LoadImgFromResource(IN HMODULE hModule, IN LPCTSTR lpName, IN LPCTSTR lpType)
- {
- HGLOBAL hGlobal = NULL;
- HRSRC hSource = NULL;
- LPVOID lpBuffer = NULL;
- DWORD dwSize = 0;
- // 1.定位我们的自定义资源,这里因为我们是从本模块定位资源,所以将句柄简单地置为NULL即可
- hSource = FindResource(hModule, lpName, lpType);
- if (hSource == NULL)
- {
- _tprintf(_T("载入资源失败:%s"), lpName);
- return NULL;
- }
- // 2.获取资源的大小;
- dwSize = (UINT)SizeofResource(NULL, hSource);
- // 3.加载资源;
- hGlobal = LoadResource(NULL, hSource);
- if (hGlobal == NULL)
- {
- _tprintf(_T("载入资源失败:%s"), lpName);
- return NULL;
- }
- // 4.锁定资源,获取buffer;
- lpBuffer = LockResource(hGlobal);
- if (lpBuffer == NULL)
- {
- _tprintf(_T("载入资源失败:%s"), lpName);
- return NULL;
- }
- // lpFileFullName需要先判断文件是否存在??不需要;
- Image* pImg = NULL;
- LoadImgFromBuffer(&pImg, (BYTE*)lpBuffer, dwSize);
- UnlockResource(hGlobal);
- FreeResource(hGlobal);
- return pImg;
- }
- BOOL SaveImgByRotate(LPCTSTR lpszFileName, BYTE* pBuffer, const INT& nBufLen, BOOL bHoriontal, BOOL bVertically)
- {
- Image* pImg = NULL;
- HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
- if (hMemery == NULL)
- return FALSE;
- BYTE* pMem = NULL;
- pMem = (BYTE*)GlobalLock(hMemery);
- if (pMem == NULL)
- return FALSE;
- memcpy(pMem, pBuffer, nBufLen);
- IStream* pstream = NULL;
- HRESULT hr = CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
- if (pstream == NULL)
- return FALSE;
- pImg = Image::FromStream(pstream);
- GlobalUnlock(hMemery);
- pstream->Release();
- if (bHoriontal && !bVertically)
- pImg->RotateFlip(RotateNoneFlipX);// 水平翻转;
- else if (bHoriontal && bVertically)
- pImg->RotateFlip(Rotate180FlipNone);// 270度;
- else if (!bHoriontal && bVertically)
- pImg->RotateFlip(Rotate180FlipX);// 垂直翻转;
- CString strNewfile = lpszFileName;
- // 需要判断路径是否存在,不存在创建目录;
- int nIndex = strNewfile.ReverseFind(_T('\\'));
- if (nIndex == -1)
- return FALSE;
- if (!PathFileExists(strNewfile.Left(nIndex)))
- {
- // 如果文件夹不存在,创建;
- SHCreateDirectoryEx(NULL, strNewfile.Left(nIndex), NULL);
- }
- nIndex = strNewfile.ReverseFind(_T('.'));
- if (nIndex == -1)
- return FALSE;
- Status stat = GenericError;
- CLSID encoderClsid = { 0 };
- BSTR newfile = strNewfile.AllocSysString();
- strNewfile = strNewfile.Mid(nIndex + 1);
- if (strNewfile.CompareNoCase(_T("bmp")) == 0)
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else if (strNewfile.CompareNoCase(_T("png")) == 0)
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
- {
- GetEncoderClsid(L"image/jpeg", &encoderClsid);
- EncoderParameters encoderParameters;
- encoderParameters.Count = 1;
- encoderParameters.Parameter[0].Guid = EncoderQuality;
- encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
- encoderParameters.Parameter[0].NumberOfValues = 1;
- // Save the image as a JPEG with quality level 100.
- ULONG uQuality = 100;
- encoderParameters.Parameter[0].Value = &uQuality;
- stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
- }
- if (pImg)
- delete pImg;
- pImg = NULL;
- SysFreeString(newfile);
- return stat == Ok ? TRUE : FALSE;
- }
- void MKDIR(LPCTSTR dir)
- {
- //////////////////////////////////////////////////////////////////////////
- // 创建目录;
- int nleft = 0;
- int nIndex = -1;
- TString strdir = dir;
- 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 != TString::npos)
- {
- if (_tmkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
- {
- WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
- break;
- }
- nleft += nIndex + 1;
- }
- } while (nIndex != -1);
- }
- // hModule 模块句柄 NULL表示当前模块;
- bool GetVersion(OUT WORD* pdwFileVersion, OUT WORD* pdwProductVerion)
- {
- TCHAR szFilePath[MAX_PATH] = { 0 };
- DWORD dwRet = GetModuleFileName(NULL, szFilePath, MAX_PATH);
- if (dwRet == 0)
- return false;
- VS_FIXEDFILEINFO* pVi = NULL;
- DWORD dwHandle = 0;
- int size = GetFileVersionInfoSize(szFilePath, &dwHandle);
- if (size > 0)
- {
- BYTE* buffer = new BYTE[size];
- memset(buffer, 0, size);
- if (GetFileVersionInfo(szFilePath, dwHandle, 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;
- }
- }
|