Click here to Skip to main content
11,802,762 members (70,282 online)
Click here to Skip to main content

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

, 12 Mar 2007 CPOL 39.6K 1K 15
Rate this:
Please Sign up or sign in to vote.
Load JPEG images from DLL with LoadResource in Managed C++

Screenshot - screenshot.png


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:

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:

  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)

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:


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);
        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

//Set the position for read the stream
    stream->Position = 0;

//Free allocated resources

//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;


//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;
    MessageBox::Show("Sorry... Image cannot be loaded...","Not found",
                    MessageBoxButtons::OK, MessageBoxIcon::Warning);


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


About the Author

Giuseppe Pischedda
Software Developer (Senior)
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151002.1 | Last Updated 13 Mar 2007
Article Copyright 2007 by Giuseppe Pischedda
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid