CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2002
    Posts
    199

    can i load icon resource by FindResource ...API functions

    HMODULE hModule = ::GetModuleHandle(NULL);
    HRSRC hRes = ::FindResource(hModule, MAKEINTRESOURCE(IDI_TEST), RT_GROUP_ICON); // IDI_TEST is the resource identity of an icon
    HGLOBAL hGlobal = ::LoadResource(hModule, hRes);
    HICON hIcon = HICON(::LockResource(hGlobal));
    dwErr = GetLastError();
    m_hIcon2 = CopyIcon(hIcon);
    dwErr = GetLastError(); //1402 --invalide cursor handle

    why?

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: can i load icon resource by FindResource ...API functions

    Well....do all the other function before that even succeed?

  3. #3
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: can i load icon resource by FindResource ...API functions

    You could have a look at the ::LoadIcon API or CWinApp::LaodIcon member function.
    ... Just saw that LoadIcon was superseded by LoadImage (it has more parameters...).

    and by the way: isn't RT_ICON the resource type of an icon, instead of RT_GROUP_ICON?
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  4. #4
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: can i load icon resource by FindResource ...API functions

    well, i got your error (i think so)
    look at your code:
    Code:
    HMODULE hModule = ::GetModuleHandle(NULL);
    HRSRC hRes = ::FindResource(hModule, MAKEINTRESOURCE(IDI_TEST), RT_GROUP_ICON); // IDI_TEST is the resource identity of an icon 
    HGLOBAL hGlobal = ::LoadResource(hModule, hRes);
    HICON hIcon = HICON(::LockResource(hGlobal));
    dwErr = GetLastError();
    m_hIcon2 = CopyIcon(hIcon);  
    dwErr = GetLastError(); //1402 --invalide cursor handle
    so where is the error:

    LockResource, takjes a handle to a resource and obtains a pointer (LPVOID) to it, so now you could access the memory occupied by your resource byte per byte, for example.

    but you cast LPVOID to HICON and give this to CopyIcon which actually fails. So why? - CopyIcon takes a HICON, but just casting a LPVOID to HICON, doesn't make it actually a HICON, understood.

    so what you have to do is either call LoaIcon/LoadImage to get a HICON or just take the hGlobal and give this to CopyIcon, because hGlobal contains the handle to your icon.

    i hope i could help you!
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  5. #5
    Join Date
    Apr 2002
    Posts
    199

    Re: can i load icon resource by FindResource ...API functions

    Quote Originally Posted by bigBA
    well, i got your error (i think so)
    look at your code: ...
    thanks, but ur way doesn't work .

    i find something from MSDN , here it is

    Code:
    HICON hIcon1;       // icon handle 
    HINSTANCE hExe;     // handle to loaded .EXE file 
    HRSRC hResource;    // handle for FindResource  
    HRSRC hMem;         // handle for LoadResource 
    BYTE *lpResource;   // pointer to resource data  
    int nID;            // ID of resource that best fits current screen 
     
    HDC hdc;        // handle to display context 
     
    // Load the file from which to copy the icon. 
    // Note: LoadLibrary should have a fully explicit path.
    //
    hExe = LoadLibrary("myapp.exe");
    if (hExe == NULL)
    {
        //Error loading module -- fail as securely as possible
    	return;
    }
     
     
    // Find the icon directory whose identifier is 440. 
     
    hResource = FindResource(hExe, 
        MAKEINTRESOURCE(440), 
        RT_GROUP_ICON); 
     
    // Load and lock the icon directory. 
     
    hMem = LoadResource(hExe, hResource); 
     
    lpResource = LockResource(hMem); 
     
    // Get the identifier of the icon that is most appropriate 
    // for the video display. 
     
    nID = LookupIconIdFromDirectoryEx((PBYTE) lpResource, TRUE, 
        CXICON, CYICON, LR_DEFAULTCOLOR); 
    
    // Find the bits for the nID icon. 
     
    hResource = FindResource(hExe, 
        MAKEINTRESOURCE(nID), 
        MAKEINTRESOURCE(RT_ICON)); 
     
    // Load and lock the icon. 
     
    hMem = LoadResource(hExe, hResource); 
     
    lpResource = LockResource(hMem); 
     
    // Create a handle to the icon. 
     
    hIcon1 = CreateIconFromResourceEx((PBYTE) lpResource, 
        SizeofResource(hExe, hResource), TRUE, 0x00030000, 
        CXICON, CYICON, LR_DEFAULTCOLOR); 
     
    // Draw the icon in the client area. 
     
    DrawIcon(hdc, 10, 20, hIcon1);

    then i modifed my code as below
    Code:
    	HMODULE hModule = ::GetModuleHandle(NULL);
    	HRSRC hRes = ::FindResource(hModule, MAKEINTRESOURCE(IDI_TEST), MAKEINTRESOURCE(RT_ICON));  //hRes = 0 
    	DWORD dwErr = GetLastError(); //1814 The specified resource name cannot be found in the image file. 
    	long szLength = ::SizeofResource(hModule, hRes);
    	HGLOBAL hGlobal = ::LoadResource(hModule, hRes);
    but i'm absolutely sure there is an icon resource in my app whose id is IDI_TEST
    Last edited by jianmin jin; December 16th, 2004 at 03:43 AM.

  6. #6
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: can i load icon resource by FindResource ...API functions

    well, ok.
    than switch the type back to RT_GROUP_ICON or lookup in your resource table/tree which type you icon has... maybe you assigned another type id/name instead of RT_GROUP_ICON/RT_ICON.

    second thing is: why are you using GetModuleHandle(NULL)? just set the first parameter of FindResource to NULL, does the same.

    what do you want to do with the icon?
    did you have a look at LoadIcon/LoadImage (LoadIcon is easier to use)?
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  7. #7
    Join Date
    Apr 2002
    Posts
    199

    Re: can i load icon resource by FindResource ...API functions

    Quote Originally Posted by bigBA
    well, ok.
    than switch the type back to RT_GROUP_ICON or lookup in your resource table/tree which type you icon has... maybe you assigned another type id/name instead of RT_GROUP_ICON/RT_ICON.

    second thing is: why are you using GetModuleHandle(NULL)? just set the first parameter of FindResource to NULL, does the same.

    what do you want to do with the icon?
    did you have a look at LoadIcon/LoadImage (LoadIcon is easier to use)?

    yeah, finially i get it worked ,but i'm confused why i should use the RT_GROUP_ICON flag

    here is the code
    Code:
    	HMODULE hModule = ::GetModuleHandle(NULL);
    	HRSRC hRes = ::FindResource(hModule, MAKEINTRESOURCE(IDI_TEST), RT_GROUP_ICON);
    	long szLength = ::SizeofResource(hModule, hRes);
    	HGLOBAL hGlobal = ::LoadResource(hModule, hRes);
    	int nID = LookupIconIdFromDirectoryEx((PBYTE)hGlobal, TRUE, 32, 32, LR_DEFAULTCOLOR); 
    	hRes = ::FindResource(hModule, MAKEINTRESOURCE(nID), MAKEINTRESOURCE(RT_ICON));
    	hGlobal = ::LoadResource(hModule, hRes);
    	BYTE *lpResource = (BYTE*)::LockResource(hGlobal);
    
    	HICON hIcon = CreateIconFromResourceEx((PBYTE) lpResource, 
        szLength, TRUE, 0x00030000, 32, 32, LR_DEFAULTCOLOR); 
    	m_hIcon2 = CopyIcon(hIcon);
    btw, i know there is a api LoadIcon, i just want to learn any other way to do it ,hehe

  8. #8
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: can i load icon resource by FindResource ...API functions

    forget about that NULL thing didn't work for me, too. but i remember that it worked with custom resources, so.. no idea why it is not with icons.

    i attached a sample project.
    but i used AfxGetInstanceHandle() instead of GetModule(NULL).
    Attached Files Attached Files
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  9. #9
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: can i load icon resource by FindResource ...API functions

    well, i see you got it. but i think thats a bit too much code, lets call it overhead , just to load an icon from the resources...

    but i don't know what you want to do with it, so it maybe suitable for
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  10. #10
    Join Date
    Apr 2002
    Posts
    199

    Re: can i load icon resource by FindResource ...API functions

    Quote Originally Posted by bigBA
    forget about that NULL thing didn't work for me, too. but i remember that it worked with custom resources, so.. no idea why it is not with icons.

    i attached a sample project.
    but i used AfxGetInstanceHandle() instead of GetModule(NULL).
    thanks u remind me of the function AfxGetInstanceHandle. i just can't remember it's name ,hehe

  11. #11
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: can i load icon resource by FindResource ...API functions

    well, i'm glad i could help - rate me
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured