CImageList::Add

I'm having big problems with loading an icon from other exe files. I'm trying to load the display icon from, for example winzip. This is the code I am using:

HMODULE hFile = LoadLibraryEx (_T("C:\\PROGRA~1\\WINZIP\\winzip32.exe"), NULL, LOAD_LIBRARY_AS_DATAFILE);

HRSRC hRes = FindResource(hFile, MAKEINTRESOURCE(3), RT_ICON);

HICON hIcon = (HICON) LoadResource( hFile, hRes);

::FreeLibrary(hFile);

The hIcon gets a handle and do NOT return NULL. Then I try to add it into a ImageList:

m_imageList.Create (16, 16, 0, 2, 2);
m_imageList.Add (hIcon);

Add returns -1..!!

If I instead do:
HICON hIcon = LoadIcon(hFile, MAKEINTRESOURCE(2)) ;
DWORD dwError = GetLastError ();

After LoadLibraryEx, hIcon then becomes NULL and GetLastError() returns ERROR_SUCCESS.

Anybody knows why?
SlaskAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MichaelSConnect With a Mentor Commented:
sorry, you have to use SHGFI_ICON flag instead of SHGFI_SYSICONINDEX.
0
 
SlaskAuthor Commented:
Edited text of question.
0
 
MichaelSCommented:
Did you tried to use AddIcon method of image list?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
SlaskAuthor Commented:
There is no function called AddIcon in CImageList....
0
 
MichaelSCommented:
so try this
HIMAGELIST hImageList = *pmyImageList;

ImageList_AddIcon(hImageList, hIcon);
0
 
SlaskAuthor Commented:
Didn't help. I think that the problem is that I don't get a handle to the icon but to something else. Since I can't use LoadIcon after LoadLibraryEx. Could that be the problem? If so any idea what's wrong?
0
 
MichaelSCommented:
you can test it very easy, just

HICON = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
0
 
MichaelSCommented:
I checked winzip32.exe and found thet icon with id = 3 don't has 16x16 image. Try to use

HRSRC hRes = FindResource(hFile, MAKEINTRESOURCE(3), RT_ICON);

for example.
0
 
SlaskAuthor Commented:
Tested and it worked, so the problem is in the LoadLibraryEx function. Anybody know why I can't use LoadLibraryEx the way I do?
0
 
MichaelSCommented:
Why don't you use LoadLibrary()?
0
 
SlaskAuthor Commented:
This can't be this diffcult as I make it. All I want to do is load the default display icon from, for instance winzip, to display in my application next to the .zip. How can I load that Icon? I found in the registry, default icon= "C:\PROGRA~\WINZIP\winzip32.exe,0"

All I want to do is get a handle to it so that I can add it to my image list....
0
 
SlaskAuthor Commented:
LoadLibrary() returns NULL, according to GetLastError () it returns:
ERROR_BAD_FORMAT

Is it impossible to get a handle to the icon?

In TweakUI on the NEW page, they have done what I want to be able to do. So there must be a way...
0
 
MichaelSCommented:
There is another way, you can get system imagelist and copy icon directly from it to your one.
0
 
SlaskAuthor Commented:
Any idea how thats done?
0
 
MichaelSCommented:
TCHAR szName[MAX_PATH];

strcpy(szName, "C:\\PROGRA~1\\WINZIP\\winzip32.exe");

SHFILEINFO sfi;

HIMAGELIST hImageList = (HIMAGELIST)SHGetFileInfo(szName, NULL, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES | FILE_ATTRIBUTE_NORMAL);

in
sfi.hIcon
you will have defauld icon of winzip32.exe.
0
 
SlaskAuthor Commented:
Thank you, thank you, thank you....

It worked perfectly.....
0
All Courses

From novice to tech pro — start learning today.