Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C++/CLI
Article

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

Rate me:
Please Sign up or sign in to vote.
4.10/5 (3 votes)
12 Mar 2007CPOL2 min read 64.5K   1.6K   17   4
Load JPEG images from DLL with LoadResource in Managed C++

Screenshot - screenshot.png

Introduction

The LoadResource function loads the specified resource in global memory (HGLOBAL) area. It's prototype is:

C++
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.

C++
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.
C++
// code .....
HRSRC rsrc = FindResource(module, MAKEINTRESOURCE(resource_ID), RT_RCDATA); 

We have to get the total size of resource for successive use:

C++
//code...
DWORD Size = SizeofResource(module, rsrc);

Now we can load the specified resource:

C++
//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:

  1. We must to create a cli::array of bytes
  2. We must do a cast of the pointer returned by the LockResource function from LPVOID to char * (LockResource pointer returns the first byte of the resource)
  3. 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)

C++
//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:

C++
//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:

C++
//code...
stream->Position = 0;

Now we can free the allocated resources:

C++
//code...
FreeLibrary(module);

We must create a pointer to the abstract class managed C++ Image:

C++
System::Drawing::Image ^ptrJpg;
ptrJpg = System::Drawing::Image::FromStream(stream);

The complete function

C++
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

C++
//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);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDirectX 10/11 Pin
sleepingduck22-Apr-11 2:14
sleepingduck22-Apr-11 2:14 
GeneralGrazie! Pin
paradiso4212-Apr-10 22:35
paradiso4212-Apr-10 22:35 
Generaldll in windows app Pin
tareqGamal10-Mar-08 3:49
tareqGamal10-Mar-08 3:49 
GeneralRe: dll in windows app Pin
Giuseppe Pischedda10-Mar-08 6:37
Giuseppe Pischedda10-Mar-08 6:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.