-
Sep 8th, 2017, 05:41 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] LoadResData problem (from DLL)
For all the toolbars etc. in my app I use PNG images.
Up to now they are embedded as resources in the app.
I have ~2000 PNGs, so the EXE becomes large.
So I want the resources in a separate DLL.
This gave me a start:
http://www.thevbzone.com/l_res.htm#USING RESOURCES LOCATED IN AN EXTERNAL DLL FILE
I verified the DLL has the resources I want to get, and if LoadLibrary was sucessful.
ResData = LoadResData("B", "PNG")
Throws error 326 (Resource not found).
Code:
Private Sub Command1_Click()
Const FILE_NAME As String = "ResDLL.dll"
Dim strFilePath As String
Dim hLibrary As Long
Dim ResData() As Byte
'--------------------------------------------------
' 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
ResData = LoadResData("B", "PNG")
' Close the Resource DLL
FreeLibrary hLibrary
End Sub
What do I do wrong?
Karl
ResTest.zip.docx
Last edited by Karl77; Sep 8th, 2017 at 06:00 AM.
Reason: Added the project
-
Sep 8th, 2017, 08:23 AM
#2
Re: LoadResData problem (from DLL)
Hi Karl. I have never tried using LoadResData this way, because when you're using an outside module, it generally makes sense to just use the FindResource and LoadResource APIs. These take an explicit module handle, which makes it clear which external DLL you intend to use.
But before attempting that - have you tried running your PNGs through a PNG optimizer? There are many ways to shrink PNG size, to reduce their impact on your .exe size. Googling "optimize PNG" will give you many different tools to do this. That may be an easier solution, and even if you do decide to move to an external resource-only DLL, I think you'll still appreciate having smaller PNG files.
-
Sep 8th, 2017, 08:30 AM
#3
Hyperactive Member
Re: LoadResData problem (from DLL)
try this class
cResource.cls
-
Sep 8th, 2017, 08:43 AM
#4
Thread Starter
Fanatic Member
Re: LoadResData problem (from DLL)
Originally Posted by Tanner_H
Hi Karl. I have never tried using LoadResData this way, because when you're using an outside module, it generally makes sense to just use the FindResource and LoadResource APIs. These take an explicit module handle, which makes it clear which external DLL you intend to use.
Ok, I was blind.
In the end of the article, they provide a ready made class.
With a function 'DLL_LoadData'.
It accepts Resource IDs as integer only, but that can be changed easily.
The functions returns a datapointer.
In order to (later) make a StdPicture, I need a byte array.
How do I do this?
But before attempting that - have you tried running your PNGs through a PNG optimizer? There are many ways to shrink PNG size, to reduce their impact on your .exe size. Googling "optimize PNG" will give you many different tools to do this. That may be an easier solution, and even if you do decide to move to an external resource-only DLL, I think you'll still appreciate having smaller PNG files.
Thanks for the hint.
I'll try when my main thing runs...
-
Sep 8th, 2017, 08:51 AM
#5
Thread Starter
Fanatic Member
Re: LoadResData problem (from DLL)
Originally Posted by yokesee
That one is better, it already accepts strings as IDs.
But the same problem (for me):
How to get the data into a byte array?
Last edited by Karl77; Sep 8th, 2017 at 09:10 AM.
-
Sep 8th, 2017, 09:16 AM
#6
Thread Starter
Fanatic Member
Re: LoadResData problem (from DLL)
Originally Posted by Tanner_H
I think you'll still appreciate having smaller PNG files.
Tested 1116 files, all icons 24x24 to 64x64.
Before: 1.143.945 Bytes
After: 1.038.848 Bytes
Only ~10%.
-
Sep 8th, 2017, 09:18 AM
#7
Re: LoadResData problem (from DLL)
Originally Posted by Karl77
That one is better, it already accepts strings as IDs.
But the same problem (for me):
How to get the data into a byte array?
The DLL_LoadData function in that class returns a source pointer and a size. The simplest way to convert that into a VB array is...
1) Create a new array at the same size:
Code:
Dim myArray() As Byte
ReDim myArray(0 to Return_DataSize-1) as Byte
2) Use CopyMemory (RTLMoveMemory) to copy the bytes into place:
Code:
'Declaration goes at the top of your form/module
Private Declare Function CopyMemory_Strict Lib "kernel32" Alias "RtlMoveMemory" (ByVal ptrDst As Long, ByVal ptrSrc As Long, ByVal numOfBytes As Long) As Long
'After the ReDim statement shown in (1)...
CopyMemory_Strict VarPtr(myArray(0)), Return_DataPointer, Return_DataSize
Now you have the data in a standard VB array.
-
Sep 8th, 2017, 09:22 AM
#8
Re: LoadResData problem (from DLL)
Originally Posted by Karl77
But the same problem (for me):
How to get the data into a byte array?
There is a DLL_LoadData function in that class. That function returns a pointer and size of data. Armed with those two pieces of information, you can resize an array and use CopyMemory to transfer the data from pointer to array.
Ugh. Tanner beat me to it.
-
Sep 8th, 2017, 09:26 AM
#9
Thread Starter
Fanatic Member
Re: LoadResData problem (from DLL)
Originally Posted by Tanner_H
Now you have the data in a standard VB array.
Thank you, I understood.
-
Sep 8th, 2017, 09:32 AM
#10
Re: LoadResData problem (from DLL)
You're welcome. RTLMoveMemory is a worthwhile API to keep around. It is useful in many different scenarios, I think.
Originally Posted by Karl77
Tested 1116 files, all icons 24x24 to 64x64.
Before: 1.143.945 Bytes
After: 1.038.848 Bytes
Only ~10%.
This doesn't mean much without saying what PNG optimizer you used! (And I'm not sure what "only" 10% means. Isn't a 10% reduction - for free - pretty nice? )
As I said, there are many different PNG optimizers. Some are quite aggressive (e.g. pngquant). Others are not (e.g. OptiPNG). There are many options out there.
-
Sep 8th, 2017, 11:29 AM
#11
Thread Starter
Fanatic Member
Re: LoadResData problem (from DLL)
Originally Posted by Tanner_H
This doesn't mean much without saying what PNG optimizer you used! (And I'm not sure what "only" 10% means. Isn't a 10% reduction - for free - pretty nice? )
As I said, there are many different PNG optimizers. Some are quite aggressive (e.g. pngquant). Others are not (e.g. OptiPNG). There are many options out there.
I tried with this "Free PNG Optimizer 2.2.0" highest optimization level.
And yes, 10% is not only "only".
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|