#include "StdAfx.h" #include "TemplateManager.h" CTemplateManager g_TemplateManager; CTemplateManager::CTemplateManager(void) { InitializeCriticalSection(&m_lock); } CTemplateManager::~CTemplateManager(void) { DeleteCriticalSection(&m_lock); } int CTemplateManager::ExtractResource( LPCTSTR szResourceType ,UINT nIDResource,void * pData ) { LPTSTR szResourceName = MAKEINTRESOURCE(LOWORD(nIDResource)); MEMORY_BASIC_INFORMATION m = { 0 }; typedef int MyFun(LPCTSTR, UINT, void *); VirtualQuery((MyFun*)&CTemplateManager::ExtractResource, &m, sizeof(MEMORY_BASIC_INFORMATION)); HMODULE _hinstDLL = (HMODULE)m.AllocationBase; HRSRC hRes = FindResource(_hinstDLL, szResourceName,szResourceType); HGLOBAL hResData; if (!hRes || !(hResData = LoadResource(_hinstDLL,hRes))){return -1;};// 资源数据的大小 DWORD dwSize = SizeofResource(_hinstDLL,hRes); if(pData == NULL){return dwSize;}// 资源加锁 PBYTE pSrc = (PBYTE)LockResource(hResData); if (!pSrc){FreeResource(hResData);return -1;}// 复制数据 CopyMemory(pData,pSrc,dwSize); // 释放资源 FreeResource(hResData); return dwSize; } std::string CTemplateManager::ExtractResource(LPCTSTR szResourceType, UINT nIDResource) { int dwSize = ExtractResource(szResourceType, nIDResource, 0); std::string result; result.resize(dwSize); ExtractResource(szResourceType, nIDResource, (char *)result.data()); return result; }