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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SlaskAuthor Commented:
Edited text of question.
0
MichaelSCommented:
Did you tried to use AddIcon method of image list?
0
SlaskAuthor Commented:
There is no function called AddIcon in CImageList....
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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
MichaelSCommented:
sorry, you have to use SHGFI_ICON flag instead of SHGFI_SYSICONINDEX.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SlaskAuthor Commented:
Thank you, thank you, thank you....

It worked perfectly.....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.