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. Finding/Loading resources

Finding/Loading resources

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionlearning
12 Posts 3 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.
  • W will1383

    Hi all, I'm trying to find and load a resource from another application. Here's the code I'm using: HINSTANCE hToDLL; HRSRC hsrc; hsrc = FindResource(GetModuleHandle(app),icon,RT_ICON); where app = "someapp.exe" and icon = "IDI_ICON3" This ALWAYS returns a NULL pointer, so it's always failing and the GetLastError always gives me a: 1813 The specified resource type cannot be found in the image file. ERROR_RESOURCE_TYPE_NOT_FOUND Now I know the resource exists in the application I'm trying to get at, so what am I doing wrong?? Do I need to call LoadLibrary first? I don't think I should have to. Any insights, help, suggestions, comments are greatly appreciated. Thanks! Dan

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    The GetModuleHandle() function retrieves a module handle for the specified module if the file has been mapped into the address space of the calling process. Had you checked the return value of it, rather than immediately calling FindResource(), this might have been evident.

    W 1 Reply Last reply
    0
    • D David Crow

      The GetModuleHandle() function retrieves a module handle for the specified module if the file has been mapped into the address space of the calling process. Had you checked the return value of it, rather than immediately calling FindResource(), this might have been evident.

      W Offline
      W Offline
      will1383
      wrote on last edited by
      #3

      DavidCrow wrote: The GetModuleHandle() function retrieves a module handle for the specified module if the file has been mapped into the address space of the calling process. Had you checked the return value of it, rather than immediately calling FindResource(), this might have been evident. Gotcha, I understand now, but then what do I need to do to map the file into the address space? Call LoadResource? Thanks! Dan

      D 1 Reply Last reply
      0
      • W will1383

        DavidCrow wrote: The GetModuleHandle() function retrieves a module handle for the specified module if the file has been mapped into the address space of the calling process. Had you checked the return value of it, rather than immediately calling FindResource(), this might have been evident. Gotcha, I understand now, but then what do I need to do to map the file into the address space? Call LoadResource? Thanks! Dan

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        HMODULE hModule;
        HRSRC hResource;

        hModule = LoadLibrary(...);
        if (NULL != hModule)
        {
        hResource = FindResource(hModule, ...);
        if (NULL != hResource)
        {
        LoadResource(hResource, ...);
        }

        FreeLibrary(hModule);
        

        }

        W 1 Reply Last reply
        0
        • D David Crow

          HMODULE hModule;
          HRSRC hResource;

          hModule = LoadLibrary(...);
          if (NULL != hModule)
          {
          hResource = FindResource(hModule, ...);
          if (NULL != hResource)
          {
          LoadResource(hResource, ...);
          }

          FreeLibrary(hModule);
          

          }

          W Offline
          W Offline
          will1383
          wrote on last edited by
          #5

          Thanks, I'll try that and report back

          B 1 Reply Last reply
          0
          • W will1383

            Thanks, I'll try that and report back

            B Offline
            B Offline
            basementman
            wrote on last edited by
            #6

            Since the module is an EXE, you may want to use the LoadLibraryEx() function instead, passing LOAD_LIBRARY_AS_ DATAFILE for the Flags parameter. This will prevent any calls to unresolved externals (like DllMain) and prevent the loader from loading any dependent dlls that the EXE imports.

            W 1 Reply Last reply
            0
            • B basementman

              Since the module is an EXE, you may want to use the LoadLibraryEx() function instead, passing LOAD_LIBRARY_AS_ DATAFILE for the Flags parameter. This will prevent any calls to unresolved externals (like DllMain) and prevent the loader from loading any dependent dlls that the EXE imports.

              W Offline
              W Offline
              will1383
              wrote on last edited by
              #7

              Ok, I'm further in my understanding, but still no further in a solution. Here's what I have now: HMODULE hModule; HRSRC hResource; sprintf(buff,"%s.exe",app); hModule = LoadLibraryEx(buff,NULL,LOAD_LIBRARY_AS_DATAFILE); if (NULL != hModule){ hResource = FindResource(hModule,"IDI_ICON3",RT_ICON); if (NULL != hResource) { IUXLogData("the resource exists\n",TRACE1); //LoadResource(hResource, ...); } else if ( NULL == hResource ) { sprintf(buff, "the %s app is not up to date with error %d\n", app, GetLastError()); IUXLogData(buff,IUX_ERROR); } FreeLibrary(hModule); } I'm still getting an 1814 error though. Still unsure why I am. Is the "IDI_ICON3" or the RT_ICON the wrong stuff? the ICON resource for my test is IDI_ICON3 and I'm sure its in the app I'm trying to open, so I'm confused as to why I'm seeing this. Any suggestions, insight, comments are greatly appreciated Thanks! Dan

              B 1 Reply Last reply
              0
              • W will1383

                Ok, I'm further in my understanding, but still no further in a solution. Here's what I have now: HMODULE hModule; HRSRC hResource; sprintf(buff,"%s.exe",app); hModule = LoadLibraryEx(buff,NULL,LOAD_LIBRARY_AS_DATAFILE); if (NULL != hModule){ hResource = FindResource(hModule,"IDI_ICON3",RT_ICON); if (NULL != hResource) { IUXLogData("the resource exists\n",TRACE1); //LoadResource(hResource, ...); } else if ( NULL == hResource ) { sprintf(buff, "the %s app is not up to date with error %d\n", app, GetLastError()); IUXLogData(buff,IUX_ERROR); } FreeLibrary(hModule); } I'm still getting an 1814 error though. Still unsure why I am. Is the "IDI_ICON3" or the RT_ICON the wrong stuff? the ICON resource for my test is IDI_ICON3 and I'm sure its in the app I'm trying to open, so I'm confused as to why I'm seeing this. Any suggestions, insight, comments are greatly appreciated Thanks! Dan

                B Offline
                B Offline
                basementman
                wrote on last edited by
                #8

                You need to wrap the IDI_ICON3 with MakeIntResource as shown in the snippet below: HRSRC hRscDLL = FindResource(NULL,MAKEINTRESOURCE(IDI_ICON3),RT_ICON); if (hRscDLL) { DWORD dwIconSize = SizeofResource(NULL,hRscDLL); HGLOBAL hMem = LoadResource(NULL,hRscDLL); if (hMem) { LPVOID pIconBytes = LockResource(hMem); if (pIconBytes) { .........

                W 1 Reply Last reply
                0
                • B basementman

                  You need to wrap the IDI_ICON3 with MakeIntResource as shown in the snippet below: HRSRC hRscDLL = FindResource(NULL,MAKEINTRESOURCE(IDI_ICON3),RT_ICON); if (hRscDLL) { DWORD dwIconSize = SizeofResource(NULL,hRscDLL); HGLOBAL hMem = LoadResource(NULL,hRscDLL); if (hMem) { LPVOID pIconBytes = LockResource(hMem); if (pIconBytes) { .........

                  W Offline
                  W Offline
                  will1383
                  wrote on last edited by
                  #9

                  Still not having any luck :mad: Now i have this: hResource = FindResource(hModule,MAKEINTRESOURCE(IDI_ICON3),RT_ICON); but yet it still comes back as NULL :confused: Here is how it's defined in the .rc file for the app I'm loading: // Icon with lowest ID value placed first to ensure application icon // remains consistent on all systems. IDI_ICON3 ICON DISCARDABLE "Eye.ico" So what the heck am I doing wrong? Any other ideas, suggestions, comments? You're helping alot so far. thanks! Dan

                  D 1 Reply Last reply
                  0
                  • W will1383

                    Still not having any luck :mad: Now i have this: hResource = FindResource(hModule,MAKEINTRESOURCE(IDI_ICON3),RT_ICON); but yet it still comes back as NULL :confused: Here is how it's defined in the .rc file for the app I'm loading: // Icon with lowest ID value placed first to ensure application icon // remains consistent on all systems. IDI_ICON3 ICON DISCARDABLE "Eye.ico" So what the heck am I doing wrong? Any other ideas, suggestions, comments? You're helping alot so far. thanks! Dan

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #10

                    Does IDI_ICON3 equate to the same number in both applications?

                    W 1 Reply Last reply
                    0
                    • D David Crow

                      Does IDI_ICON3 equate to the same number in both applications?

                      W Offline
                      W Offline
                      will1383
                      wrote on last edited by
                      #11

                      DavidCrow wrote: Does IDI_ICON3 equate to the same number in both applications? :laugh: good question. But yes they are the same number. I made sure they were. The number is 104, and I made sure that the number is the same. any other ideas :confused: Thanks! Dan

                      W 1 Reply Last reply
                      0
                      • W will1383

                        DavidCrow wrote: Does IDI_ICON3 equate to the same number in both applications? :laugh: good question. But yes they are the same number. I made sure they were. The number is 104, and I made sure that the number is the same. any other ideas :confused: Thanks! Dan

                        W Offline
                        W Offline
                        will1383
                        wrote on last edited by
                        #12

                        Woo Hoo! I finally understand it! Turns out that I need to be using the RT_GROUP_ICON because I'm not using hardware-dependent icons. :) I was then able to try other resource types and successfully got the information I needed. Thanks Guys! I really appreciate the help! Dan

                        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