Changing console application window icon at runtime!

Bored with the old black icon that you have on the main window. Here is a way to change it. There is an undocumented API called SetConsoleIcon and it’s in Kernel32.dll.  Here is how we use it…

[sourcecode language=’cpp’]void ChangeIcon( const HICON hNewIcon )
{
 // Load kernel 32 library
 HMODULE hMod = LoadLibrary( _T( “Kernel32.dll” ));
 ASSERT( hMod );

 // Load console icon changing procedure
 typedef DWORD ( __stdcall *SCI )( HICON );
 SCI pfnSetConsoleIcon = reinterpret_cast( GetProcAddress( hMod, “SetConsoleIcon” ));
 ASSERT( pfnSetConsoleIcon );

  // Call function to change icon
 pfnSetConsoleIcon( hNewIcon );

 FreeLibrary( hMod );
}// End ChangeIcon

// Main function
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

HMODULE hMainMod = GetModuleHandle( 0 );
ASSERT( hMainMod );

HICON hMainIcon = ::LoadIcon( hMainMod, MAKEINTRESOURCE( IDI_ICON3 ));
ASSERT( hMainIcon );

// Change main window icon
ChangeIcon( hMainIcon );

// To reset to old icon uncomment this code
// ChangeIcon( 0 );

return nRetCode;
}[/sourcecode]

19 thoughts on “Changing console application window icon at runtime!

  1. Works nice! Thank you! But do you know how to reset the icon? I ask, because I’d like to reset the standard icon when my console application finishes.

    Regards
    Marcus

  2. Yeah I’ve modified the entry to answer your question, please look at it.

    BTW the answer is to call ChangeIcon with a NULL parameter but because of the ASSERT it was not possible before but now I’ve removed it and it should work.

  3. Hi, I wrote you from Yucatan, Mexico.
    I have been looking for a simple utility like this since long time ago.

    I have a useful DbaseIV application and I want to change the “old black icon” while it runs.
    I call this Dbase app from a bat file. I was thought running first a small changer icon utility maybe with the new icon path as a parameter and then call the Dbase app.
    Do you think the new icon stays while the Dbase app runs?

    If you have done a small changer icon utility like this I appreciate if you can sent it to me. I am not a profesional programmer so I dont have the tools to make it from your code.

    Hugo

  4. Hi Nibu, Thanks a lot for take care of my problem. I found a related post in this website:

    “Change console application title!”

    I did the homework, I got the Dev-C++ from the web and copiled the following code:

    #include
    #include
    #include
    #include

    int main( int argc, char** argv)
    {
    SetConsoleTitle(“HugoConsole”);
    return 0;
    }

    and the exe file obteined works exactly the way a was telling you !!!

    The next step, I said, is do the same with the icon. I try the following code:

    #include
    #include
    #include
    #include

    int main( int argc, char** argv)
    {
    SetConsoleIcon(“NewIcon.ico”);
    return 0;
    }

    but I got this error mesage:
    – [Linker error] undefined reference to `SetConsoleIcon’ –

    I think the parameter “NewIcon.ico” is the problem. I was thought copy the new icon in the same folder than the exe file but probably there is another way to link with the new icon.
    Do you know what is the correct way to describe this parameter or if there is another problem with the code?

    Thanks,
    Hugo.

  5. Hello Hugo,

    Sorry for the delay… Just copy and paste the code given by me into Dev-C++. All you need to do is add an icon as a resource.

    As far as the error is concerned -> SetConsoleIcon declaration is not there in older sdk’s but in newer one’s. It’s exported from Kernel32.dll, so we use GetProcAddress to retrieve the address of this API and call it.

    Thanks,
    Nibu.

  6. Hi, Nibu, I get an error in two points:

    1) HMODULE hMod = LoadLibrary( _T( “Kernel32.dll” ));

    2) SCI pfnSetConsoleIcon = reinterpret_cast( GetProcAddress( hMod, “SetConsoleIcon” ));

    I think the functions ‘_T’ and ‘reinterpret_cast’ are not defined, but I don’t know how to do that. Also said something about SCI.

    Hugo

  7. Happy end, Nibu!

    I did what you said and also I put all the ‘assert’ statement as a coments.
    The resoult works exactly the way a was looking for.

    Thanks, I learn a lot and I want to go far away so maybe I gonna distract you some other time with another question.

    Regards
    Hugo

  8. Hi

    I am using the following code for assigning a different icon for my executable. On Executing the program, i notice that the executable is not being assigned a different icon. Could you suggest what is going wrong?

    #include
    #include “windows.h”
    #include “conio.h”

    void ChangeIcon( const HICON hNewIcon )
    {
    // Load kernel 32 library
    HMODULE hMod = LoadLibraryA(“Kernel32.dll”);
    if(hMod == NULL)
    printf(“Error: %d”,GetLastError());

    // Load console icon changing procedure
    typedef DWORD ( __stdcall *SCI )( HICON );
    SCI pfnSetConsoleIcon = NULL;
    pfnSetConsoleIcon = reinterpret_cast( GetProcAddress( hMod, “SetConsoleIcon” ));

    if(pfnSetConsoleIcon != NULL)
    {
    // Call function to change icon
    pfnSetConsoleIcon( hNewIcon );

    // Free library
    FreeLibrary( hMod );
    }
    }// End ChangeIcon

    // Main function
    int main(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    int nRetCode = 0;

    HMODULE hMainMod = GetModuleHandle(NULL);
    if(hMainMod == NULL)
    printf(“Error: %d”,GetLastError());

    //HICON hMainIcon = ::LoadIcon( hMainMod, MAKEINTRESOURCE(IDI_ICON3));
    HICON hMainIcon = ::LoadIcon( NULL, IDI_WARNING);
    if(hMainIcon == NULL)
    printf(“Error: %d”,GetLastError());

    // Change main window icon
    ChangeIcon(hMainIcon);

    // To reset to old icon uncomment this code
    // ChangeIcon( 0 );

    return nRetCode;
    }

  9. I am using Windows XP and Microsoft Visual C++ 2005. I could not add a resource file to this workspace as this is a console application. Is’nt the .exe created after execution of this program supposed to have the specified icon?

  10. Hi,

    I actually wanted to assign a .png image as the default icon for my program executable. I have not clue how to add this .png as a resource file in a console application workspace. I have tried explicitly loading the icon by providing the relative path to where it is located as ……..

    HICON hMainIcon = ::LoadIcon( hMainMod, “.\icon.png”);

    GetLastError gives me a image file not found error!!

    Rajiv

Appreciate your comments...