12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007 |
- #include "stdafx.h"
- #include <time.h>
- #include "common_api.h"
- common_api::common_api(void)
- {
- m_NotifyEvent = CreateMutexA(NULL, FALSE, NULL);
- if (m_NotifyEvent == NULL)
- {
- printf("CreateMutex error: %d\n", GetLastError());
- return;
- }
- }
- common_api::~common_api(void)
- {
- CloseHandle(m_NotifyEvent);
- }
- common_api g_common1_for_init;
- bool string_2_guid(const char *pstr, GUID &stGuid)
- {
- string temp;
- vector<string> thelist;
- if (SplitTo84422222222String(pstr, thelist) == false)
- {
- return false;
- }
- temp = "0x";
- temp += thelist[0].c_str();
- StrToIntT(temp.c_str(), &stGuid.Data1);
- //sscanf_s(thelist[0].c_str(), "%08x", &stGuid.Data1);
- temp = "0x";
- temp += thelist[1].c_str();
- StrToIntT(temp.c_str(), &stGuid.Data2);
- //sscanf_s(thelist[1].c_str(), "%04x", &stGuid.Data2);
- temp = "0x";
- temp += thelist[2].c_str();
- StrToIntT(temp.c_str(), &stGuid.Data3);
- //sscanf_s(thelist[2].c_str(), "%04x", &stGuid.Data3);
- for (size_t i = 3; i < 11; i++)
- {
- //sscanf_s(thelist[i].c_str(), "%02x", &stGuid.Data4[i - 3],2);
- temp = "0x";
- temp += thelist[i].c_str();
- StrToIntT(temp.c_str(), &stGuid.Data4[i - 3]);
- }
- return true;
- }
- bool guid_2_string(GUID &stGuid, string &str)
- {
- char szBuff[MAX_PATH] = { 0 };
- sprintf_s(szBuff,"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", stGuid.Data1, stGuid.Data2, stGuid.Data3,
- stGuid.Data4[0], stGuid.Data4[1], stGuid.Data4[2], stGuid.Data4[3],
- stGuid.Data4[4], stGuid.Data4[5], stGuid.Data4[6], stGuid.Data4[7]);
- str = szBuff;
- return true;
- }
- bool AddEnvPath(const char *pPath)
- {
- DWORD PathLen = 32768;
- char *pszPath = new char[PathLen];
- DWORD Len = GetEnvironmentVariable("Path", pszPath, PathLen);
- if (Len > PathLen)
- {
- delete[]pszPath;
- return false;
- }
- std::string::size_type pos = 0;
- string EnvPath = pszPath;
- delete[]pszPath;
- //EnvPath.find(pPath, 0);
- if ((pos = EnvPath.find(pPath, pos)) == std::string::npos)
- {
- string NewPath = EnvPath;
- NewPath += ";";
- NewPath += pPath;
- if (SetEnvironmentVariable("Path", NewPath.c_str()) == FALSE)
- {
- return false;
- }
- }
- return true;
- }
- bool DelEnvPath(const char* pPath)
- {
- DWORD PathLen = 32768;
- char* pszPath = new char[PathLen];
- DWORD Len = GetEnvironmentVariable("Path", pszPath, PathLen);
- if (Len > PathLen)
- {
- delete[]pszPath;
- return false;
- }
- std::string::size_type pos = 0;
- string EnvPath = pszPath;
- delete[]pszPath;
- //EnvPath.find(pPath, 0);
- if ((pos = EnvPath.find(pPath, pos)) > 0)
- {
- string NewPath = EnvPath;
- //NewPath += ";";
- //NewPath += pPath;
- NewPath.erase(pos, pos + strlen(pPath) + 1);
- if (SetEnvironmentVariable("Path", NewPath.c_str()) == FALSE)
- {
- return false;
- }
- }
- return true;
- }
- __time64_t GetCurrentRealTimeOfStk()
- {
- __time64_t now;
- _time64(&now);
- return now;
- }
- UINT64 GetDay(UINT64 TheTime)
- {
- struct tm tCur;
- _gmtime64_s(&tCur, (__time64_t*)&TheTime);
- tCur.tm_sec = 0;
- tCur.tm_min = 0;
- tCur.tm_hour = 0;
- __time64_t CurDay = _mkgmtime64(&tCur);
- return (UINT64)CurDay;
- }
- int CompareTimeByDay(UINT64 Prev, UINT64 Cur)
- {
- struct tm tPrev,tCur;
- _gmtime64_s(&tPrev, (__time64_t*)&Prev);
- tPrev.tm_sec = 0;
- tPrev.tm_min = 0;
- tPrev.tm_hour = 0;
- __time64_t PrevDay = _mkgmtime64(&tPrev);
- _gmtime64_s(&tCur, (__time64_t*)&Cur);
- tCur.tm_sec = 0;
- tCur.tm_min = 0;
- tCur.tm_hour = 0;
- __time64_t CurDay = _mkgmtime64(&tPrev);
- if (CurDay == PrevDay)
- {
- return 0;
- }
- if (CurDay > PrevDay)
- {
- return 1;
- }
- return -1;
- }
- bool getBusIdFromFilePath(const char *pFilePath,string &BusId)
- {
- string Path = pFilePath;
- size_t readcount = 0;
- size_t firstHit = Path.find_first_of('/');
- if(firstHit == string::npos || firstHit != 0)
- {
- return false;
- }
- size_t SecondHit = Path.find_first_of('/',1);
- if(SecondHit == string::npos)
- {
- BusId = Path.substr(1,Path.size()-1);
- }
- else
- {
- BusId = Path.substr(1,SecondHit - 1);
- }
- if(BusId.size() == 0)
- {
- return false;
- }
- return true;
- }
- void RawToHex(const char *pRaw, DWORD RawLen, string &Hex)
- {
- char szMsg[4] = { 0 };
- Hex.reserve(RawLen * 3);
- for (DWORD i = 0; i < RawLen; i++)
- {
- _snprintf_s(szMsg, 3, "%02X ", pRaw[i]);
- Hex += szMsg;
- }
- }
- bool MergeDevicePath(vector<string> &nodes,string &Devpath)
- {
- Devpath = "";
- for (size_t i = 0; i < nodes.size(); i++)
- {
- if (nodes[i].size() > 0)
- {
- Devpath += "/";
- Devpath += nodes[i];
- }
- }
- return (Devpath.size() > 0);
- }
- bool SplitStringSub(string &Path, char key, vector<string> &nodes)
- {
- string node;
- size_t readcount = 0;
- //nodes.clear();
- string::size_type firstHit = Path.find_first_of(key);
- if (firstHit == string::npos)
- {
- if (Path.size() > 0)
- {
- nodes.push_back(Path);
- }
- return true;
- }
- //firstHit += 1;
- if (firstHit > 0)
- {
- node = Path.substr(0, firstHit);
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- }
- while (firstHit < Path.size())
- {
- firstHit += 1;
- string::size_type SecondHit = Path.find_first_of(key, firstHit);
- if (SecondHit == string::npos)
- {
- node = Path.substr(firstHit, Path.size() - (firstHit));
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- return true;
- }
- node = Path.substr(firstHit, SecondHit - (firstHit));
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- firstHit = SecondHit;
- }
- return (nodes.size() > 0);
- }
- bool SplitString(const char *pString,string args, vector<string> &nodes)
- {
- vector<string> req;
- vector<string> res;
- req.push_back(string(pString));
- for (size_t i = 0; i < args.size(); i++)
- {
- //cut all req by key
- for (size_t j = 0; j < req.size(); j++)
- {
- SplitStringSub(req[j], args[i], res);
- }
- //clear and reset
- req.clear();
- req = res;
- res.clear();
- }
- nodes = req;
- return true;
- }
- bool SplitTo84422222222String(const char *pString, vector<string> &nodes)
- {
- string total;
- vector<string> thelist;
- SplitString(pString, string("{}-"), thelist);
- for (size_t i = 0; i < thelist.size(); i++)
- {
- total += thelist[i];
- }
- if (total.size() != 32)
- {
- return false;
- }
- string temp;
- temp = total.substr(0, 8);//8
- nodes.push_back(temp);
- temp = total.substr(8, 4);//4
- nodes.push_back(temp);
- temp = total.substr(12, 4);//4
- nodes.push_back(temp);
- temp = total.substr(16, 2);//2
- nodes.push_back(temp);
- temp = total.substr(18, 2);//2
- nodes.push_back(temp);
- temp = total.substr(20, 2);//2
- nodes.push_back(temp);
- temp = total.substr(22, 2);//2
- nodes.push_back(temp);
- temp = total.substr(24, 2);//2
- nodes.push_back(temp);
- temp = total.substr(26, 2);//2
- nodes.push_back(temp);
- temp = total.substr(28, 2);//2
- nodes.push_back(temp);
- temp = total.substr(30, 2);//2
- nodes.push_back(temp);
- return true;
- }
- string FormatstdString(const char *fmt, ...)
- {
- std::string strResult = "";
- WaitForSingleObject(g_common1_for_init.m_NotifyEvent, INFINITE);
- {
- if (NULL != fmt)
- {
- va_list marker = NULL;
- va_start(marker, fmt);
- size_t nLength = _vscprintf(fmt, marker) + 1;
- std::vector<char> vBuffer(nLength, '\0');
- int nWritten = vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
- if (nWritten > 0)
- {
- strResult = &vBuffer[0];
- }
- va_end(marker);
- }
- ReleaseMutex(g_common1_for_init.m_NotifyEvent);
- }
- return strResult;
- }
- bool SplitDevicePath(const char *pDevicePath, vector<string> &nodes)
- {
- string node;
- string Path = pDevicePath;
- size_t readcount = 0;
- nodes.clear();
- string::size_type firstHit = Path.find_first_of('/');
- if (firstHit == string::npos || firstHit != 0)
- {
- return false;
- }
- while (firstHit < Path.size())
- {
- firstHit += 1;
- string::size_type SecondHit = Path.find_first_of('/', firstHit);
- if (SecondHit == string::npos)
- {
- node = Path.substr(firstHit, Path.size() - firstHit);
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- return true;
- }
- node = Path.substr(firstHit, SecondHit - (firstHit));
- if (node.size() > 0)
- {
- nodes.push_back(node);
- }
- firstHit = SecondHit;
- }
- return (nodes.size() > 0);
- }
- bool SplitDeviceList(const char* pDevicePath, vector<string>& nodes)
- {
- string node;
- string Path = pDevicePath;
- size_t readcount = 0;
- nodes.clear();
- string::size_type firstHit = Path.find_first_of(';');
- if (firstHit == string::npos )
- {
- nodes.push_back(pDevicePath);
- return true;
- }
- else
- {
- node = Path.substr(0, (firstHit));
- nodes.push_back(node);
- }
- while (firstHit < Path.size())
- {
- firstHit += 1;
- string::size_type SecondHit = Path.find_first_of(';', firstHit);
- if (SecondHit == string::npos)
- {
- node = Path.substr(firstHit, Path.size() - firstHit);
- nodes.push_back(node);
- return true;
- }
- node = Path.substr(firstHit, SecondHit - (firstHit));
- nodes.push_back(node);
- firstHit = SecondHit;
- }
- return (nodes.size() > 0);
- }
- string GetProcessDirectory()
- {
- string ret = "";
- char szFilename[MAX_PATH] = { 0 };
- DWORD res = GetModuleFileNameA(0, szFilename, MAX_PATH);
- if (res == 0)
- {
- return ret;
- }
- string fullpath = szFilename;
- string::size_type firstHit = fullpath.find_last_of('\\');
- if (firstHit == string::npos || firstHit == 0)
- {
- return ret;
- }
- ret = fullpath.substr(0, firstHit);//kick last \
- return ret;
- }
- //fullpath must be like X:\filetitle.x.y.z...,or X:/filetitle.x.y.z...
- string GetFileTitle(string &fullpath)
- {
- string ret = "";
- string::size_type firstHit = fullpath.find_last_of('\\');
- string::size_type firstHitSec = fullpath.find_last_of('/');
- if ((firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size())) && (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size())))
- {
- return ret;
- }
- if (firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size()))
- {
- ret = fullpath.substr(firstHitSec + 1);
- }
- else if (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size()))
- {
- ret = fullpath.substr(firstHit + 1);
- }
- else
- {
- //effective both
- if (firstHit > firstHitSec)
- {
- ret = fullpath.substr(firstHit + 1);//kick last
- }
- else
- {
- ret = fullpath.substr(firstHitSec + 1);//kick last
- }
- }
- //got filetitle with ext
- if (ret.size() > 0)
- {
- string::size_type firstHit = ret.find_first_of('.');
- ret = ret.substr(0, firstHit);
- }
- return ret;
- }
- string GetFileName(string &fullpath)
- {
- string ret = "";
- string::size_type firstHit = fullpath.find_last_of('\\');
- string::size_type firstHitSec = fullpath.find_last_of('/');
- if ((firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size())) && (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size())))
- {
- return ret;
- }
- if (firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size()))
- {
- ret = fullpath.substr(firstHitSec + 1);
- }
- else if (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size()))
- {
- ret = fullpath.substr(firstHit + 1);
- }
- else
- {
- //effective both
- if (firstHit > firstHitSec)
- {
- ret = fullpath.substr(firstHit + 1);//kick last
- }
- else
- {
- ret = fullpath.substr(firstHitSec + 1);//kick last
- }
- }
- //got filetitle with ext
- //if (ret.size() > 0)
- //{
- // string::size_type ExtHit = ret.find_last_of('.');
- // if (ExtHit == 0 || ExtHit == string::npos || ((ExtHit + 1) == fullpath.size()))
- // {
- // ret = "";
- // }
- // else
- // {
- // ret = ret.substr(ExtHit + 1);
- // }
- //}
- return ret;
- }
- string ReplaceFileTitle(string FilePath, string NewTitle)
- {
- string::size_type ExtHit = FilePath.find_last_of('.');
- if (ExtHit != string::npos)
- {
- FilePath.replace(ExtHit + 1, string::npos, NewTitle);
- }
- return FilePath;
- }
- bool CreateFileDirectory(string &FullFilePath)
- {
- string path = GetFileDirectory(FullFilePath);
- string workpath = GetProcessDirectory() + string("\\");
- transform(path.begin(), path.end(), path.begin(), tolower);
- transform(workpath.begin(), workpath.end(), workpath.begin(), tolower);
- vector<string> folders;
- string subpath = ReplaceSubString(path, workpath, string(""));
- SplitString(subpath.c_str(), string("/\\"), folders);
-
- for (size_t i = 0; i < folders.size(); i++)
- {
- workpath += folders[i] + string("\\");
- DWORD attr = GetFileAttributes(workpath.c_str());
- if (attr == (DWORD)-1)
- {
- if (CreateDirectory(workpath.c_str(), 0) == 0)
- {
- return false;
- }
- }
- if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0)
- {
- return false;
- }
- }
- return true;
- }
- string GetFileDirectory(string &fullpath)
- {
- string ret = "";
- string::size_type firstHit = fullpath.find_last_of('\\');
- string::size_type firstHitSec = fullpath.find_last_of('/');
- if ((firstHit == string::npos || firstHit == 0) && (firstHitSec == string::npos || firstHitSec == 0))
- {
- return ret;
- }
- if (firstHit == string::npos || firstHit == 0)
- {
- ret = fullpath.substr(0, firstHitSec);//kick last
- }
- else if (firstHitSec == string::npos || firstHitSec == 0)
- {
- ret = fullpath.substr(0, firstHit);//kick last
- }
- else
- {
- //effective both
- if (firstHit > firstHitSec)
- {
- ret = fullpath.substr(0, firstHit);//kick last
- }
- else
- {
- ret = fullpath.substr(0, firstHitSec);//kick last
- }
- }
- return ret;
- }
- string ReplaceSubString(string org, string &keystr, string &replacestr)
- {
- std::string::size_type pos = 0;
- std::string::size_type keylen = keystr.size();
- std::string::size_type replen = replacestr.size();
- while ((pos = org.find(keystr, pos)) != std::string::npos)
- {
- org.replace(pos, keylen, replacestr);
- pos += replen;
- }
- return org;
- }
- bool FindSortedSubFiles(string rootFile, vector<string> &filelist)
- {
- WIN32_FIND_DATA fd;
- map<string,FILETIME> sortmap;
- ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
- if (rootFile.size() == 0)
- {
- return false;
- }
- HANDLE hFile;
- BOOL bRet = TRUE;
- string rootDir = GetFileDirectory(rootFile);
- if (rootDir[rootDir.size() - 1] != '\\')
- {
- rootDir += "\\";
- }
- string subpath = rootFile;
- subpath += ".*";//log file looks like xxx.log.20161107_185132
- hFile = FindFirstFile(subpath.c_str(), &fd);
- while (hFile != INVALID_HANDLE_VALUE && bRet)
- {
- if (fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
- strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
- {
- //ignore dir
- }
- else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
- {
- //ignore it
- }
- else
- {
- //full file name
- subpath = rootDir;
- subpath += fd.cFileName;
- sortmap[subpath] = fd.ftLastWriteTime;
- }
- bRet = FindNextFile(hFile, &fd);
- }
- FindClose(hFile);
- //sort it
- while (sortmap.size() > 0)
- {
- ULARGE_INTEGER fTime1 = { 0 };
- map<string, FILETIME>::iterator itermax;
- map<string, FILETIME>::iterator iter = sortmap.begin();
- while (iter != sortmap.end())
- {
- ULARGE_INTEGER *pfTime2 = (ULARGE_INTEGER *)&(iter->second);
- if (fTime1.QuadPart < (*pfTime2).QuadPart)
- {
- itermax = iter;
- }
- ++iter;
- }
- //got bigest one
- filelist.push_back(itermax->first);
- //del iter
- sortmap.erase(itermax);
- }
- return (filelist.size() > 0);
- }
- bool FindSubVersionDirs(string rootDir, map<string,vector<string>>& dirlist)
- {
- WIN32_FIND_DATA fd;
- ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
- if (rootDir.size() == 0)
- {
- return false;
- }
- HANDLE hFile;
- BOOL bRet = TRUE;
- if (rootDir[rootDir.size() - 1] != '\\')
- {
- rootDir += "\\";
- }
- string subpath = rootDir;
- //define the format of the basepath
- subpath += "*";
- hFile = FindFirstFile(subpath.c_str(), &fd);
- while (hFile != INVALID_HANDLE_VALUE && bRet)
- {
- if ((fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) &&
- strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
- {
- string TheDir = rootDir;
- TheDir += fd.cFileName;
- dirlist[fd.cFileName].push_back(TheDir);
- }
- else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
- {
- //ignore it
- }
- else
- {
- //ignore it
- }
- bRet = FindNextFile(hFile, &fd);
- }
- FindClose(hFile);
- return (dirlist.size() > 0);
- }
- bool FindSubFiles(string rootDir, vector<string> &filelist,bool Recursive, string strWildcard)
- {
- WIN32_FIND_DATA fd;
- ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
- if (rootDir.size() == 0)
- {
- return false;
- }
- HANDLE hFile;
- BOOL bRet = TRUE;
- if (rootDir[rootDir.size() - 1] != '\\')
- {
- rootDir += "\\";
- }
- string subpath = rootDir;
- //define the format of the basepath
- subpath += strWildcard;
- hFile = FindFirstFile(subpath.c_str(), &fd);
- while (hFile != INVALID_HANDLE_VALUE && bRet)
- {
- if ((fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) &&
- strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
- {
- if (Recursive)
- {
- subpath = rootDir;
- subpath += fd.cFileName;
- FindSubFiles(subpath, filelist);
- }
- }
- else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
- {
- //ignore it
- }
- else
- {
- //full file name
- subpath = rootDir;
- subpath += fd.cFileName;
- filelist.push_back(subpath);
- }
- bRet = FindNextFile(hFile, &fd);
- }
- FindClose(hFile);
- return (filelist.size() > 0);
- }
- string & makeLowerStr(string & src)
- {
- size_t len = src.size();
- for (size_t i = 0; i < len; i++)
- {
- src.at(i) = tolower(src.at(i));
- }
- return src;
- }
- std::vector<export_functions> GetDllFunctionInfo(const char* moduleName)
- {
- HMODULE hModule = LoadLibraryA(moduleName);
- if (hModule == nullptr)
- return {};
- const IMAGE_DOS_HEADER* pDosHeader = reinterpret_cast<IMAGE_DOS_HEADER*>(hModule);
- const IMAGE_NT_HEADERS* pNtHeader = reinterpret_cast<IMAGE_NT_HEADERS*>(reinterpret_cast<BYTE*>(hModule) + pDosHeader->e_lfanew);
- const IMAGE_EXPORT_DIRECTORY* pExportDirectory = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(reinterpret_cast<BYTE*>(
- hModule) + pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
- const DWORD* pAddressOfFunctions = reinterpret_cast<DWORD*>(reinterpret_cast<BYTE*>(hModule) + pExportDirectory->
- AddressOfFunctions);
- const DWORD* pAddressOfNames = reinterpret_cast<DWORD*>(reinterpret_cast<BYTE*>(hModule) + pExportDirectory->AddressOfNames);
- const WORD* pAddressOfNameOrdinals = reinterpret_cast<WORD*>(reinterpret_cast<BYTE*>(hModule) + pExportDirectory->AddressOfNameOrdinals);
- // 返回格式 [[函数名, 函数地址, 函数序号], ...]
- std::vector<export_functions> result;
- for (DWORD i = 0; i < pExportDirectory->NumberOfNames; i++)
- {
- const char* pName = reinterpret_cast<char*>(reinterpret_cast<BYTE*>(hModule) + pAddressOfNames[i]);
- const DWORD dwAddress = pAddressOfFunctions[pAddressOfNameOrdinals[i]];
- const DWORD dwOrdinal = pAddressOfNameOrdinals[i] + pExportDirectory->Base;
- // 根据自身需求,选择是否直接函数内打印
- // std::cout << "Function Name: " << pName << std::endl;
- // std::cout << "Function Address: " << dwAddress << std::endl;
- // std::cout << "Function Ordinal: " << dwOrdinal << std::endl;
- // std::cout << std::endl;
- result.push_back(std::make_tuple( pName, dwAddress, dwOrdinal) );
- }
- return result;
- }
- void TransferModuleJosnConfig2DriverConfig(ResDataObject& config)
- {
- for (int x = 0; x < config.size(); x++)
- {
- //如果有Value
- if (config[x].GetKeyCount("Value") > 0)
- {
- //FINFO("TRY COVERT [{$}] VALUE {$}", config.GetKey(x), config[x]["Value"].size() > 0 ? config[x]["Value"].encode() : (const char*)config[x]["Value"]);
- if (config[x]["Value"].size() <= 0)
- {
- string va = (const char*)config[x]["Value"];
- config[x] = va.c_str();
- }
- else
- {
- ResDataObject rest = config[x]["Value"];
- config[x] = rest;
- //FINFO("convert object [{$}], object {$}", config.GetKey(x), rest.encode());
- }
- }
- //FINFO("After Convert {$}", config.encode());
- }
- }
- std::string DatetimeToString(time_t timeVal)
- {
- // 将time_t转换为本地时间的tm结构体
- struct tm* localTime = localtime(&timeVal);
- if (localTime == nullptr)
- {
- // 处理转换失败的情况,返回空字符串或错误标识
- return "";
- }
- // 缓冲区,足够容纳格式化后的时间字符串(包含终止符)
- char timeStr[32] = { 0 };
- // 格式化时间字符串:年-月-日 时:分:秒
- // %Y:四位年份,%m:两位月份,%d:两位日期
- // %H:24小时制小时,%M:分钟,%S:秒
- strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localTime);
- return std::string(timeStr);
- }
|