;(c) Copyright Barry Kauler, March 2003. www.goosee.com/x86 ;No limits on using this code, kindly keep this acknowledgement when ;distributing this file. ;How to read a BMP file into memory, later on convert it to a handle. ;I had a need for my app to be able to dynamically read small BMP files ;(just 12x12 pixels, monochrome), to be uploaded to the menu items via ;the bitmap checkmark function SetMenuItemBitmaps(). ;The normal approach is LoadBitmap() is used to load a bitmap from resources, ;or LoadImage can load it from file. These return a handle to the bitmap, ;that can then be used by SetMenuItemBitmaps(). ;The problem is, there is no function to load a BMP file when it is already ;read into memory. ;That is, I used CreateFile() then ReadFile() to read in the BMP file. ;Here is code that will do the trick... .DATA ;ipicture interface... IID_IPicture DWORD 07bf80980h ;unsigned long. WORD 0bf32h ;unsigned short. WORD 0101ah ;unsigned short BYTE 08bh,0bbh,0,0aah,0,030h,0ch,0abh ;unsigned char Data4[8]; ;0x7bf80980,0xbf32,0x101a,0x8b,0xbb,0,0xaa,0,0x30,0xc,0xab gppicturecheckmark DWORD 0 ;methods for ipicture interface.... GPget_Handle EQU 12 GPget_hPal EQU 16 GPget_Type EQU 20 GPget_Width EQU 24 GPget_Height EQU 28 GPRender EQU 32 GPset_hPal EQU 36 GPget_CurDC EQU 40 GPSelectPicture EQU 44 GPget_KeepOriginalFormat EQU 48 GPput_KeepOriginalFormat EQU 52 GPPictureChanged EQU 56 GPSaveAsFile EQU 60 GPget_Attributes EQU 64 .CODE ;my code has esi->object --don't worry about that. it has relevant info, as you can ;see in the comments. ;the BMP file has already been read into memory, and is residing at ;(OBJECT PTR [esi]).sztext+128 --again, this is not important, just my app. ;eax->menu description, menuitemid=ecx=menu item identifier... invoke AppendMenuA,helementsmenu,edx,menuitemid,eax ;v3.45 ;now insert a checkmark bitmap, if any... mov eax,(OBJECT PTR [esi]).fstatus and ax,800h ;bit11=1 if there is a checkmark bitmap. .IF ax != 0 mov eax,(OBJECT PTR [esi]).ximage ;holds gppicturecheckmark. .IF eax==0 ;bmp not loaded. ;we have bmp file in library-data-element.sztext+128. mov gppicturecheckmark,0 ;need to convert the mem-block into a "stream"... mov eax,(OBJECT PTR [esi]).nshape ;size of file. invoke GlobalAlloc,GMEM_FIXED,eax .IF eax != 0 ;ok mov edi,eax ;address of mem block. mov pmem,eax lea ebx,(OBJECT PTR [esi]).sztext add ebx,128 ;address of file in memory. mov ecx,(OBJECT PTR [esi]).nshape ;size of file. cpy1: mov al,[ebx] mov [edi],al inc ebx inc edi loop cpy1 invoke GlobalHandle,pmem mov hglobal,eax invoke CreateStreamOnHGlobal,hglobal,TRUE,ADDR pstm ;...returns pstm =ptr to stream. .IF eax != 0 ;eax=0 ok. xor eax,eax .ELSE mov ecx,(OBJECT PTR [esi]).nshape ;size of file. invoke OleLoadPicture,pstm,ecx,0,ADDR IID_IPicture,ADDR gppicturecheckmark ;..."IID_IPicture" is the type of interface pointer to return ;...The requested interface pointer is returned in "gpPicturecheckmark". ;returns eax=0 if ok ;note, need to release the iPicture in deletebitmaphandles() etc. .IF eax != 0 ;S_OK=0 xor eax,eax .ELSE mov eax,gppicturecheckmark .ENDIF push eax mov eax,pstm ;pstm->Release(); release the stream. push eax ;/ mov edx,[eax] ;/ call dword ptr [edx+8] ;/ pop eax .ENDIF push eax invoke GlobalFree,hglobal ;destroy temp mem block. pop eax .ENDIF mov (OBJECT PTR [esi]).ximage,eax ;gppicturecheckmark. .ENDIF .IF eax != 0 ;now get a GDI handle for the bitmap... mov edx,eax ;gppicturecheckmark lea eax,hbitmap ;gpPicture->get_Handle(&hbitmap) push eax ;/ push edx ;/ mov ecx,[edx] ;/ call dword ptr [ecx+GPget_Handle] ;/ mov eax,hbitmap ;returns hbitmap=0 if failed. ;now load the checkmark to the menuitem... .IF eax != 0 invoke SetMenuItemBitmaps,helementsmenu,menuitemid,MF_BYCOMMAND,eax,eax .ENDIF .ENDIF .ENDIF ;....... ;this is how to release the "gppicturecheckmark"... mov eax,(OBJECT PTR [esi]).ximage ;holds gppicturecheckmark. .IF eax != 0 mov edx,eax ;gppicturecheckmark->Release(); push edx ;/ "this" has to be passed, which is a ptr mov ecx,dword ptr [edx] ;/ to the interface. call dword ptr [ecx+8] ;/ mov (OBJECT PTR [esi]).ximage,0 .ENDIF ;...............