common_api.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. #include "stdafx.h"
  2. #include <time.h>
  3. #include "common_api.h"
  4. common_api::common_api(void)
  5. {
  6. m_NotifyEvent = CreateMutexA(NULL, FALSE, NULL);
  7. if (m_NotifyEvent == NULL)
  8. {
  9. printf("CreateMutex error: %d\n", GetLastError());
  10. return;
  11. }
  12. }
  13. common_api::~common_api(void)
  14. {
  15. CloseHandle(m_NotifyEvent);
  16. }
  17. common_api g_common1_for_init;
  18. bool string_2_guid(const char *pstr, GUID &stGuid)
  19. {
  20. string temp;
  21. vector<string> thelist;
  22. if (SplitTo84422222222String(pstr, thelist) == false)
  23. {
  24. return false;
  25. }
  26. temp = "0x";
  27. temp += thelist[0].c_str();
  28. StrToIntT(temp.c_str(), &stGuid.Data1);
  29. //sscanf_s(thelist[0].c_str(), "%08x", &stGuid.Data1);
  30. temp = "0x";
  31. temp += thelist[1].c_str();
  32. StrToIntT(temp.c_str(), &stGuid.Data2);
  33. //sscanf_s(thelist[1].c_str(), "%04x", &stGuid.Data2);
  34. temp = "0x";
  35. temp += thelist[2].c_str();
  36. StrToIntT(temp.c_str(), &stGuid.Data3);
  37. //sscanf_s(thelist[2].c_str(), "%04x", &stGuid.Data3);
  38. for (size_t i = 3; i < 11; i++)
  39. {
  40. //sscanf_s(thelist[i].c_str(), "%02x", &stGuid.Data4[i - 3],2);
  41. temp = "0x";
  42. temp += thelist[i].c_str();
  43. StrToIntT(temp.c_str(), &stGuid.Data4[i - 3]);
  44. }
  45. return true;
  46. }
  47. bool guid_2_string(GUID &stGuid, string &str)
  48. {
  49. char szBuff[MAX_PATH] = { 0 };
  50. sprintf_s(szBuff,"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", stGuid.Data1, stGuid.Data2, stGuid.Data3,
  51. stGuid.Data4[0], stGuid.Data4[1], stGuid.Data4[2], stGuid.Data4[3],
  52. stGuid.Data4[4], stGuid.Data4[5], stGuid.Data4[6], stGuid.Data4[7]);
  53. str = szBuff;
  54. return true;
  55. }
  56. bool AddEnvPath(const char *pPath)
  57. {
  58. DWORD PathLen = 32768;
  59. char *pszPath = new char[PathLen];
  60. DWORD Len = GetEnvironmentVariable("Path", pszPath, PathLen);
  61. if (Len > PathLen)
  62. {
  63. delete[]pszPath;
  64. return false;
  65. }
  66. std::string::size_type pos = 0;
  67. string EnvPath = pszPath;
  68. delete[]pszPath;
  69. //EnvPath.find(pPath, 0);
  70. if ((pos = EnvPath.find(pPath, pos)) == std::string::npos)
  71. {
  72. string NewPath = EnvPath;
  73. NewPath += ";";
  74. NewPath += pPath;
  75. if (SetEnvironmentVariable("Path", NewPath.c_str()) == FALSE)
  76. {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. bool DelEnvPath(const char* pPath)
  83. {
  84. DWORD PathLen = 32768;
  85. char* pszPath = new char[PathLen];
  86. DWORD Len = GetEnvironmentVariable("Path", pszPath, PathLen);
  87. if (Len > PathLen)
  88. {
  89. delete[]pszPath;
  90. return false;
  91. }
  92. std::string::size_type pos = 0;
  93. string EnvPath = pszPath;
  94. delete[]pszPath;
  95. //EnvPath.find(pPath, 0);
  96. if ((pos = EnvPath.find(pPath, pos)) > 0)
  97. {
  98. string NewPath = EnvPath;
  99. //NewPath += ";";
  100. //NewPath += pPath;
  101. NewPath.erase(pos, pos + strlen(pPath) + 1);
  102. if (SetEnvironmentVariable("Path", NewPath.c_str()) == FALSE)
  103. {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. __time64_t GetCurrentRealTimeOfStk()
  110. {
  111. __time64_t now;
  112. _time64(&now);
  113. return now;
  114. }
  115. UINT64 GetDay(UINT64 TheTime)
  116. {
  117. struct tm tCur;
  118. _gmtime64_s(&tCur, (__time64_t*)&TheTime);
  119. tCur.tm_sec = 0;
  120. tCur.tm_min = 0;
  121. tCur.tm_hour = 0;
  122. __time64_t CurDay = _mkgmtime64(&tCur);
  123. return (UINT64)CurDay;
  124. }
  125. int CompareTimeByDay(UINT64 Prev, UINT64 Cur)
  126. {
  127. struct tm tPrev,tCur;
  128. _gmtime64_s(&tPrev, (__time64_t*)&Prev);
  129. tPrev.tm_sec = 0;
  130. tPrev.tm_min = 0;
  131. tPrev.tm_hour = 0;
  132. __time64_t PrevDay = _mkgmtime64(&tPrev);
  133. _gmtime64_s(&tCur, (__time64_t*)&Cur);
  134. tCur.tm_sec = 0;
  135. tCur.tm_min = 0;
  136. tCur.tm_hour = 0;
  137. __time64_t CurDay = _mkgmtime64(&tPrev);
  138. if (CurDay == PrevDay)
  139. {
  140. return 0;
  141. }
  142. if (CurDay > PrevDay)
  143. {
  144. return 1;
  145. }
  146. return -1;
  147. }
  148. bool getBusIdFromFilePath(const char *pFilePath,string &BusId)
  149. {
  150. string Path = pFilePath;
  151. size_t readcount = 0;
  152. size_t firstHit = Path.find_first_of('/');
  153. if(firstHit == string::npos || firstHit != 0)
  154. {
  155. return false;
  156. }
  157. size_t SecondHit = Path.find_first_of('/',1);
  158. if(SecondHit == string::npos)
  159. {
  160. BusId = Path.substr(1,Path.size()-1);
  161. }
  162. else
  163. {
  164. BusId = Path.substr(1,SecondHit - 1);
  165. }
  166. if(BusId.size() == 0)
  167. {
  168. return false;
  169. }
  170. return true;
  171. }
  172. void RawToHex(const char *pRaw, DWORD RawLen, string &Hex)
  173. {
  174. char szMsg[4] = { 0 };
  175. Hex.reserve(RawLen * 3);
  176. for (DWORD i = 0; i < RawLen; i++)
  177. {
  178. _snprintf_s(szMsg, 3, "%02X ", pRaw[i]);
  179. Hex += szMsg;
  180. }
  181. }
  182. bool MergeDevicePath(vector<string> &nodes,string &Devpath)
  183. {
  184. Devpath = "";
  185. for (size_t i = 0; i < nodes.size(); i++)
  186. {
  187. if (nodes[i].size() > 0)
  188. {
  189. Devpath += "/";
  190. Devpath += nodes[i];
  191. }
  192. }
  193. return (Devpath.size() > 0);
  194. }
  195. bool SplitStringSub(string &Path, char key, vector<string> &nodes)
  196. {
  197. string node;
  198. size_t readcount = 0;
  199. //nodes.clear();
  200. string::size_type firstHit = Path.find_first_of(key);
  201. if (firstHit == string::npos)
  202. {
  203. if (Path.size() > 0)
  204. {
  205. nodes.push_back(Path);
  206. }
  207. return true;
  208. }
  209. //firstHit += 1;
  210. if (firstHit > 0)
  211. {
  212. node = Path.substr(0, firstHit);
  213. if (node.size() > 0)
  214. {
  215. nodes.push_back(node);
  216. }
  217. }
  218. while (firstHit < Path.size())
  219. {
  220. firstHit += 1;
  221. string::size_type SecondHit = Path.find_first_of(key, firstHit);
  222. if (SecondHit == string::npos)
  223. {
  224. node = Path.substr(firstHit, Path.size() - (firstHit));
  225. if (node.size() > 0)
  226. {
  227. nodes.push_back(node);
  228. }
  229. return true;
  230. }
  231. node = Path.substr(firstHit, SecondHit - (firstHit));
  232. if (node.size() > 0)
  233. {
  234. nodes.push_back(node);
  235. }
  236. firstHit = SecondHit;
  237. }
  238. return (nodes.size() > 0);
  239. }
  240. bool SplitString(const char *pString,string args, vector<string> &nodes)
  241. {
  242. vector<string> req;
  243. vector<string> res;
  244. req.push_back(string(pString));
  245. for (size_t i = 0; i < args.size(); i++)
  246. {
  247. //cut all req by key
  248. for (size_t j = 0; j < req.size(); j++)
  249. {
  250. SplitStringSub(req[j], args[i], res);
  251. }
  252. //clear and reset
  253. req.clear();
  254. req = res;
  255. res.clear();
  256. }
  257. nodes = req;
  258. return true;
  259. }
  260. bool SplitTo84422222222String(const char *pString, vector<string> &nodes)
  261. {
  262. string total;
  263. vector<string> thelist;
  264. SplitString(pString, string("{}-"), thelist);
  265. for (size_t i = 0; i < thelist.size(); i++)
  266. {
  267. total += thelist[i];
  268. }
  269. if (total.size() != 32)
  270. {
  271. return false;
  272. }
  273. string temp;
  274. temp = total.substr(0, 8);//8
  275. nodes.push_back(temp);
  276. temp = total.substr(8, 4);//4
  277. nodes.push_back(temp);
  278. temp = total.substr(12, 4);//4
  279. nodes.push_back(temp);
  280. temp = total.substr(16, 2);//2
  281. nodes.push_back(temp);
  282. temp = total.substr(18, 2);//2
  283. nodes.push_back(temp);
  284. temp = total.substr(20, 2);//2
  285. nodes.push_back(temp);
  286. temp = total.substr(22, 2);//2
  287. nodes.push_back(temp);
  288. temp = total.substr(24, 2);//2
  289. nodes.push_back(temp);
  290. temp = total.substr(26, 2);//2
  291. nodes.push_back(temp);
  292. temp = total.substr(28, 2);//2
  293. nodes.push_back(temp);
  294. temp = total.substr(30, 2);//2
  295. nodes.push_back(temp);
  296. return true;
  297. }
  298. string FormatstdString(const char *fmt, ...)
  299. {
  300. std::string strResult = "";
  301. WaitForSingleObject(g_common1_for_init.m_NotifyEvent, INFINITE);
  302. {
  303. if (NULL != fmt)
  304. {
  305. va_list marker = NULL;
  306. va_start(marker, fmt);
  307. size_t nLength = _vscprintf(fmt, marker) + 1;
  308. std::vector<char> vBuffer(nLength, '\0');
  309. int nWritten = vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
  310. if (nWritten > 0)
  311. {
  312. strResult = &vBuffer[0];
  313. }
  314. va_end(marker);
  315. }
  316. ReleaseMutex(g_common1_for_init.m_NotifyEvent);
  317. }
  318. return strResult;
  319. }
  320. bool SplitDevicePath(const char *pDevicePath, vector<string> &nodes)
  321. {
  322. string node;
  323. string Path = pDevicePath;
  324. size_t readcount = 0;
  325. nodes.clear();
  326. string::size_type firstHit = Path.find_first_of('/');
  327. if (firstHit == string::npos || firstHit != 0)
  328. {
  329. return false;
  330. }
  331. while (firstHit < Path.size())
  332. {
  333. firstHit += 1;
  334. string::size_type SecondHit = Path.find_first_of('/', firstHit);
  335. if (SecondHit == string::npos)
  336. {
  337. node = Path.substr(firstHit, Path.size() - firstHit);
  338. if (node.size() > 0)
  339. {
  340. nodes.push_back(node);
  341. }
  342. return true;
  343. }
  344. node = Path.substr(firstHit, SecondHit - (firstHit));
  345. if (node.size() > 0)
  346. {
  347. nodes.push_back(node);
  348. }
  349. firstHit = SecondHit;
  350. }
  351. return (nodes.size() > 0);
  352. }
  353. string GetProcessDirectory()
  354. {
  355. string ret = "";
  356. char szFilename[MAX_PATH] = { 0 };
  357. DWORD res = GetModuleFileNameA(0, szFilename, MAX_PATH);
  358. if (res == 0)
  359. {
  360. return ret;
  361. }
  362. string fullpath = szFilename;
  363. string::size_type firstHit = fullpath.find_last_of('\\');
  364. if (firstHit == string::npos || firstHit == 0)
  365. {
  366. return ret;
  367. }
  368. ret = fullpath.substr(0, firstHit);//kick last \
  369. return ret;
  370. }
  371. //fullpath must be like X:\filetitle.x.y.z...,or X:/filetitle.x.y.z...
  372. string GetFileTitle(string &fullpath)
  373. {
  374. string ret = "";
  375. string::size_type firstHit = fullpath.find_last_of('\\');
  376. string::size_type firstHitSec = fullpath.find_last_of('/');
  377. if ((firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size())) && (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size())))
  378. {
  379. return ret;
  380. }
  381. if (firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size()))
  382. {
  383. ret = fullpath.substr(firstHitSec + 1);
  384. }
  385. else if (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size()))
  386. {
  387. ret = fullpath.substr(firstHit + 1);
  388. }
  389. else
  390. {
  391. //effective both
  392. if (firstHit > firstHitSec)
  393. {
  394. ret = fullpath.substr(firstHit + 1);//kick last
  395. }
  396. else
  397. {
  398. ret = fullpath.substr(firstHitSec + 1);//kick last
  399. }
  400. }
  401. //got filetitle with ext
  402. if (ret.size() > 0)
  403. {
  404. string::size_type firstHit = ret.find_first_of('.');
  405. ret = ret.substr(0, firstHit);
  406. }
  407. return ret;
  408. }
  409. string GetFileName(string &fullpath)
  410. {
  411. string ret = "";
  412. string::size_type firstHit = fullpath.find_last_of('\\');
  413. string::size_type firstHitSec = fullpath.find_last_of('/');
  414. if ((firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size())) && (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size())))
  415. {
  416. return ret;
  417. }
  418. if (firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size()))
  419. {
  420. ret = fullpath.substr(firstHitSec + 1);
  421. }
  422. else if (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size()))
  423. {
  424. ret = fullpath.substr(firstHit + 1);
  425. }
  426. else
  427. {
  428. //effective both
  429. if (firstHit > firstHitSec)
  430. {
  431. ret = fullpath.substr(firstHit + 1);//kick last
  432. }
  433. else
  434. {
  435. ret = fullpath.substr(firstHitSec + 1);//kick last
  436. }
  437. }
  438. //got filetitle with ext
  439. //if (ret.size() > 0)
  440. //{
  441. // string::size_type ExtHit = ret.find_last_of('.');
  442. // if (ExtHit == 0 || ExtHit == string::npos || ((ExtHit + 1) == fullpath.size()))
  443. // {
  444. // ret = "";
  445. // }
  446. // else
  447. // {
  448. // ret = ret.substr(ExtHit + 1);
  449. // }
  450. //}
  451. return ret;
  452. }
  453. string ReplaceFileTitle(string FilePath, string NewTitle)
  454. {
  455. string::size_type ExtHit = FilePath.find_last_of('.');
  456. if (ExtHit != string::npos)
  457. {
  458. FilePath.replace(ExtHit + 1, string::npos, NewTitle);
  459. }
  460. return FilePath;
  461. }
  462. bool CreateFileDirectory(string &FullFilePath)
  463. {
  464. string path = GetFileDirectory(FullFilePath);
  465. string workpath = GetProcessDirectory() + string("\\");
  466. transform(path.begin(), path.end(), path.begin(), tolower);
  467. transform(workpath.begin(), workpath.end(), workpath.begin(), tolower);
  468. vector<string> folders;
  469. string subpath = ReplaceSubString(path, workpath, string(""));
  470. SplitString(subpath.c_str(), string("/\\"), folders);
  471. for (size_t i = 0; i < folders.size(); i++)
  472. {
  473. workpath += folders[i] + string("\\");
  474. DWORD attr = GetFileAttributes(workpath.c_str());
  475. if (attr == (DWORD)-1)
  476. {
  477. if (CreateDirectory(workpath.c_str(), 0) == 0)
  478. {
  479. return false;
  480. }
  481. }
  482. if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0)
  483. {
  484. return false;
  485. }
  486. }
  487. return true;
  488. }
  489. string GetFileDirectory(string &fullpath)
  490. {
  491. string ret = "";
  492. string::size_type firstHit = fullpath.find_last_of('\\');
  493. string::size_type firstHitSec = fullpath.find_last_of('/');
  494. if ((firstHit == string::npos || firstHit == 0) && (firstHitSec == string::npos || firstHitSec == 0))
  495. {
  496. return ret;
  497. }
  498. if (firstHit == string::npos || firstHit == 0)
  499. {
  500. ret = fullpath.substr(0, firstHitSec);//kick last
  501. }
  502. else if (firstHitSec == string::npos || firstHitSec == 0)
  503. {
  504. ret = fullpath.substr(0, firstHit);//kick last
  505. }
  506. else
  507. {
  508. //effective both
  509. if (firstHit > firstHitSec)
  510. {
  511. ret = fullpath.substr(0, firstHit);//kick last
  512. }
  513. else
  514. {
  515. ret = fullpath.substr(0, firstHitSec);//kick last
  516. }
  517. }
  518. return ret;
  519. }
  520. string ReplaceSubString(string org, string &keystr, string &replacestr)
  521. {
  522. std::string::size_type pos = 0;
  523. std::string::size_type keylen = keystr.size();
  524. std::string::size_type replen = replacestr.size();
  525. while ((pos = org.find(keystr, pos)) != std::string::npos)
  526. {
  527. org.replace(pos, keylen, replacestr);
  528. pos += replen;
  529. }
  530. return org;
  531. }
  532. bool FindSortedSubFiles(string rootFile, vector<string> &filelist)
  533. {
  534. WIN32_FIND_DATA fd;
  535. map<string,FILETIME> sortmap;
  536. ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
  537. if (rootFile.size() == 0)
  538. {
  539. return false;
  540. }
  541. HANDLE hFile;
  542. BOOL bRet = TRUE;
  543. string rootDir = GetFileDirectory(rootFile);
  544. if (rootDir[rootDir.size() - 1] != '\\')
  545. {
  546. rootDir += "\\";
  547. }
  548. string subpath = rootFile;
  549. subpath += ".*";//log file looks like xxx.log.20161107_185132
  550. hFile = FindFirstFile(subpath.c_str(), &fd);
  551. while (hFile != INVALID_HANDLE_VALUE && bRet)
  552. {
  553. if (fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
  554. strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
  555. {
  556. //ignore dir
  557. }
  558. else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
  559. {
  560. //ignore it
  561. }
  562. else
  563. {
  564. //full file name
  565. subpath = rootDir;
  566. subpath += fd.cFileName;
  567. sortmap[subpath] = fd.ftLastWriteTime;
  568. }
  569. bRet = FindNextFile(hFile, &fd);
  570. }
  571. FindClose(hFile);
  572. //sort it
  573. while (sortmap.size() > 0)
  574. {
  575. ULARGE_INTEGER fTime1 = { 0 };
  576. map<string, FILETIME>::iterator itermax;
  577. map<string, FILETIME>::iterator iter = sortmap.begin();
  578. while (iter != sortmap.end())
  579. {
  580. ULARGE_INTEGER *pfTime2 = (ULARGE_INTEGER *)&(iter->second);
  581. if (fTime1.QuadPart < (*pfTime2).QuadPart)
  582. {
  583. itermax = iter;
  584. }
  585. ++iter;
  586. }
  587. //got bigest one
  588. filelist.push_back(itermax->first);
  589. //del iter
  590. sortmap.erase(itermax);
  591. }
  592. return (filelist.size() > 0);
  593. }
  594. bool FindSubVersionDirs(string rootDir, map<string,vector<string>>& dirlist)
  595. {
  596. WIN32_FIND_DATA fd;
  597. ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
  598. if (rootDir.size() == 0)
  599. {
  600. return false;
  601. }
  602. HANDLE hFile;
  603. BOOL bRet = TRUE;
  604. if (rootDir[rootDir.size() - 1] != '\\')
  605. {
  606. rootDir += "\\";
  607. }
  608. string subpath = rootDir;
  609. //define the format of the basepath
  610. subpath += "*";
  611. hFile = FindFirstFile(subpath.c_str(), &fd);
  612. while (hFile != INVALID_HANDLE_VALUE && bRet)
  613. {
  614. if ((fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) &&
  615. strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
  616. {
  617. string TheDir = rootDir;
  618. TheDir += fd.cFileName;
  619. dirlist[fd.cFileName].push_back(TheDir);
  620. }
  621. else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
  622. {
  623. //ignore it
  624. }
  625. else
  626. {
  627. //ignore it
  628. }
  629. bRet = FindNextFile(hFile, &fd);
  630. }
  631. FindClose(hFile);
  632. return (dirlist.size() > 0);
  633. }
  634. bool FindSubFiles(string rootDir, vector<string> &filelist,bool Recursive, string strWildcard)
  635. {
  636. WIN32_FIND_DATA fd;
  637. ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
  638. if (rootDir.size() == 0)
  639. {
  640. return false;
  641. }
  642. HANDLE hFile;
  643. BOOL bRet = TRUE;
  644. if (rootDir[rootDir.size() - 1] != '\\')
  645. {
  646. rootDir += "\\";
  647. }
  648. string subpath = rootDir;
  649. //define the format of the basepath
  650. subpath += strWildcard;
  651. hFile = FindFirstFile(subpath.c_str(), &fd);
  652. while (hFile != INVALID_HANDLE_VALUE && bRet)
  653. {
  654. if ((fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) &&
  655. strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
  656. {
  657. if (Recursive)
  658. {
  659. subpath = rootDir;
  660. subpath += fd.cFileName;
  661. FindSubFiles(subpath, filelist);
  662. }
  663. }
  664. else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
  665. {
  666. //ignore it
  667. }
  668. else
  669. {
  670. //full file name
  671. subpath = rootDir;
  672. subpath += fd.cFileName;
  673. filelist.push_back(subpath);
  674. }
  675. bRet = FindNextFile(hFile, &fd);
  676. }
  677. FindClose(hFile);
  678. return (filelist.size() > 0);
  679. }
  680. string & makeLowerStr(string & src)
  681. {
  682. size_t len = src.size();
  683. for (size_t i = 0; i < len; i++)
  684. {
  685. src.at(i) = tolower(src.at(i));
  686. }
  687. return src;
  688. }
  689. std::vector<export_functions> GetDllFunctionInfo(const char* moduleName)
  690. {
  691. HMODULE hModule = LoadLibraryA(moduleName);
  692. if (hModule == nullptr)
  693. return {};
  694. const IMAGE_DOS_HEADER* pDosHeader = reinterpret_cast<IMAGE_DOS_HEADER*>(hModule);
  695. const IMAGE_NT_HEADERS* pNtHeader = reinterpret_cast<IMAGE_NT_HEADERS*>(reinterpret_cast<BYTE*>(hModule) + pDosHeader->e_lfanew);
  696. const IMAGE_EXPORT_DIRECTORY* pExportDirectory = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(reinterpret_cast<BYTE*>(
  697. hModule) + pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
  698. const DWORD* pAddressOfFunctions = reinterpret_cast<DWORD*>(reinterpret_cast<BYTE*>(hModule) + pExportDirectory->
  699. AddressOfFunctions);
  700. const DWORD* pAddressOfNames = reinterpret_cast<DWORD*>(reinterpret_cast<BYTE*>(hModule) + pExportDirectory->AddressOfNames);
  701. const WORD* pAddressOfNameOrdinals = reinterpret_cast<WORD*>(reinterpret_cast<BYTE*>(hModule) + pExportDirectory->AddressOfNameOrdinals);
  702. // 返回格式 [[函数名, 函数地址, 函数序号], ...]
  703. std::vector<export_functions> result;
  704. for (DWORD i = 0; i < pExportDirectory->NumberOfNames; i++)
  705. {
  706. const char* pName = reinterpret_cast<char*>(reinterpret_cast<BYTE*>(hModule) + pAddressOfNames[i]);
  707. const DWORD dwAddress = pAddressOfFunctions[pAddressOfNameOrdinals[i]];
  708. const DWORD dwOrdinal = pAddressOfNameOrdinals[i] + pExportDirectory->Base;
  709. // 根据自身需求,选择是否直接函数内打印
  710. // std::cout << "Function Name: " << pName << std::endl;
  711. // std::cout << "Function Address: " << dwAddress << std::endl;
  712. // std::cout << "Function Ordinal: " << dwOrdinal << std::endl;
  713. // std::cout << std::endl;
  714. result.push_back(std::make_tuple( pName, dwAddress, dwOrdinal) );
  715. }
  716. return result;
  717. }