Change Icon of EXE file through code extracting it from other EXE file

Discussion in 'MFC' started by shabbir, Mar 18, 2006.

  1. kylls

    kylls New Member

    Joined:
    May 28, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    tanks man !
     
  2. jefri_btc

    jefri_btc New Member

    Joined:
    Aug 9, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I need source code in Visual basic 2005 or 2008
    Please Help ME
     
  3. BokiSergy

    BokiSergy New Member

    Joined:
    Aug 20, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
  4. limon

    limon New Member

    Joined:
    Sep 29, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I saw programs that can show you the ICONS in an executable files and so I thought if we can see them then definitely we can edit them as well. If we can edit them then there is a way to give them a different ICON to be displayed in the explorer.
     
  5. coder_gate

    coder_gate New Member

    Joined:
    Jan 27, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    how are you doing extract icon from this code

    PHP:
    #include<stdio.h>
    #include<windows.h>
    #include <stdlib.h>
    #include<iostream.h>

    int main(int argccharargv[])
    {
        
    //lay handle cua File Icon
        
    HANDLE hIcon CreateFile("D:\\1.ico",
            
    GENERIC_READ,
            
    0,
            
    NULL,
            
    OPEN_EXISTING,
            
    FILE_ATTRIBUTE_NORMAL,
            
    NULL);
        if (!
    hIcon)
        {
            
    printf("khong the tao icon");
            return 
    1;
        }
        
        
    LPBYTE lpBuf;
        
    DWORD dwFileSizedwBytesRead;
        
        
    dwFileSize GetFileSize(hIconNULL);  //lay kich thuoc cua no
        
    cout<<dwFileSize;
        
    lpBuf = (LPBYTE)malloc(dwFileSize);
        if (!
    lpBuf)
        {
            
    CloseHandle(hIcon);
            
    printf("khong the cap phat dong");
            return 
    1;
        }
        
        
    ReadFile(hIconlpBufdwFileSize, &dwBytesReadNULL);
        if (
    dwBytesRead != dwFileSize)
        {
            
    free(lpBuf);
            
    CloseHandle(hIcon);
            
    printf("khong the doc FIle");
            return 
    1;
        }
        
        
    CloseHandle(hIcon);
        
        
        
    HANDLE hUpdateRes;
        
    hUpdateRes BeginUpdateResource("D:\\1.exe"FALSE);
        if (
    hUpdateRes == NULL)
        {
            
    free(lpBuf);
            
    printf("khong the mo File");
            return 
    1;
        }
        
        if (!
    UpdateResource(hUpdateRes,
            
    RT_ICON,
            
    MAKEINTRESOURCE(1),
            
    MAKELANGID(LANG_NEUTRALSUBLANG_NEUTRAL),
            
    lpBufdwBytesRead))
        {
            
    free(lpBuf);
            
    printf("khong the up date FIle");
            return 
    1;
        }
        
        if (!
    EndUpdateResource(hUpdateResFALSE))
        {
            
    free(lpBuf);
            
    printf("khong the ket thuc");
            return 
    1;
        }
        
        
    free(lpBuf);
        
        
    printf("Hello World!\n");
        return 
    0;
    }
     
  6. jambo

    jambo New Member

    Joined:
    May 1, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I downloaded second attachement, tried it in two files but didn't work ??
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Try and see if it works with the exe's I have mentioned.
     
  8. jambo

    jambo New Member

    Joined:
    May 1, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    shabbir, I am having trouble with the API's. The second parameter of FindResource, msdn says it has to be the resource's name, so if I am extracting an icon from an exe how am I supposed to know it's name ?? what did u do with the '#' thing lol ?
    Also the 3rd and 4th parameters of updateresource method, dunno much what to put there, especially the language thing!!
    Can you explain to me ? :$
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The Name of the Icon is mainly an integer

    This Code should explain that

    Code:
        
        // Locate the ICON resource in the .EXE file. 
        for(iLoop = 1;;iLoop++)
        {
            CString str;
            str.Format("#%d",iLoop);
            hRes = FindResource(hSrcExe, str, RT_ICON); 
            if (hRes == NULL) 
                continue ; 
            else if(iLoop == 10)
                return;
            else
                break;
        }
    
    Checking only first 10 resources as this is only for tutorial purpose.

    Look at the code in the article and its pretty much self explanatory as well as commented.
     
  10. jambo

    jambo New Member

    Joined:
    May 1, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Of course I have read the code and the comments, however, UpdateResource in my code always returns false and l have some questions that are still left unanswered and I would be very happy if you could answer them.

    Why did you need to Loop to find the icon ? Does this mean :
    a) a file has more than one icon ?
    b) FindResource checks resource one by one??

    ..a string... an integer..or even a humain if it wants to, my problem is how to know what it is, I don't care it's type. I have no idea what to fill in this parameter and I don't understand what's the '#' for.

    What is MAKEINTID(1) ? some function I need to declare ??
    And what's that thing with the language lol ? I mean, seriously, since when language is relevant to us ??

    I want to cry

    :D
     
  11. jambo

    jambo New Member

    Joined:
    May 1, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    ah and.. sure I read all msdn however I'm not native english speaker and I sometimes can't understand especially with things I never dealt with before So if u can tke some of ur time and explain to me you would save my life

    note :I'm on VBNet
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Actually yes. There are many Icons and even if you see one there are of many sizes like 16x16, 32x32 and so on. So its always more than one Icon.
    Yes it does it one by one based on the Id ( Integer ) you provide and tells you if its a valid resource of the type specified.
    If you know how the resources are stored in the RC file this would be easy. When you add any resource it gets assigned an Integer in the Resource.h file and so its always integer
    Its already defined and you do not need to do anything extra to declare it but just use it.

    I hope I have cleared some of your queries
     
  13. jambo

    jambo New Member

    Joined:
    May 1, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    hmm thx for replies Jabbir but I can't succeed in writing this in VB :(
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I am not a VB Expert :D
     
  15. zzo

    zzo New Member

    Joined:
    Jun 6, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I've found this code, and topic googleing, looking for a good example that will explain how to do the exact thing your example does :D

    I am using Visual Basic, and code is not working for me.

    I'm having problems with this function FindResource, as it wont find the resource.

    I opened my source EXE file with a tool and i've seen that my icon names are 30003, 30004, and 30005, so i used "#30004" to find icon.

    Code:
    hRes = FindResource(hSrc, "#30002", RT_ICON)
    It always returns 0. I tried all possibilities. I tried another EXE with Icon names 1,2,3...8. Function also failed.

    Have two questions:
    - What could be the cause for function failiure?
    - Is there any other way to do this?
     
  16. zzo

    zzo New Member

    Joined:
    Jun 6, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I didn't mention that examples are not working for me. I compiled the example with source code too.
    Neither is working. :/
     
  17. zzo

    zzo New Member

    Joined:
    Jun 6, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I have also tried to rewrite the code in VB and made everything correct but some function fail. I guess that this FindResource is not compatible with VB, or the declarations are not correct, maybe for this RT_ICON. I have started writing this code in c++ console application and it works now for me. But this example that is in MFC does not work. I dont understand why.

    This is my VB code with declarations:


    //Those are examples where i know my icon name is #1, but it can be #20000 also so you shold loop from 1 to ... 65536 maybe. Just guessing.

    Code:
    This is C++ code:
    hRes = FindResource(hSrc, MAKEINTRESOURCE(1), RT_ICON); //Works!
    Code:
    LPCTSTR lpName;
    lpName = "#1" 
    hRes = FindResource(hSrc, lpName, RT_ICON); //Works
    And this is Visual Basic code that doesn work:
    Code:
    Private Declare Function FindResource Lib "kernel32.dll" Alias "FindResourceA" ( _
         ByVal hInstance As Long, _
         ByVal lpName As String, _
         ByVal lpType As String) As Long
         
    Private Const RT_ICON As Long = 3&
    Code:
    Dim hRes as Long
    hRes = FindResource(hSrc, "#1", RT_ICON)
     
  18. athidi

    athidi New Member

    Joined:
    Sep 28, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I can't open the two .zip files given at the end please help

    in my computer the file extension is changed to .php after downloading and even if the change the extension to .zip or .rar I can't open it......

    what could be the possiible error..........
     
  19. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Dont know why but I checked it once again now and it did download as zip
     
  20. VijayLaxmi

    VijayLaxmi New Member

    Joined:
    Feb 16, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I also want to do this,i m already taking help of this thread but its not working.
    t think you done it so please provide me sample to change icon to exe.
    thanks.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice