Extracting Icons from Executable Files

An application can use the ExtractIcon function to retrieve the handle of an icon from a specified executable file, dynamic-link library, or icon file. The following example uses the DragQueryPoint function to retrieve the coordinates of the point where a file was dropped, the DragQueryFile function to retrieve the filename of a dropped file, and the ExtractIcon function to retrieve the handle of the first icon in the file, if any.

POINT pt;

WORD cFiles;

HDC hdc;

char lpszFile[80];

HANDLE hCurrentInst, hicon;

DragQueryPoint((HANDLE) wParam, &pt);

cFiles = DragQueryFile((HANDLE) wParam, 0xFFFF, NULL, NULL);

if(cFiles > 1) {

TextOut(hdc, pt.x, pt.y,

"Please drop only one icon file.", 31);

return FALSE;

}

else {

DragQueryFile((HANDLE) wParam, 0, lpszFile, sizeof(lpszFile));

hCurrentInst = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE);

hicon = ExtractIcon(hCurrentInst, lpszFile, 0);

if (hicon == NULL)

TextOut(hdc, pt.x, pt.y, "No icons found.", 15);

else if (hicon == (HICON) 1)

TextOut(hdc, pt.x, pt.y,

"File must be .EXE, .ICO, or .DLL.", 33);

else

DrawIcon(hdc, pt.x, pt.y, hicon);

}

DragFinish((HANDLE) wParam);