Announcement

Collapse
No announcement yet.

Including binary files in a PB EXE

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Including binary files in a PB EXE

    A few people have asked me about the code that I mentioned in another
    context with the topic that E.B.Knoppert had posted so I thought it was
    reasonable to post it for anyone to use.

    It is a very simple technique for including any file type within a
    PowerBASIC EXE file by treating it as a binary resource. The file to be
    included does not need to be modified in any way as the API call that gets
    it treats it as a sequence of bytes and not as a preset resource type.

    Below are te 3 necessary files to build and run the demo, all you need to
    do is select the file you wish to include in the EXE file, put its name in
    the resource script and importantly, get its BYTE length and put it in the
    EXE file. The name you set in the EXE file need not be the same as the
    name in the resource script as it will be writen to disk at whatever name
    you select.

    I originally had this method in mind for installations so that the package
    was not a gaggle of junk but of course it is not limited to an application
    of that type. One thing you can do with it is store a DLL you have written
    and tested and write it to disk on startup and use it as normal. This
    means you must use LoadLibrary() and GetProcAddress() to call its
    functions but it is no big deal to do and you don't have to wait for some
    whizz bang technology to do it, it works fine now.

    Regards,

    [email protected]

    Code:
      
      DOS batch file ..........
      
      @echo off
      
      : ----------------------------------------
      : Change the paths to your own local paths
      : ----------------------------------------
      d:\pb6\bin\rc /v demo.rc
      d:\pb6\bin\pbres demo.res
      d:\pb6\bin\pbdll /l /q demo.bas
      
      type demo.log
      
      pause
      
      
      
      Resource script ..........
      
      //##########################################################################
      
        // ----------------------------
        // Change this to your own path
        // ----------------------------
          #include "D:\PB6\Winapi\Resource.h"
      
      // The ID number "2" is the one used by the API call in the .BAS file.
      
          2 RCDATA DISCARDABLE "yourfile.ext"
      
      //##########################################################################
      
    
      
      PowerBASIC source file ..........
      
      '##########################################################################
      
          #COMPILE EXE
          #RESOURCE "demo.pbr"
      
        ' ----------------------------
        ' Change this to your own path
        ' ----------------------------
          #INCLUDE "d:\pb6\winapi\win32api.inc"
      
          GLOBAL hInstance as LONG
      
      '##########################################################################
      
      FUNCTION WinMain(ByVal Instance      as LONG, _
                       ByVal hPrevInstance as LONG, _
                       lpszCmdLine         as ASCIIZ PTR, _
                       ByVal nCmdShow      as LONG) AS LONG
      
          LOCAL fname as ASCIIZ * 24
          fname = "yourfile.???"  ' << put the name you wish to write to disk here.
          fsize& = ????           ' << put the actual BYTE length of the file here.
      
          hInstance = Instance
      
          hRsrc& = FindResource(hInstance,ByVal 2,ByVal %RT_RCDATA)
      
          If hRsrc& = 0 Then
            MessageBox 0,"Resource failed to load","Test Value",%MB_OK
            ! jmp The_End
          Else
            MessageBox 0,"Resource loaded correctly","Test Value",%MB_OK
          End If
      
          lpRes& = LoadResource(hInstance,hRsrc&)
      
          hFile& = CreateFile(fname,%GENERIC_WRITE, _
                              ByVal %NULL,ByVal %NULL,%CREATE_ALWAYS, _
                              %FILE_ATTRIBUTE_NORMAL,ByVal %NULL)
      
          lFile& = 0
      
          WriteFile hFile&,ByVal lpRes&,fsize&,ByVal varPtr(lFile&),ByVal %NULL
      
          MessageBox 0,"File written to disk", _
                     "Finish",%MB_ICONINFORMATION
      
          CloseHandle hFile&
      
          The_End:
      
          FUNCTION = 0
      
      END FUNCTION
      
      '##########################################################################
    hutch at movsd dot com
    The MASM Forum - SLL Modules and PB Libraries

    http://www.masm32.com/board/index.php?board=69.0

  • #2
    I took this a bit further.
    We used our update exe's this way.
    A simple resource file but MS- compressed.
    It decompresses it to install.

    Very speedy with PB6/createfile!

    Yes, running a dll using call dword could do, but it ain't static use.

    ------------------

    Comment

    Working...
    X