Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Images in Dll

Images in Dll

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kiranin
    wrote on last edited by
    #1

    in MFC application am using JPEG images which are using as background images for my dialogs. fro the am carrying image filtes with my application instead of that can i set imaged into dll and load the images from dll whenever required?

    S 1 Reply Last reply
    0
    • K kiranin

      in MFC application am using JPEG images which are using as background images for my dialogs. fro the am carrying image filtes with my application instead of that can i set imaged into dll and load the images from dll whenever required?

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Yes. Add them into the resource file. I've added an HTML file (contained in the 'res' sub-directory of my project) to my resources using this entry in the resource (.RC) file:

      ABOUTDIALOG.HTML HTML "res\\aboutdia.htm"

      You could use (for example)

      TEST.JPG JPG "res\\test.jpg"

      You can then use a code fragment like the following to load a resource into memory:

      std::string LoadFileFromResource(HINSTANCE hmodResource, LPCTSTR name, LPCTSTR type)
      {
      if (!type)
      type = ::PathFindExtension(name) + 1;
      HRSRC rsrcFile = ::FindResource(hmodResource, name, type);
      if (!rsrcFile) return std::string();
      HGLOBAL gblFile = ::LoadResource(hmodResource, rsrcFile);
      if (!gblFile) return std::string();
      DWORD sizeFile = ::SizeofResource(hmodResource, rsrcFile);
      if (!sizeFile) return std::string();
      LPVOID filePointer = ::LockResource(gblFile);
      if (filePointer)
      {
      std::string contents((char*)filePointer, sizeFile);
      ::FreeResource(gblFile);
      return contents;
      }
      return std::string();
      }

      [edit]You'd call that function a bit like this:

      std::string loadedJpeg = LoadFileFromResource(resourceDllHandle, _T("TEST.JPG"), _T("JPG"));

      std::string might not be the best result type for you, as you're loading a binary file - I've only loaded text files with this...[/edit] HTH!

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      modified on Thursday, April 2, 2009 5:25 AM

      K 1 Reply Last reply
      0
      • S Stuart Dootson

        Yes. Add them into the resource file. I've added an HTML file (contained in the 'res' sub-directory of my project) to my resources using this entry in the resource (.RC) file:

        ABOUTDIALOG.HTML HTML "res\\aboutdia.htm"

        You could use (for example)

        TEST.JPG JPG "res\\test.jpg"

        You can then use a code fragment like the following to load a resource into memory:

        std::string LoadFileFromResource(HINSTANCE hmodResource, LPCTSTR name, LPCTSTR type)
        {
        if (!type)
        type = ::PathFindExtension(name) + 1;
        HRSRC rsrcFile = ::FindResource(hmodResource, name, type);
        if (!rsrcFile) return std::string();
        HGLOBAL gblFile = ::LoadResource(hmodResource, rsrcFile);
        if (!gblFile) return std::string();
        DWORD sizeFile = ::SizeofResource(hmodResource, rsrcFile);
        if (!sizeFile) return std::string();
        LPVOID filePointer = ::LockResource(gblFile);
        if (filePointer)
        {
        std::string contents((char*)filePointer, sizeFile);
        ::FreeResource(gblFile);
        return contents;
        }
        return std::string();
        }

        [edit]You'd call that function a bit like this:

        std::string loadedJpeg = LoadFileFromResource(resourceDllHandle, _T("TEST.JPG"), _T("JPG"));

        std::string might not be the best result type for you, as you're loading a binary file - I've only loaded text files with this...[/edit] HTH!

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        modified on Thursday, April 2, 2009 5:25 AM

        K Offline
        K Offline
        kiranin
        wrote on last edited by
        #3

        can i load from external dll?

        S 1 Reply Last reply
        0
        • K kiranin

          can i load from external dll?

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Yes, so long as you load the DLL with a LoadLibrary call.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          K 1 Reply Last reply
          0
          • S Stuart Dootson

            Yes, so long as you load the DLL with a LoadLibrary call.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            K Offline
            K Offline
            kiranin
            wrote on last edited by
            #5

            how can i set images in external dll and load into application?

            S 1 Reply Last reply
            0
            • K kiranin

              how can i set images in external dll and load into application?

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              When you build the DLL, embed the jpeg files in the DLL's resources, as I described before. In your executable, call LoadLibrary for the external DLL containing the image files. That'll return the HMODULE you need to pass into the LoadFileFromResource function I gave you the source for. Then you can load the image files from the DLL as desired.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              K 1 Reply Last reply
              0
              • S Stuart Dootson

                When you build the DLL, embed the jpeg files in the DLL's resources, as I described before. In your executable, call LoadLibrary for the external DLL containing the image files. That'll return the HMODULE you need to pass into the LoadFileFromResource function I gave you the source for. Then you can load the image files from the DLL as desired.

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                K Offline
                K Offline
                kiranin
                wrote on last edited by
                #7

                I created MFC dll with JPG images as you explained, but when try to load into exe, FindResource() returning ERROR_RESOURCE_DATA_NOT_FOUND. Can you suggest me what to do (I have an application inc which dialogs and contros skinned with jpg images, so am carrying jpg images with my application, for that want to create dll and load images whenever required), what type of Dll(Win32 or MFC) need to build? IF MFC why it is failing to find resource, if win32 how images can add into dll.

                S 1 Reply Last reply
                0
                • K kiranin

                  I created MFC dll with JPG images as you explained, but when try to load into exe, FindResource() returning ERROR_RESOURCE_DATA_NOT_FOUND. Can you suggest me what to do (I have an application inc which dialogs and contros skinned with jpg images, so am carrying jpg images with my application, for that want to create dll and load images whenever required), what type of Dll(Win32 or MFC) need to build? IF MFC why it is failing to find resource, if win32 how images can add into dll.

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #8

                  Win32 DLL is fine - just create a Win32 DLL project and add the resources. You might have to export a routine for it to make the DLL. For the MFC one - have you got the correct HMODULE for the DLL (use LoadLibrary to get the correct one)

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  K 1 Reply Last reply
                  0
                  • S Stuart Dootson

                    Win32 DLL is fine - just create a Win32 DLL project and add the resources. You might have to export a routine for it to make the DLL. For the MFC one - have you got the correct HMODULE for the DLL (use LoadLibrary to get the correct one)

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    K Offline
                    K Offline
                    kiranin
                    wrote on last edited by
                    #9

                    Ya am loading dll using Loadlibrary, but FindResource is failing. How can we know dll contains images or not? You can see this link Load JPEG images from DLL with LoadResource in Managed C++[^] , here also loading image from dll, the same way i tried but it is failed. any reasons?

                    modified on Saturday, April 4, 2009 9:06 AM

                    S 1 Reply Last reply
                    0
                    • K kiranin

                      Ya am loading dll using Loadlibrary, but FindResource is failing. How can we know dll contains images or not? You can see this link Load JPEG images from DLL with LoadResource in Managed C++[^] , here also loading image from dll, the same way i tried but it is failed. any reasons?

                      modified on Saturday, April 4, 2009 9:06 AM

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #10

                      kiranin wrote:

                      Ya am loading dll using Loadlibrary, but FindResource is failing. How can we know dll contains images or not?

                      Open it in Visual Studio - that should show you if it has resources, including images.

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups