How to extract BMPs from Visual FoxPro executables

Use this simple function to recover lost bitmaps from FoxPro APP and EXE files.

By Olaf Doschke

I recently had a problem with lost bitmap files. I had lost some BMP files from my FoxPro project folder, but I still had a VFP executable that included the embedded images. Was there a way of extracting the images from the EXE file and saving them back to separate files?

After some experimenting, I came up with a solution. I wrote a simple function that scans a VFP EXE or APP file, looking for embedded BMPs. It recognises these by the header "BM" followed by four bytes containing the file size.

To use it, call the ExtractBMPs function shown here. Pass the name of your executable file (qualified with a path if necessary), and the path to a folder where you want to save the bitmaps.

FUNCTION  ExtractBMPs()
LPARAMETERS tcVFPApp, tcDestinationImageFolder

LOCAL lcVFPApp, lnBMPPosition, lcBMPHeader, lnImageNo

lcVFPApp  = FILETOSTR(m.tcVFPApp)
lnImageNo = 1

DO WHILE .T.

  * Search for indicator of BMP file: BM
  lnBMPPosition = AT("BM", m.lcVFPApp, m.lnImageNo)
  IF m.lnBMPPosition = 0
    * No further occurrence found
    EXIT
  ENDIF

  * Read the 4 bytes containing file length from the BMP header
  lcBMPHeader = SUBSTR(m.lcVFPApp, m.lnBMPPosition + 2, 4)
  lnSize = CTOBIN(m.lcBMPHeader, "4RS")

  IF lnSize < 6 OR lnSize + lnBMPPosition > LEN(lcVFPApp)
    * Wrong size, so assume it's not a BMP file
  ELSE
    * Create a filename for the BMP file
    lcFilename = "image" + PADL(m.lnImageNo, 4, "0") + ".bmp"

    * Write the BMP to the file
    STRTOFILE(SUBSTR(m.lcVFPApp, m.lnBMPPosition ,m.lnSize), ;
      ADDBS(m.tcDestinationImageFolder) + m.lcFilename)
  ENDIF

  lnImageNo = m.lnImageNo + 1
  
ENDDO
ENDFUNC

I wrote this function as a tool for developers, so it doesn't do much in the way of error handling or progress reporting. Nevertheless, I hope it will help some other poor soul with the same problem.

June 2014

Please note: The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly with your sites or applications.

If you found this article useful, please tell your friends: