title: Problems with setting application icon
body: <p>(I'm using Visual Studio 2008, though I remember having similar problems with older versions as well.)</p>
  
  <p>I've tried several different methods (many of them mentioned in <a href="https://stackoverflow.com/questions/320677/how-do-i-set-the-icon-for-my-application-in-visual-studio-2008">this other question</a>), but I am still having some strange issues:</p>
  
  <ol>
  <li><p>When including an icon as a resource, it does show up as the executable file's icon immediately, but for the icon to show up on the taskbar, I have to restart the computer.  Until then, it continues to show up as whatever the previous icon was.  Cleaning the solution, restarting VS, doesn't have any effect.  Not a really big issue, as it won't affect a released exe, but it would be nice to know where it's keeping the old icon cached and how to get rid of it.</p></li>
  <li><p>No matter what I do, the icon displayed when alt-tabbing is the default app icon (square and white and generic).  This includes embedding the icon in the executable, as well as setting <code>ICON_BIG</code> with <code>WM_SETICON</code>.</p></li>
  </ol>
  
  <p>As for the second matter, my code looks something like:</p>
  
  <pre><code>   hIcon = (HICON)(
        LoadImage( NULL, szFilename, IMAGE_ICON, 32, 32, LR_LOADFROMFILE ) );
     if( hIcon )
        SendMessage( hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon );
  </code></pre>
  
  <p>However, after sending <code>WM_SETICON</code>, <code>GetLastError()</code> returns 6, "The handle is invalid.".  <code>hWnd</code> is a valid window handle, and <code>hIcon</code> appears to be a valid icon handle.  I've tried searching for reasons why <code>WM_SETICON</code> could cause that error, and at the very least, to figure out WHICH handle it thinks is invalid, but no luck yet.  I've cleared the error code immediately before calling <code>SendMessage()</code>, so it has to be set somewhere in the processing of the message.</p>
  
  <p>I tried an alternate method, loading the icon from the exe itself, where the ID of the resource is <code>101</code> (it's the first and only resource included):</p>
  
  <pre><code>   hIcon = (HICON)(
        LoadImage( GetModuleHandle( NULL ), MAKEINTRESOURCE( 101 ),
        IMAGE_ICON, 48, 48, 0 ) );
     if( hIcon )
        SendMessage( hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon );
  </code></pre>
  
  <p>... but the same thing happens; after calling <code>SendMessage()</code>, <code>GetLastError()</code> gives the same error status.</p>
  
  <p>I've tried different dimensions (such as 48x48, all of which are present in the icon file), but to no different effect.  I know it's definitely finding and loading the images, because if I specify a size that doesn't exist or an invalid resource ID or the wrong filename (depending on how I am loading it), it fails out long before <code>SendMessage()</code>.</p>
  
  <p>Strangely, if I specify <code>ICON_SMALL</code> instead of <code>ICON_BIG</code>, the call succeeds with no error status, but from <a href="http://msdn.microsoft.com/en-us/library/ms632643.aspx" rel="nofollow noreferrer">the docs</a>, I need to use <code>ICON_BIG</code> to set the icon used while alt-tabbing.  Also, if I use <code>ICON_BIG</code> but load the 16x16 icon, I get no error status, but nothing changes.</p>
  
  <p>Any ideas about what could be causing <code>WM_SETICON</code> to fail?  Anything terribly wrong with any of the code I've posted (aside from formatting/style/casting issues, as it's simplified to just the basics)?</p>