Introduction
The LoadResource
function loads the specified resource in global memory (HGLOBAL
) area. It's prototype is:
HGLOBAL LoadResource(HMODULE hmodule, HRSRC hResInfo)
The first parameter (hmodule
) is an executable module (DLL, EXE) that contains the resource. If this parameter is NULL
, it will be assumed that the resource will have to be found in the current process. When the parameter (module EXE/DLL) is specified, it must be loaded with the LoadLibrary
API.
HINSTANCE module = LoadLibrary(module_name);
The second parameter (
hResInfo
) is the Handle of the resource that must be found. This Handle corresponds to the value of return of the function
FindResource
or
FindResourceEx
(see MSDN). If the demanded resource is found, the
FindResource
return value is the position of the resource.
HRSRC rsrc = FindResource(module, MAKEINTRESOURCE(resource_ID), RT_RCDATA);
We have to get the total size of resource for successive use:
DWORD Size = SizeofResource(module, rsrc);
Now we can load the specified resource:
HGLOBAL MemoryHandle = LoadResource(module, rsrc);
To this point if the specified resource has been found and therefore loaded, it is necessary to lock it in memory to obtain a pointer to the first byte of the resource. The pointer is returned from a call to LockResource
. If the resource in memory has been locked, the return
value of the LockResource
API is a pointer (LPVOID
) to the first byte of the resource, else LockResource
returns NULL
. To this point the problem is that the resource has been loaded into the GLOBAL MEMORY area (HGLOBAL
) and in C++/CLI we cannot directly access memory allocated by Win32 API. The solution in 3 steps:
- We must to create a
cli::array
of bytes - We must do a cast of the pointer returned by the
LockResource
function from LPVOID
to char *
(LockResource
pointer return
s the first byte of the resource) - We must to copy with the
Marshal::Copy
method the array of char
(pointed by a char *
pointer, in other words the unmanaged memory pointer) in the cli:array
(the managed array)
cli::array ^MemPtr = gcnew Array (Size + 2);
char * lkr = (char *)(LockResource(MemoryHandle));
Marshal::Copy((IntPtr)lkr, MemPtr, 0, Size);
A stream must therefore be created in memory (MemoryStream
) that must be sufficiently large to allocate the memory to contain all the cli::array
. Therefore we must write in the stream (MemoryStream
) the content of the cli::array
from position 0
:
System::IO::MemoryStream ^stream = gcnew System::IO::MemoryStream(MemPtr);
stream->Write(MemPtr, 0, Size);
After the end of writing, the MemoryStream
pointer is located to last byte. Therefore for a correct assignment we must bring back the position of the stream to position 0
:
stream->Position = 0;
Now we can free the allocated resources:
FreeLibrary(module);
We must create a pointer to the abstract class managed C++ Image:
System::Drawing::Image ^ptrJpg;
ptrJpg = System::Drawing::Image::FromStream(stream);
The complete function
public: Image ^ getImageFromRes(long resource_ID,LPCWSTR module_name)
{
HINSTANCE module = LoadLibrary(module_name);
if(module == NULL)
{
return nullptr;
}
HRSRC rsrc = FindResource(module, MAKEINTRESOURCE(resource_ID),RT_RCDATA);
if(!rsrc)
{
return nullptr;
}
DWORD Size = SizeofResource(module , rsrc);
HGLOBAL MemoryHandle = LoadResource(module,rsrc);
if(MemoryHandle == NULL)
{
return nullptr;
}
cli::array<BYTE> ^MemPtr = gcnew array<BYTE>(Size + 2);
char *lkr = (char *)(LockResource(MemoryHandle));
Marshal::Copy((IntPtr)lkr,MemPtr, 0, Size);
System::IO::MemoryStream ^stream = gcnew System::IO::MemoryStream(MemPtr);
stream->Write(MemPtr,0,Size);
stream->Position = 0;
FreeLibrary(module);
System::Drawing::Image ^ptrJpg;
ptrJpg = System::Drawing::Image::FromStream(stream);
return ptrJpg;
}
Usage
System::Drawing::Image ^jpeg;
jpeg = getImageFromRes(RESOURCE_ID,L"myResources.dll");
if(jpeg != nullptr)
{
pictureBox1->Image = jpeg;
}
else
MessageBox::Show("Sorry... Image cannot be loaded...","Not found",
MessageBoxButtons::OK, MessageBoxIcon::Warning);