DWORD CIconExtractor::ExtractIcon(HINSTANCE hResource, LPCTSTR id, LPCTSTR TargetICON) { // Find the group icon resource HRSRC hRsrc = FindResource(hResource, id, RT_GROUP_ICON); if (!hRsrc) return GetLastError(); HGLOBAL hGlobal = nullptr; if ((hGlobal = LoadResource(hResource, hRsrc)) == nullptr) return GetLastError(); LPMEMICONDIR lpIcon = nullptr; if ((lpIcon = static_cast<LPMEMICONDIR>(LockResource(hGlobal))) == nullptr) return GetLastError(); LPICONRESOURCE lpIR = nullptr; if ((lpIR = static_cast<LPICONRESOURCE>(malloc(sizeof(ICONRESOURCE) + ((lpIcon->idCount - 1) * sizeof(ICONIMAGE))))) == nullptr) return GetLastError(); SecureZeroMemory(lpIR, sizeof(ICONRESOURCE) + ((lpIcon->idCount - 1) * sizeof(ICONIMAGE))); lpIR->nNumImages = lpIcon->idCount; SCOPE_EXIT { for (UINT i = 0; i < lpIR->nNumImages; ++i) free(lpIR->IconImages[i].lpBits); free(lpIR); }; // Go through all the icons for (UINT i = 0; i < lpIR->nNumImages; ++i) { // Get the individual icon if ((hRsrc = FindResource(hResource, MAKEINTRESOURCE(lpIcon->idEntries[i].nID), RT_ICON)) == nullptr) return GetLastError(); if ((hGlobal = LoadResource(hResource, hRsrc)) == nullptr) return GetLastError(); // Store a copy of the resource locally lpIR->IconImages[i].dwNumBytes = SizeofResource(hResource, hRsrc); lpIR->IconImages[i].lpBits = static_cast<LPBYTE>(malloc(lpIR->IconImages[i].dwNumBytes)); if (!lpIR->IconImages[i].lpBits) return GetLastError(); memcpy(lpIR->IconImages[i].lpBits, LockResource(hGlobal), lpIR->IconImages[i].dwNumBytes); // Adjust internal pointers if (!AdjustIconImagePointers(&(lpIR->IconImages[i]))) return GetLastError(); } return WriteIconToICOFile(lpIR, TargetICON); }
/**************************************************************************** * * FUNCTION: ReadIconFromEXEFile * * PURPOSE: Load an Icon Resource from a DLL/EXE file * * PARAMS: LPCTSTR szFileName - name of DLL/EXE file * * RETURNS: LPICONRESOURCE - pointer to icon resource * * History: * July '95 - Created * \****************************************************************************/ LPICONRESOURCE ReadIconFromEXEFile( LPCTSTR szFileName ) { LPICONRESOURCE lpIR = NULL, lpNew = NULL; HINSTANCE hLibrary; LPTSTR lpID; EXEDLLICONINFO EDII; // Load the DLL/EXE - NOTE: must be a 32bit EXE/DLL for this to work if( (hLibrary = LoadLibraryEx( szFileName, NULL, LOAD_LIBRARY_AS_DATAFILE )) == NULL ) { // Failed to load - abort MessageBox( hWndMain, "Error Loading File - Choose a 32bit DLL or EXE", szFileName, MB_OK ); return NULL; } // Store the info EDII.szFileName = szFileName; EDII.hInstance = hLibrary; // Ask the user, "Which Icon?" if( (lpID = ChooseIconFromEXEFile( &EDII )) != NULL ) { HRSRC hRsrc = NULL; HGLOBAL hGlobal = NULL; LPMEMICONDIR lpIcon = NULL; UINT i; // Find the group icon resource if( (hRsrc = FindResource( hLibrary, lpID, RT_GROUP_ICON )) == NULL ) { FreeLibrary( hLibrary ); return NULL; } if( (hGlobal = LoadResource( hLibrary, hRsrc )) == NULL ) { FreeLibrary( hLibrary ); return NULL; } if( (lpIcon = LockResource(hGlobal)) == NULL ) { FreeLibrary( hLibrary ); return NULL; } // Allocate enough memory for the images if( (lpIR = malloc( sizeof(ICONRESOURCE) + ((lpIcon->idCount-1) * sizeof(ICONIMAGE)) )) == NULL ) { MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK ); FreeLibrary( hLibrary ); return NULL; } // Fill in local struct members lpIR->nNumImages = lpIcon->idCount; lstrcpy( lpIR->szOriginalDLLFileName, szFileName ); lstrcpy( lpIR->szOriginalICOFileName, "" ); // Loop through the images for( i = 0; i < lpIR->nNumImages; i++ ) { // Get the individual image if( (hRsrc = FindResource( hLibrary, MAKEINTRESOURCE(lpIcon->idEntries[i].nID), RT_ICON )) == NULL ) { free( lpIR ); FreeLibrary( hLibrary ); return NULL; } if( (hGlobal = LoadResource( hLibrary, hRsrc )) == NULL ) { free( lpIR ); FreeLibrary( hLibrary ); return NULL; } // Store a copy of the resource locally lpIR->IconImages[i].dwNumBytes = SizeofResource( hLibrary, hRsrc ); lpIR->IconImages[i].lpBits = malloc( lpIR->IconImages[i].dwNumBytes ); memcpy( lpIR->IconImages[i].lpBits, LockResource( hGlobal ), lpIR->IconImages[i].dwNumBytes ); // Adjust internal pointers if( ! AdjustIconImagePointers( &(lpIR->IconImages[i]) ) ) { MessageBox( hWndMain, "Error Converting to Internal Format", szFileName, MB_OK ); free( lpIR ); FreeLibrary( hLibrary ); return NULL; } } } FreeLibrary( hLibrary ); return lpIR; }
/**************************************************************************** * * FUNCTION: ReadIconFromICOFile * * PURPOSE: Reads an Icon Resource from an ICO file * * PARAMS: LPCTSTR szFileName - Name of the ICO file * * RETURNS: LPICONRESOURCE - pointer to the resource, NULL for failure * * History: * July '95 - Created * \****************************************************************************/ LPICONRESOURCE ReadIconFromICOFile( LPCTSTR szFileName ) { LPICONRESOURCE lpIR = NULL, lpNew = NULL; HANDLE hFile = NULL; LPRESOURCEPOSINFO lpRPI = NULL; UINT i; DWORD dwBytesRead; LPICONDIRENTRY lpIDE = NULL; // Open the file if( (hFile = CreateFile( szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL )) == INVALID_HANDLE_VALUE ) { MessageBox( hWndMain, "Error Opening File for Reading", szFileName, MB_OK ); return NULL; } // Allocate memory for the resource structure if( (lpIR = malloc( sizeof(ICONRESOURCE) )) == NULL ) { MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK ); CloseHandle( hFile ); return NULL; } // Read in the header if( (lpIR->nNumImages = ReadICOHeader( hFile )) == (UINT)-1 ) { MessageBox( hWndMain, "Error Reading File Header", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIR ); return NULL; } // Adjust the size of the struct to account for the images if( (lpNew = realloc( lpIR, sizeof(ICONRESOURCE) + ((lpIR->nNumImages-1) * sizeof(ICONIMAGE)) )) == NULL ) { MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIR ); return NULL; } lpIR = lpNew; // Store the original name lstrcpy( lpIR->szOriginalICOFileName, szFileName ); lstrcpy( lpIR->szOriginalDLLFileName, "" ); // Allocate enough memory for the icon directory entries if( (lpIDE = malloc( lpIR->nNumImages * sizeof( ICONDIRENTRY ) ) ) == NULL ) { MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIR ); return NULL; } // Read in the icon directory entries if( ! ReadFile( hFile, lpIDE, lpIR->nNumImages * sizeof( ICONDIRENTRY ), &dwBytesRead, NULL ) ) { MessageBox( hWndMain, "Error Reading File", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIR ); return NULL; } if( dwBytesRead != lpIR->nNumImages * sizeof( ICONDIRENTRY ) ) { MessageBox( hWndMain, "Error Reading File", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIR ); return NULL; } // Loop through and read in each image for( i = 0; i < lpIR->nNumImages; i++ ) { // Allocate memory for the resource if( (lpIR->IconImages[i].lpBits = malloc(lpIDE[i].dwBytesInRes)) == NULL ) { MessageBox( hWndMain, "Error Allocating Memory", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIR ); free( lpIDE ); return NULL; } lpIR->IconImages[i].dwNumBytes = lpIDE[i].dwBytesInRes; // Seek to beginning of this image if( SetFilePointer( hFile, lpIDE[i].dwImageOffset, NULL, FILE_BEGIN ) == 0xFFFFFFFF ) { MessageBox( hWndMain, "Error Seeking in File", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIR ); free( lpIDE ); return NULL; } // Read it in if( ! ReadFile( hFile, lpIR->IconImages[i].lpBits, lpIDE[i].dwBytesInRes, &dwBytesRead, NULL ) ) { MessageBox( hWndMain, "Error Reading File", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIR ); free( lpIDE ); return NULL; } if( dwBytesRead != lpIDE[i].dwBytesInRes ) { MessageBox( hWndMain, "Error Reading File", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIDE ); free( lpIR ); return NULL; } // Set the internal pointers appropriately if( ! AdjustIconImagePointers( &(lpIR->IconImages[i]) ) ) { MessageBox( hWndMain, "Error Converting to Internal Format", szFileName, MB_OK ); CloseHandle( hFile ); free( lpIDE ); free( lpIR ); return NULL; } } // Clean up free( lpIDE ); free( lpRPI ); CloseHandle( hFile ); return lpIR; }
DWORD CIconExtractor::ExtractIcon(HINSTANCE hResource, LPCTSTR id, LPCTSTR TargetICON) { LPICONRESOURCE lpIR = NULL; HRSRC hRsrc = NULL; HGLOBAL hGlobal = NULL; LPMEMICONDIR lpIcon = NULL; // Find the group icon resource hRsrc = FindResource(hResource, id, RT_GROUP_ICON); if (hRsrc == NULL) return GetLastError(); if ((hGlobal = LoadResource(hResource, hRsrc)) == NULL) return GetLastError(); if ((lpIcon = (LPMEMICONDIR)LockResource(hGlobal)) == NULL) return GetLastError(); if ((lpIR = (LPICONRESOURCE) malloc(sizeof(ICONRESOURCE) + ((lpIcon->idCount-1) * sizeof(ICONIMAGE)))) == NULL) return GetLastError(); lpIR->nNumImages = lpIcon->idCount; // Go through all the icons for (UINT i = 0; i < lpIR->nNumImages; ++i) { // Get the individual icon if ((hRsrc = FindResource(hResource, MAKEINTRESOURCE(lpIcon->idEntries[i].nID), RT_ICON )) == NULL) { free(lpIR); return GetLastError(); } if ((hGlobal = LoadResource(hResource, hRsrc )) == NULL) { free(lpIR); return GetLastError(); } // Store a copy of the resource locally lpIR->IconImages[i].dwNumBytes = SizeofResource(hResource, hRsrc); lpIR->IconImages[i].lpBits =(LPBYTE) malloc(lpIR->IconImages[i].dwNumBytes); if (lpIR->IconImages[i].lpBits == NULL) { free(lpIR); return GetLastError(); } memcpy(lpIR->IconImages[i].lpBits, LockResource(hGlobal), lpIR->IconImages[i].dwNumBytes); // Adjust internal pointers if (!AdjustIconImagePointers(&(lpIR->IconImages[i]))) { free(lpIR); return GetLastError(); } } DWORD ret = WriteIconToICOFile(lpIR,TargetICON); if (ret) { free(lpIR); return ret; } free(lpIR); return NO_ERROR; }