Results 1 to 11 of 11

Thread: [RESOLVED] LoadResData problem (from DLL)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    524

    Resolved [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.
    Name:  VBF.jpg
Views: 390
Size:  18.9 KB


    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

  2. #2
    Fanatic Member
    Join Date
    Aug 2013
    Posts
    806

    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.
    Check out PhotoDemon, a pro-grade photo editor written completely in VB6. (Full source available at GitHub.)

  3. #3
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    EspaƱa
    Posts
    508

    Re: LoadResData problem (from DLL)

    try this class

    cResource.cls

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    524

    Re: LoadResData problem (from DLL)

    Quote Originally Posted by Tanner_H View Post
    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...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    524

    Re: LoadResData problem (from DLL)

    Quote Originally Posted by yokesee View Post
    try this class
    cResource.cls
    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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    524

    Re: LoadResData problem (from DLL)

    Quote Originally Posted by Tanner_H View Post
    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%.

  7. #7
    Fanatic Member
    Join Date
    Aug 2013
    Posts
    806

    Re: LoadResData problem (from DLL)

    Quote Originally Posted by Karl77 View Post
    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.
    Check out PhotoDemon, a pro-grade photo editor written completely in VB6. (Full source available at GitHub.)

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: LoadResData problem (from DLL)

    Quote Originally Posted by Karl77 View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    524

    Re: LoadResData problem (from DLL)

    Quote Originally Posted by Tanner_H View Post
    Now you have the data in a standard VB array.
    Thank you, I understood.

  10. #10
    Fanatic Member
    Join Date
    Aug 2013
    Posts
    806

    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.

    Quote Originally Posted by Karl77 View Post
    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.
    Check out PhotoDemon, a pro-grade photo editor written completely in VB6. (Full source available at GitHub.)

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    524

    Re: LoadResData problem (from DLL)

    Quote Originally Posted by Tanner_H View Post
    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
  •  



Click Here to Expand Forum to Full Width