TemplateManager.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "StdAfx.h"
  2. #include "TemplateManager.h"
  3. CTemplateManager::CTemplateManager(void)
  4. {
  5. }
  6. CTemplateManager::~CTemplateManager(void)
  7. {
  8. }
  9. BOOL CTemplateManager::ExtractResourceToFile( LPCTSTR szResourceType ,UINT nIDResource,LPCTSTR szFileName )
  10. {
  11. int dwSize =ExtractResource(szResourceType,nIDResource,NULL);
  12. if(dwSize<=0)return FALSE;
  13. char * pData = new char[dwSize];
  14. dwSize =ExtractResource(szResourceType,nIDResource,pData);
  15. if(dwSize<=0) {delete pData; return FALSE;}
  16. BOOL r =CreateFileWithData(szFileName,pData,dwSize);
  17. delete pData;
  18. return r;
  19. }
  20. BOOL CTemplateManager::CreateFileWithData( LPCTSTR szFileName,void* pData,DWORD dwSize )
  21. {
  22. CFile f;
  23. if(!f.Open(szFileName,CFile::modeCreate|CFile::modeWrite)) return FALSE;
  24. f.Write(pData,dwSize);
  25. f.Close();
  26. return TRUE;
  27. }
  28. int CTemplateManager::ExtractResource( LPCTSTR szResourceType ,UINT nIDResource,void * pData )
  29. {
  30. LPTSTR szResourceName = MAKEINTRESOURCE(LOWORD(nIDResource));
  31. MEMORY_BASIC_INFORMATION m = {0};
  32. VirtualQuery( CTemplateManager::ExtractResource, &m, sizeof(MEMORY_BASIC_INFORMATION));
  33. HMODULE _hinstDLL =(HMODULE) m.AllocationBase;
  34. HRSRC hRes = FindResource(_hinstDLL, szResourceName,szResourceType);
  35. DWORD error = GetLastError();
  36. HGLOBAL hResData;
  37. if (!hRes || !(hResData = LoadResource(_hinstDLL,hRes))){return -1;};// 资源数据的大小
  38. DWORD dwSize = SizeofResource(_hinstDLL,hRes);
  39. if(pData == NULL){return dwSize;}// 资源加锁
  40. PBYTE pSrc = (PBYTE)LockResource(hResData);
  41. if (!pSrc){FreeResource(hResData);return -1;}// 复制数据
  42. CopyMemory(pData,pSrc,dwSize);
  43. // 释放资源
  44. FreeResource(hResData);
  45. return dwSize;
  46. }