1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- #include "StdAfx.h"
- #include "TemplateManager.h"
- CTemplateManager::CTemplateManager(void)
- {
- }
- CTemplateManager::~CTemplateManager(void)
- {
- }
- BOOL CTemplateManager::ExtractResourceToFile( LPCTSTR szResourceType ,UINT nIDResource,LPCTSTR szFileName )
- {
- int dwSize =ExtractResource(szResourceType,nIDResource,NULL);
- if(dwSize<=0)return FALSE;
- char * pData = new char[dwSize];
- dwSize =ExtractResource(szResourceType,nIDResource,pData);
- if(dwSize<=0) {delete pData; return FALSE;}
- BOOL r =CreateFileWithData(szFileName,pData,dwSize);
- delete pData;
- return r;
- }
- BOOL CTemplateManager::CreateFileWithData( LPCTSTR szFileName,void* pData,DWORD dwSize )
- {
- CFile f;
- if(!f.Open(szFileName,CFile::modeCreate|CFile::modeWrite)) return FALSE;
- f.Write(pData,dwSize);
- f.Close();
- return TRUE;
- }
- int CTemplateManager::ExtractResource( LPCTSTR szResourceType ,UINT nIDResource,void * pData )
- {
- LPTSTR szResourceName = MAKEINTRESOURCE(LOWORD(nIDResource));
- MEMORY_BASIC_INFORMATION m = {0};
- VirtualQuery( CTemplateManager::ExtractResource, &m, sizeof(MEMORY_BASIC_INFORMATION));
- HMODULE _hinstDLL =(HMODULE) m.AllocationBase;
- HRSRC hRes = FindResource(_hinstDLL, szResourceName,szResourceType);
- DWORD error = GetLastError();
- 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;
- }
|