Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing bitmaps from a .dll file

Status
Not open for further replies.

markbellki

Technical User
Jan 21, 2003
7
AU
I would like to be able to extract bitmaps that have been stored in a .dll file. Does anyone have any ideas on how to do this?

Thanks.
 
- Load the DLL using LoadLibraryEx with LOAD_LIBRARY_AS_DATAFILE flag.
- Call EnumResourceNames with RT_BITMAP as lpType to enumerate all the bitmaps in the resource.
- In the EnumResNameProc callback procedure, use the LoadBitmap function to load the bitmap in memory.

Here is an example of this program, it loads and displays all the bitmaps in shell32.dll. To check this program, add a code module to a standard EXE project and paste the following code in it.
Change the Startup Object of the project to Sub Main and run the program.

The code goes here...
-----------------------------------------------------------
'Code to be pasted in module.
Declare Function LoadLibraryEx Lib "kernel32" Alias "LoadLibraryExA" (ByVal lpLibFileName As String, ByVal hFile As Long, ByVal dwFlags As Long) As Long
Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Declare Function EnumResourceNames Lib "kernel32" Alias "EnumResourceNamesA" (ByVal hModule As Long, lpType As Any, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function LoadBitmap Lib "user32" Alias "LoadBitmapA" (ByVal hInstance As Long, lpBitmapName As Any) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicArray As Long, RefIID As Any, ByVal OwnsHandle As Long, IPic As Any) As Long
Const LOAD_LIBRARY_AS_DATAFILE = 2
Const RT_BITMAP = &H2&

Dim hModule As Long
Public Sub Main()
hModule = LoadLibraryEx("shell32.dll", 0, LOAD_LIBRARY_AS_DATAFILE)
EnumResourceNames hModule, ByVal RT_BITMAP, AddressOf EnumResNameProc, 0
FreeLibrary hModule
End Sub

Function EnumResNameProc(ByVal hModule As Long, ByVal lpType As Long, ByVal lpszName As Long, ByVal lParam As Long) As Long
Dim F As New Form1, hBmp As Long
F.Show
F.Caption = lpszName
hBmp = LoadBitmap(hModule, ByVal lpszName)
F.Picture = HandleToPicture(hBmp)
DeleteObject hBmp
EnumResNameProc = True
End Function

'Makes a Visual Basic picture object from a GDI object handle.
Function HandleToPicture(ByVal Handle As Long, Optional ByVal PicType As PictureTypeConstants = vbPicTypeBitmap) As Object
Dim P(0 To 4) As Long, G(0 To 15) As Byte
G(1) = 4: G(2) = 2: G(8) = 192: G(15) = 70
P(0) = 20: P(1) = PicType: P(2) = Handle
OleCreatePictureIndirect P(0), G(0), 1, HandleToPicture
End Function
 
I made a small mistake. Please omit the following line from the EnumResNameProc function.
Code:
DeleteObject hBmp 'remove this line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top