An example to save the icon of an executable :
#include <windows.h>
#include <olectl.h>
const IID __cdecl IID__IPicture =
{ 0x7BF80980, 0xBF32, 0x101A, { 0x8B, 0xBB, 0x00, 0xAA, 0x00, 0x30, 0x0C, 0xAB } };
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
PICTDESC pd;
HICON hIcon;
HMODULE hModule;
IPicture *pBitmap;
IStream *pStream;
LONG pcbSize;
HGLOBAL hGlobal;
LPVOID IconAddr;
HANDLE hFile;
DWORD bWritten;
// load the test executable and the associated icon
hModule = LoadLibrary("Test.exe");
hIcon = LoadIcon(hModule,MAKEINTRESOURCE(100));
// initialize the PICTDESC structure
pd.cbSizeofstruct = sizeof(PICTDESC);
pd.picType = PICTYPE_ICON;
pd.icon.hicon = hIcon;
// create the OLE image
OleCreatePictureIndirect(&pd,&IID__IPicture,TRUE,(void **)&pBitmap);
// create the destination stream to save the icon
CreateStreamOnHGlobal(NULL,TRUE,(LPSTREAM *)&pStream);
// save the icon to the stream
pBitmap->lpVtbl->SaveAsFile(pBitmap,(LPSTREAM)pStream,TRUE,&pcbSize);
// get the address of the icon in memory
GetHGlobalFromStream(pStream,&hGlobal);
IconAddr = GlobalLock(hGlobal);
// write the icon to disc
hFile= CreateFile("Smiley.ico",GENERIC_WRITE,0,0,
CREATE_ALWAYS,0,0);
WriteFile(hFile,IconAddr,pcbSize,&bWritten,0);
CloseHandle(hFile);
// release the pointers
pBitmap->lpVtbl->Release(pBitmap);
pStream->lpVtbl->Release(pStream);
return 0;
}