TemplateManager.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "StdAfx.h"
  2. #include "TemplateManager.h"
  3. CTemplateManager g_TemplateManager;
  4. CTemplateManager::CTemplateManager(void)
  5. {
  6. InitializeCriticalSection(&m_lock);
  7. }
  8. CTemplateManager::~CTemplateManager(void)
  9. {
  10. DeleteCriticalSection(&m_lock);
  11. }
  12. int CTemplateManager::ExtractResource( LPCTSTR szResourceType ,UINT nIDResource,void * pData )
  13. {
  14. LPTSTR szResourceName = MAKEINTRESOURCE(LOWORD(nIDResource));
  15. MEMORY_BASIC_INFORMATION m = { 0 };
  16. typedef int MyFun(LPCTSTR, UINT, void *);
  17. VirtualQuery((MyFun*)&CTemplateManager::ExtractResource, &m, sizeof(MEMORY_BASIC_INFORMATION));
  18. HMODULE _hinstDLL = (HMODULE)m.AllocationBase;
  19. HRSRC hRes = FindResource(_hinstDLL, szResourceName,szResourceType);
  20. HGLOBAL hResData;
  21. if (!hRes || !(hResData = LoadResource(_hinstDLL,hRes))){return -1;};// 资源数据的大小
  22. DWORD dwSize = SizeofResource(_hinstDLL,hRes);
  23. if(pData == NULL){return dwSize;}// 资源加锁
  24. PBYTE pSrc = (PBYTE)LockResource(hResData);
  25. if (!pSrc){FreeResource(hResData);return -1;}// 复制数据
  26. CopyMemory(pData,pSrc,dwSize);
  27. // 释放资源
  28. FreeResource(hResData);
  29. return dwSize;
  30. }
  31. std::string CTemplateManager::ExtractResource(LPCTSTR szResourceType, UINT nIDResource)
  32. {
  33. int dwSize = ExtractResource(szResourceType, nIDResource, 0);
  34. std::string result;
  35. result.resize(dwSize);
  36. ExtractResource(szResourceType, nIDResource, (char *)result.data());
  37. return result;
  38. }