BOOL CGDIPClockDlg::ImageFromIDResource(UINT nID, LPCTSTR sTR,Image * &pImg)
{
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
if (!hRsrc)
return FALSE; // load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return FALSE; // Allocate global memory on which to create stream
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem,lpRsrc,len);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
// load from stream
pImg=Gdiplus::Image::FromStream(pstm); // free/release stuff
GlobalUnlock(m_hMem);
pstm->Release();
FreeResource(lpRsrc);}GlobalUnlock(m_hMem);之后,m_hMem指向的内存就会自动释放么?
是否需要 GlobalFree(pSD); ??
{
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
if (!hRsrc)
return FALSE; // load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return FALSE; // Allocate global memory on which to create stream
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem,lpRsrc,len);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
// load from stream
pImg=Gdiplus::Image::FromStream(pstm); // free/release stuff
GlobalUnlock(m_hMem);
pstm->Release();
FreeResource(lpRsrc);}GlobalUnlock(m_hMem);之后,m_hMem指向的内存就会自动释放么?
是否需要 GlobalFree(pSD); ??
GlobalFree(m_hMem);
是不是就有内存泄露?
WINOLEAPI CreateStreamOnHGlobal(
__in HGLOBAL hGlobal,
__in BOOL fDeleteOnRelease,
__out LPSTREAM* ppstm
);fDeleteOnRelease
A value that indicates whether the underlying handle for this stream object should be automatically freed when the stream object is released. If set to FALSE, the caller must free the hGlobal after the final release. If set to TRUE, the final release will automatically free the hGlobal parameter.如果第二个参数为TRUE,就不需要显示的释放hGlobal
难道 MSDN 说的有问题?
这里第二个参数为 FALSE 啊