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. LoadImage not loading animated cursor from resources when statically linking to MFC

LoadImage not loading animated cursor from resources when statically linking to MFC

Scheduled Pinned Locked Moved C / C++ / MFC
c++debugginghelpannouncementlearning
6 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.
  • P Offline
    P Offline
    Paolo Messina
    wrote on last edited by
    #1

    This is very strange problem because my code works perfectly when dynamically linking to MFC. At some point in my project I use:

    HCURSOR hAniCur = (HCURSOR)LoadImage(AfxGetResourceHandle(),
    MAKEINTRESOURCE(IDR_HOURGLASS), IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR);

    which gives me a valid handle as long as MFC is in a DLL, but returns NULL when I link static MFC libraries. There are no differences between Debug or Release builds, both suffer the same problem. I also tried to change the resource ID of the animated cursor and to use a string instead of a number, but without success. I have reproduced this problem on a little test project if anyone likes the challenge... Thanks in advance, Paolo

    M P 3 Replies Last reply
    0
    • P Paolo Messina

      This is very strange problem because my code works perfectly when dynamically linking to MFC. At some point in my project I use:

      HCURSOR hAniCur = (HCURSOR)LoadImage(AfxGetResourceHandle(),
      MAKEINTRESOURCE(IDR_HOURGLASS), IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR);

      which gives me a valid handle as long as MFC is in a DLL, but returns NULL when I link static MFC libraries. There are no differences between Debug or Release builds, both suffer the same problem. I also tried to change the resource ID of the animated cursor and to use a string instead of a number, but without success. I have reproduced this problem on a little test project if anyone likes the challenge... Thanks in advance, Paolo

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Step through AfxGetResourceHandle() and make sure it's returning the correct HINSTANCE. I've seen that function return unexpected results before, which then causes all sorts of resource headaches.

      P 1 Reply Last reply
      0
      • M Michael Dunn

        Step through AfxGetResourceHandle() and make sure it's returning the correct HINSTANCE. I've seen that function return unexpected results before, which then causes all sorts of resource headaches.

        P Offline
        P Offline
        Paolo Messina
        wrote on last edited by
        #3

        I tried with AfxGetInstanceHandle(), AfxGetResourceHandle() and GetModuleHandle(NULL). They all return the same value, that is 0x00400000, which is right. What's more, loading from a file works just fine with the arguments I pass to the function. It seems like LoadImage can't find the resource, but why? When I link with static libraries I add some resources to my executable, so I thought something was in conflict with the animated cursor resource. I tried with different IDs, but without any luck. I imported the animated cursor as a custom resource, naming that type "ANICURSOR". Is this right? Or I should use something else? I think the correct resource type is RT_ANICURSOR, but how can I add such resources to my project? However, loading the animated cursor with the type I specified works in Debug version. Any other ideas?

        F 1 Reply Last reply
        0
        • P Paolo Messina

          This is very strange problem because my code works perfectly when dynamically linking to MFC. At some point in my project I use:

          HCURSOR hAniCur = (HCURSOR)LoadImage(AfxGetResourceHandle(),
          MAKEINTRESOURCE(IDR_HOURGLASS), IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR);

          which gives me a valid handle as long as MFC is in a DLL, but returns NULL when I link static MFC libraries. There are no differences between Debug or Release builds, both suffer the same problem. I also tried to change the resource ID of the animated cursor and to use a string instead of a number, but without success. I have reproduced this problem on a little test project if anyone likes the challenge... Thanks in advance, Paolo

          P Offline
          P Offline
          Paolo Messina
          wrote on last edited by
          #4

          I discovered what was wrong: if there are static cursor resources (RT_CURSOR) both LoadImage() and LoadCursor() don't look for animated cursor resources (RT_ANICURSOR = 21). Statically linking to MFC involves the inclusion of some MFC resources, among which there are two cursors for context-sensitive help. I've tried adding the animated cursor as a static cursor, but both VC++ IDE and the resource compiler recognize the internal file format. So what should I do, if I don't want to load the cursor from file? Is this a BUG ?? (tested on Win95 and Win2000) Any suggestion would be appreciated... :(

          1 Reply Last reply
          0
          • P Paolo Messina

            This is very strange problem because my code works perfectly when dynamically linking to MFC. At some point in my project I use:

            HCURSOR hAniCur = (HCURSOR)LoadImage(AfxGetResourceHandle(),
            MAKEINTRESOURCE(IDR_HOURGLASS), IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR);

            which gives me a valid handle as long as MFC is in a DLL, but returns NULL when I link static MFC libraries. There are no differences between Debug or Release builds, both suffer the same problem. I also tried to change the resource ID of the animated cursor and to use a string instead of a number, but without success. I have reproduced this problem on a little test project if anyone likes the challenge... Thanks in advance, Paolo

            P Offline
            P Offline
            Paolo Messina
            wrote on last edited by
            #5

            To solve the problem, I thought to load the animated cursor resource and then create the cursor handle with the following code:HRSRC hResInfo = FindResource(NULL, MAKEINTRESOURCE(IDR_HOURGLASS), RT_ANICURSOR); DWORD dwResSize = SizeofResource(NULL, hResInfo); HGLOBAL hRes = LoadResource(NULL, hResInfo); PBYTE pResData = (PBYTE)LockResource(hRes); HCURSOR hAniCur = CreateIconFromResourceEx(pResData, dwResSize, FALSE, 0x00030000, 32, 32, LR_DEFAULTCOLOR); The problem remains :( All the instruction are successful with both types of linkage, but the last returns a valid handle only when dynamically linking to MFC. When I specify to use static MFC libraries, CreateIconFromResourceEx() cease to work and returns NULL. Can anyone tell me why? I tried to debug USER32 code, but it's a suicide without debug symbols. Where can I get debug symbols for Windows 2000? Please help!!

            1 Reply Last reply
            0
            • P Paolo Messina

              I tried with AfxGetInstanceHandle(), AfxGetResourceHandle() and GetModuleHandle(NULL). They all return the same value, that is 0x00400000, which is right. What's more, loading from a file works just fine with the arguments I pass to the function. It seems like LoadImage can't find the resource, but why? When I link with static libraries I add some resources to my executable, so I thought something was in conflict with the animated cursor resource. I tried with different IDs, but without any luck. I imported the animated cursor as a custom resource, naming that type "ANICURSOR". Is this right? Or I should use something else? I think the correct resource type is RT_ANICURSOR, but how can I add such resources to my project? However, loading the animated cursor with the type I specified works in Debug version. Any other ideas?

              F Offline
              F Offline
              Feng Yuan
              wrote on last edited by
              #6

              Convert the image to one of the closest DIB format, and then use StretchDIBits to display it. My guess is that each pixel is a 16-bit grayscale level, so the closest DIB format would be 8-bit grayscale image, or a 256-color DIB with a grayscale color table. Setup a BITMAPINFO structure with a 256-color grayscale color table, convert the image array to a 64x64 BYTE array, and then use StretchDIBits to display it.

              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