+ Reply to Thread
Results 1 to 2 of 2

Thread: How to extract an Icon from specified executable?

  1. #1

    How to extract an Icon from specified executable?

    How to extract an Icon from specified executable?

  2. #2
    Example:

    Code:
    void SaveIconToFile(HICON hico, LPCTSTR szFileName, BOOL bAutoDelete = FALSE)
    {
    	PICTDESC pd = {sizeof(pd), PICTYPE_ICON};
    	pd.icon.hicon = hico;
    
    	CComPtr<IPicture> pPict = NULL;
    	CComPtr<IStream>  pStrm = NULL;
    	LONG cbSize = 0;
    
    	BOOL res = FALSE;
    
    	res = SUCCEEDED( ::CreateStreamOnHGlobal(NULL, TRUE, &pStrm) );
    	res = SUCCEEDED( ::OleCreatePictureIndirect(&pd, IID_IPicture, bAutoDelete, (void**)&pPict) );
    	res = SUCCEEDED( pPict->SaveAsFile( pStrm, TRUE, &cbSize ) );
    
    	if( res )
    	{
    		// rewind stream to the beginning
    		LARGE_INTEGER li = {0};
    		pStrm->Seek(li, STREAM_SEEK_SET, NULL);
    
    		// write to file
    		HANDLE hFile = ::CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    		if( INVALID_HANDLE_VALUE != hFile )
    		{
    			DWORD dwWritten = 0, dwRead = 0, dwDone = 0;
    			BYTE  buf[4096];
    			while( dwDone < cbSize )
    			{
    				if( SUCCEEDED(pStrm->Read(buf, sizeof(buf), &dwRead)) )
    				{
    					::WriteFile(hFile, buf, dwRead, &dwWritten, NULL);
    					if( dwWritten != dwRead )
    						break;
    					dwDone += dwRead;
    				}
    				else
    					break;
    			}
    
    			_ASSERTE(dwDone == cbSize);
    			::CloseHandle(hFile);
    		}
    	}
    }
    
    void ExtractIcon(CString strExecutableName, CString strTargetFile)
    {
    	HICON hIcon = ::ExtractIcon(NULL, strExecutableName, 0) ;
    
    	SaveIconToFile(hIcon, strTargetFile) ;
    
    	DestroyIcon(hIcon) ;
    }
    SaveIconToFile code is from : http://www.codeguru.com/forum/showthread.php?p=733535
    Last edited by Harry; 03-09-2012 at 08:21 AM.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts