Home > Undocumented WinAPI > Changing console application window icon at runtime!

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…

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<sci>( 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;
}

Advertisements
  1. August 19, 2008 at 6:00 am

    Thanks Alex!

  2. August 17, 2008 at 3:41 am

    Your blog is interesting!

    Keep up the good work!

  3. July 8, 2008 at 9:55 am

    No that’s not possible, only icon’s are possible, or convert to icon.

  4. Rajiv
    July 8, 2008 at 9:54 am

    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

  5. July 8, 2008 at 9:35 am

    Console applications can also have resource files! I did test this code sometime back with IDI_QUESTION and it did work.

  6. Rajiv
    July 8, 2008 at 9:32 am

    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?

  7. July 8, 2008 at 9:21 am

    How about providing a custom icon? Does that work?

    By the way is this XP or vista.

  8. Rajiv
    July 8, 2008 at 9:01 am

    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. January 28, 2008 at 10:36 am

    🙂 Happy to know that Hugo! You are welcome.

Comment pages
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: