 |
|
 |
ryansanders2002 Level: Big Cheese
 Registered: 26-10-2004 Posts: 21
|
Loading resources from dll
I need some help with loading resources from a dll. I found this code.
Option Explicit
Private Type BITMAP
bmType As Long 'LONG
bmWidth As Long 'LONG
bmHeight As Long 'LONG
bmWidthBytes As Long 'LONG
bmPlanes As Integer 'WORD
bmBitsPixel As Integer 'WORD
bmBits As Long 'LPVOID
End Type
Private Const WM_SETICON = &H80
Private Const ICON_BIG = 1
Private Const SND_APPLICATION = &H80
Private Const SND_ALIAS = &H10000
Private Const SND_ALIAS_ID = &H110000
Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000
Private Const SND_LOOP = &H8
Private Const SND_MEMORY = &H4
Private Const SND_NODEFAULT = &H2
Private Const SND_NOSTOP = &H10
Private Const SND_NOWAIT = &H2000
Private Const SND_PURGE = &H40
Private Const SND_RESOURCE = &H40004
Private Const SND_SYNC = &H0
Private Declare Function FindResource Lib "KERNEL32" Alias "FindResourceA" (ByVal hLib As Long, _
ByVal strName As String, ByVal strType As String) As Long
Private Declare Function FreeLibrary Lib "KERNEL32" (ByVal hLib As Long) As Long 'BOOL
Private Declare Function LoadLibrary Lib "KERNEL32" Alias "LoadLibraryA" ( _
ByVal strFilePath As String) As Long
Private Declare Function LoadBitmap Lib "USER32" Alias "LoadBitmapA" (ByVal hInstance As Long, _
ByVal lngBitmapID As Long) As Long
Private Declare Function LoadCursor Lib "USER32" Alias "LoadCursorA" (ByVal hLib As Long, _
ByVal lngCursorID As Long) As Long
Private Declare Function LoadIcon Lib "USER32" Alias "LoadIconA" (ByVal hLib As Long, _
ByVal lngIconID As Long) As Long
Private Declare Function LoadString Lib "USER32" Alias "LoadStringA" (ByVal hLib As Long, _
ByVal ResourceID As Long, ByVal lpBuffer As String, ByVal nBufferSize As Long) As Long
Private Declare Function LoadResource Lib "KERNEL32" (ByVal hLib As Long, _
ByVal hRes As Long) As Long
Private Declare Function LockResource Lib "KERNEL32" (ByVal hRes As Long) As Long
Private Declare Function SizeofResource Lib "KERNEL32" (ByVal hModule As Long, _
ByVal hResInfo As Long) As Long
Private Declare Function PlaySound Lib "WINMM.DLL" Alias "PlaySoundA" (ByRef Sound As Any, _
ByVal hLib As Long, ByVal lngFlag As Long) As Long 'BOOL
Private Declare Function SendMessage Lib "USER32.DLL" Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function SetCursor Lib "USER32.DLL" (ByVal hCursor As Long) As Long
Private Declare Function BitBlt Lib "GDI32" (ByVal hDC_Destination As Long, _
ByVal X_Dest As Long, ByVal Y_Dest As Long, ByVal Width_Dest As Long, _
ByVal Height_Dest As Long, ByVal hDC_Source As Long, ByVal X_Src As Long, _
ByVal Y_Src As Long, ByVal RasterOperation As Long) As Long
Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As Long
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As Long
Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hGDIObj As Long) As Long
Private Declare Function DeleteObject Lib "GDI32" (ByVal hGDIObj As Long) As Long
Private Declare Function GetObjectAPI Lib "GDI32" Alias "GetObjectA" (ByVal hObject As Long, _
ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Sub Form_Load()
Const FILE_NAME As String = "Project1.dll"
Dim strFilePath As String
Dim hLibrary As Long
Dim hResource As Long
Dim hData As Long
Dim lpData As Long
Dim hIcon As Long
Dim hCursor As Long
Dim hBitmap As Long
Dim strString As String
Dim lngStringLen As Long
Dim BitmapInfo As BITMAP
Dim hDC_Screen As Long
Dim hDC_Temp As Long
Dim hBMP_Prev As Long
Me.Show
Me.AutoRedraw = True
' Get the path to the Resource DLL
strFilePath = App.Path
If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"
strFilePath = strFilePath & FILE_NAME
' Load the Resource DLL
hLibrary = LoadLibrary(strFilePath & Chr(0))
If hLibrary = 0 Then
MsgBox "Failed to load the specified library with error code " & Err.LastDllError
Exit Sub
End If
' Get an icon from the Resource DLL
hIcon = LoadIcon(hLibrary, 101)
If hIcon <> 0 Then SendMessage Me.hWnd, WM_SETICON, ICON_BIG, ByVal hIcon
' Get a cursor from the Resource DLL
hCursor = LoadCursor(hLibrary, 101)
If hCursor <> 0 Then SetCursor hCursor
' Get a string from the Resource DLL
strString = String(256, Chr(0))
lngStringLen = LoadString(hLibrary, 101, strString, Len(strString))
If lngStringLen <> 0 Then Me.Caption = Left(strString, lngStringLen)
' Get a bitmap from the Resource DLL
hBitmap = LoadBitmap(hLibrary, 101)
If hBitmap <> 0 Then
GetObjectAPI hBitmap, Len(BitmapInfo), BitmapInfo
hDC_Screen = GetDC(0)
hDC_Temp = CreateCompatibleDC(hDC_Screen)
hBMP_Prev = SelectObject(hDC_Temp, hBitmap)
BitBlt Me.hDC, 0, 0, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hDC_Temp, 0, 0, vbSrcCopy
Me.Refresh
SelectObject hDC_Temp, hBMP_Prev
DeleteDC hDC_Temp
ReleaseDC 0, hDC_Screen
End If
' Get a .WAV file from the Resource DLL
hResource = FindResource(hLibrary, "#" & CStr(1) & Chr(0), "WAVE" & Chr(0))
If hResource <> 0 Then
hData = LoadResource(hLibrary, hResource) 'This gets a handle to the data
If hData <> 0 Then
lpData = LockResource(hData) 'This gets a POINTER to the data... which is what we need
If lpData <> 0 Then
PlaySound ByVal lpData, 0, SND_MEMORY Or SND_NODEFAULT Or SND_SYNC
End If
End If
End If
' Close the Resource DLL
FreeLibrary hLibrary
End Sub
|
my problem is with ' Get a bitmap from the Resource DLL
hBitmap = LoadBitmap(hLibrary, 101)
If hBitmap <> 0 Then
GetObjectAPI hBitmap, Len(BitmapInfo), BitmapInfo
hDC_Screen = GetDC(0)
hDC_Temp = CreateCompatibleDC(hDC_Screen)
hBMP_Prev = SelectObject(hDC_Temp, hBitmap)
BitBlt Me.hDC, 0, 0, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hDC_Temp, 0, 0, vbSrcCopy
Me.Refresh
SelectObject hDC_Temp, hBMP_Prev
DeleteDC hDC_Temp
ReleaseDC 0, hDC_Screen
End If |
This loads the picture into the background. I want to be able to load pictures into pictureboxes.
I tired picture1.picture=hbitmap but it gives me a type mismatch error. How do I change that code to load into pictureboxes.
[Edited by ryansanders2002 on 27-05-2005 at 06:18 PM GMT]
|
|
27-05-2005 at 11:16 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: Loading resources from dll
Change
| BitBlt Me.hDC, 0, 0, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hDC_Temp, 0, 0, vbSrcCopy |
into
| BitBlt Picture1.hDC, 0, 0, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hDC_Temp, 0, 0, vbSrcCopy |
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
28-05-2005 at 12:13 AM |
|
|
ryansanders2002 Level: Big Cheese
 Registered: 26-10-2004 Posts: 21
|
Re: Loading resources from dll
thanks..
Now I have a really strange issue with the string loading. I load all my strings into an array. This works perfectly. Now when I do something like:
MsgBox (strString(1) & vbCrLf & _
strString(2))
only the value of strString(1) appears. but strString(2) has a value. What the heck is going on?
|
|
28-05-2005 at 01:50 AM |
|
|
humberto Level: VB Lord
 Registered: 13-01-2005 Posts: 246
|
Re: Loading resources from dll
how is your array..?
if the array is (0 to 1)
MsgBox (strString(0) & vbCrLf & _
strString(1))
|
|
28-05-2005 at 02:01 AM |
|
|
ryansanders2002 Level: Big Cheese
 Registered: 26-10-2004 Posts: 21
|
Re: Loading resources from dll
I got it. I had to change the way the resource was saved to the array. Don't get it but its all good now.
|
|
28-05-2005 at 03:16 AM |
|
|
|
|
 |
 |