Thread: [winapi] Headache from ExtractAssociatedIcon, I need a doctor

  1. #1
    Banned
    Join Date
    Apr 2011
    Posts
    8

    Red face [winapi] Headache from ExtractAssociatedIcon, I need a doctor

    Hi folks,

    I have a terrible problem, already struggeling for 2 weeks with it. So I decided to post on a forum. I searched the whole internet for answers. Asked my fellow students. Nobody knew the answer.
    Oke, here it comes. In every .exe file there is a resource section which maintains the icons that are contained in the exe. There are often more than just one icon in every .exe stored. Every icon has an unique identifier. Now I try to get the identifier of the icon which is displayed as you see it normally when u view an .exe in your windows viewer for example.

    I try to do it this way:
    Code:
    WORD a;
    WORD b;
    char arr[] = "C:\\monkey.exe";
    LPWSTR wArr;
    
    buffer = (char*)malloc(10);
    memset(buffer, 0, 10);
    
    int buffSize = (int)strlen(arr) + 1;
    wArr = (wchar_t*)malloc(sizeof(wchar_t) * buffSize);
    MultiByteToWideChar(CP_ACP, 0, arr, buffSize, wArr, buffSize);
    
    if(ExtractAssociatedIcon(0, wArr, &a) == 0) // first 0 can be hInstance
       MessageBox(0, (LPCSTR)"fail",0,0);
    
    	itoa(a, buffer, 10);
    	MessageBox(0, (LPWSTR)buffert,0,0);
    The trouble is with the ExtractAssociatedIcon. It should put the correct identifier in the variabele a_. It does not do this.
    With resource hacker I can check if it returns me the right icon identifier. Guess what, it does not work for me. It worked once for me in the past. Don't know why it's not working now. I checked a lot of examples (Which are hard to find) and also the msdn (ExtractAssociatedIcon Function (Windows)) didn't help me further. Very rare problem I guess.
    Thank god who can tell me what I'm doing wrong.
    Last edited by links4all; 04-28-2011 at 03:12 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's because you're using the wrong call...

    ExtractIcon Function (Windows)

    Files in executables (including DLLS) are identified by an index value starting from 0.
    So you want to feed the function the full path to the file and the index of the icon you want.

    For example: ExtractIcon(NULL, "C:\\Windows\\System32\\moreicons.dll", 12);

    The return value is an icon handle that you can use for other winapi functions.

  3. #3
    Banned
    Join Date
    Apr 2011
    Posts
    8
    But that's not the problem. The problem is that I want to know the identifier of the main icon ( as you see it displayed in windows viewer). I need to know the identifier of the main icon because of special reasons. This is just a snippet of code from my project.
    I know how to extract the icon and safe it, but I'm not interested in a handle to the icon, just the identifier number of the main icon. :O

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The "main icon", that is the one you see in explorer is always at index 0.

    You won't likely be able to get the resource ID or name that's converted to an address when the program is compiled.
    Last edited by CommonTater; 04-28-2011 at 03:30 PM.

  5. #5
    Banned
    Join Date
    Apr 2011
    Posts
    8
    I wish it would be this easy but this is not the case.
    I loaded up an example:
    http://i56.tinypic.com/mucwg8.png
    As you can see the correct icon here has identifier 4.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummm... don't go by that... there's no guarantee they're in the EXE in that order.

    Really... I've had to shuffle the resource files (.rc) around more than once to get my project to display the correct icon... always the first one, #0.

  7. #7
    Banned
    Join Date
    Apr 2011
    Posts
    8
    That's the whole point. But the function ExtractAssociatedIcon can give me the right identifier. At least, it should. It worked for me once, in the past. :O It's really important for me, I need it this way.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    I don't know if this helps but the programming guru's at Microsoft do it this way for identifying the main icon:
    Code:
    #define ID_APPICON    1  /* must be one for file manager/explorer to find this */

  9. #9
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    You need to use the ex version: http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx. Despite what the docs say for the non-ex version doesn't change the last parameter.

    Code:
        WORD index = 0, id = 0;
        HICON hIcon = ExtractAssociatedIconEx(NULL, fileName, &index, &id);
        // destroy the icon if you don't need it
        // id is the resource id of the icon in filename

  10. #10
    Banned
    Join Date
    Apr 2011
    Posts
    8
    Code:
    char* buff;
    buff = (char*)malloc(10);
    	memset(buff, 0, 10);
    LPWORD a = new WORD;
    LPWORD b = new WORD;
    char arr[] = "C:\\incr.exe";
    //wchar_t widearray[100];
    //mbstowcs(widearray, narrowarray, 100);
    int buffSize = (int)strlen(arr) + 1;
    LPWSTR gah = new wchar_t[buffSize];
    MultiByteToWideChar(CP_ACP, 0, arr, buffSize, gah, buffSize);
    MessageBox(0,(LPCWSTR)gah,0,0);
    if(ExtractAssociatedIconEx(0, (LPWSTR)gah, a, b) == NULL)
       MessageBox(0, _T("fail"),0,0);
    
     	
    	itoa(*a, buff, 10);
    	MessageBox(0, (LPWSTR)buff,0,0);
    	itoa(*b, buff, 10);
    	MessageBox(0, (LPWSTR)gah,0,0);
    Yeah I tried already all this alternatives. Also the extended version. :O Because I tried already 100 times or so :P. Still doesn't work for me, did it work for you ??
    Last edited by links4all; 04-29-2011 at 04:16 AM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to set up the call correctly....

    Code:
    DWORD a;                      // icon index
    DWORD b;                      // returned icon id
    CHAR gah[MAX_PATH];         // file with icon resource
    HICON ico;                     // returned icon handle  
    
    strcpy(gah, "C:\\incr.exe");  
    a = 0;    
    ico = ExtractAssociatedIconEx(NULL,gah, &a, &b);
    
    if (!ico)
      { MessageBox(NULL,"Could not retrieve Icon","Fail",MB_TOPMOST);
        // recover from error here }
    When it returns... ico has the icon's handle, a has the index, b has the resource ID, gah has the path to the executable.
    Last edited by CommonTater; 04-29-2011 at 08:12 AM.

  12. #12
    Banned
    Join Date
    Apr 2011
    Posts
    8

    Unhappy

    I admire your help Tater. It still does not work for me.
    What number do you get out of a then? I just always get 1 or 0. While this is not the correct identifier.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The identifier is in b... not a.

    And no I didn't spend an hour writing a program to test this... that's your job.

  14. #14
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by links4all View Post
    Still doesn't work for me, did it work for you ??
    Yep. If you look at the very top left, you can see the top of the icon the function returned after it'd been drawn that proves it's the one it should be.
    Last edited by adeyblue; 04-29-2011 at 01:11 PM.

  15. #15
    Banned
    Join Date
    Apr 2011
    Posts
    8
    Oke guys it works for me now Thank you so ..........ing much !!
    Last edited by links4all; 04-30-2011 at 07:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I/O headache
    By Strait in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2005, 12:20 PM
  2. I finally went to the doctor...
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-28-2004, 02:31 PM
  3. Doctor Who
    By minesweeper in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 11-22-2002, 03:01 PM
  4. Im getting a headache
    By RyeDunn in forum C Programming
    Replies: 5
    Last Post: 07-25-2002, 10:27 PM
  5. I've got headache~
    By black in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 07-17-2002, 08:09 PM