I'm writing a program which need to load icon resource from memory, there seems no direct windows API can do this work. the icon file was transmit by network, my program receives the data of the file, and store this data in my program buffer, I want to use this data to set the icon of a window. the code structure just like this:
char* buffer;
recvicon_data_by_network(&buffer,...);
HICON icon=(HICON)how_to_load_icondata_from_memory(buffer,...);
SendMessage(my_hwnd,WM_SETICON,(WPARAM)ICON_SMALL,(LPARAM)icon);
As referred in MSDN, there is a structure ICONIFO
typedef struct _ICONINFO {
BOOL fIcon;
DWORD xHotspot;
DWORD yHotspot;
HBITMAP hbmMask;
HBITMAP hbmColor;
} ICONINFO;
And the structure of .ICO file , it's a little complicate, there seems have a lot about compression alg. I do not have the skills to deal with it yet.
What I have tried:
There are also some articles about convert .ico format file data to ICONINFO structure, I have tried, not work, maybe not the correct way.
int cx = 16, cy = 16;
int offset=LookupIconIdFromDirectoryEx((PBYTE)&buffer[0], TRUE, cx, cy, LR_DEFAULTCOLOR);
unsigned short i=1, count = *((unsigned short*)&buffer[4]);
while (i < count) {
memcpy(((PBYTE)&buffer[6]) + (i * 0xe),((PBYTE)&buffer[6]) + (i * 0x10), 0xe);
}
HICON ico_fav = CreateIconFromResourceEx((PBYTE)&buffer[offset], 0, TRUE, 0x00030000,cx,cy, LR_DEFAULTCOLOR);
I do not want to save the icon data in a local file, and my program structure does not support the way of gdiplus::Image::FromStream(...). I need the HICON handle and I have the memory of icon's data.