Load JPEG images from DLL with LoadResource in Managed C++






4.10/5 (3 votes)
Load JPEG images from DLL with LoadResource in Managed C++
Introduction
The LoadResource
function loads the specified resource in global memory (HGLOBAL
) area. It's prototype is:
HGLOBAL LoadResource(HMODULE hmodule, HRSRC hResInfo)
//See MSDN for more information.
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.
// code .....
HRSRC rsrc = FindResource(module, MAKEINTRESOURCE(resource_ID), RT_RCDATA);
We have to get the total size of resource for successive use:
//code...
DWORD Size = SizeofResource(module, rsrc);
Now we can load the specified resource:
//code...
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 fromLPVOID
tochar *
(LockResource
pointerreturn
s the first byte of the resource) - We must to copy with the
Marshal::Copy
method the array ofchar
(pointed by achar *
pointer, in other words the unmanaged memory pointer) in thecli:array
(the managed array)
//code...
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
:
//code...
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
:
//code...
stream->Position = 0;
Now we can free the allocated resources:
//code...
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)
{
// Function getImageFromRes
// A function for loading jpeg images from resources in C++/CLI
// Copyright (C) Giuseppe Pischedda 2007
//Load the resource module:
HINSTANCE module = LoadLibrary(module_name);
if(module == NULL)
{
return nullptr;
}
// Find the resource using the resource ID from file "resource.rh"
HRSRC rsrc = FindResource(module, MAKEINTRESOURCE(resource_ID),RT_RCDATA);
if(!rsrc)
{
return nullptr;
}
// Load the resource and save the total size.
DWORD Size = SizeofResource(module , rsrc);
HGLOBAL MemoryHandle = LoadResource(module,rsrc);
if(MemoryHandle == NULL)
{
return nullptr;
}
//Create a cli::array of byte with size = total size + 2
cli::array<BYTE> ^MemPtr = gcnew array<BYTE>(Size + 2);
//Cast from LPVOID to char *
char *lkr = (char *)(LockResource(MemoryHandle));
//Copy from unmanaged memory to managed array
Marshal::Copy((IntPtr)lkr,MemPtr, 0, Size);
// Create a new MemoryStream with size = MemPtr
System::IO::MemoryStream ^stream = gcnew System::IO::MemoryStream(MemPtr);
//Write in the MemoryStream
stream->Write(MemPtr,0,Size);
//Set the position for read the stream
stream->Position = 0;
//Free allocated resources
FreeLibrary(module);
//Create an Image abstract class pointer
System::Drawing::Image ^ptrJpg;
//Assign the stream to abstract class pointer
ptrJpg = System::Drawing::Image::FromStream(stream);
return ptrJpg;
}
Usage
//In an event handler (a button_click for example):
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);