Accessing embedded files can have problems


FoxPro allows users to embed files into an APP or EXE file using the project manager. That means an icon or JPG can be embedded inside so it can’t be used externally except by the application. However, there is a bug in some file functions that attempt to read the embedded file in certain scenarios. A simple workaround is to make a temporary copy of the file on disk and use that copy. Another workaround is to keep the file on disk and not to embed the file at all.


 


The code below builds a project called Test2, embeds an icon file, builds test2.exe, and runs it. The code opens and displays the sizes of the embedded icon file and the copy on disk. If you run it, the Embedded Size will show some random number, whereas one would expect the two to be the same.


 


SET SAFETY OFF


CLOSE DATABASES all


CLEAR


ERASE test2.*


 


#define ICONFILE HOME(0)+”graphics\icons\computer\disk01.ico”


#define PHYSICON “temp.ico”


COPY FILE ICONFILE TO PHYSICON      && make temporary copy that is physically on disk


 


TEXT  to xmyvar   && create code for test2.exe


      hnd=FOPEN(ICONFILE,10)  && open unbuffered, readonly


      nSize=FSEEK(hnd,0,2)    && Seek to EOF returns file size


      FCLOSE(hnd)


      hnd=FOPEN(PHYSICON,10)  && open unbuffered, readonly


      nSize2=FSEEK(hnd,0,2)


      FCLOSE(hnd)


      MESSAGEBOX(“Embedded Size = “+TRANSFORM(nSize)+” Size on Disk=”+TRANSFORM(nSize2),0,_vfp.FullName)


ENDTEXT


 


STRTOFILE(xmyvar,”test2.prg”)


BUILD PROJECT test2 FROM test2


MODIFY PROJECT test2 nowait


_vfp.ActiveProject.Files.Add(ICONFILE)    && embed the icon. Comment out this line and it works


_vfp.ActiveProject.Close


 


BUILD EXE test2 FROM test2


!/n test2


 

Comments (3)

  1. Malcolm Greene says:

    Calvin,

    Would this file bug qualify as a Sedna type service pack fix?

    I enjoy reading your blog – you’re posts always inspire some new code on my end.

    Thanks,

    Malcolm

  2. Calvin_Hsia says:

    Consideration for fixing depends on many things: impact on product, severity of problem, severity of fix, available workarounds, etc.

    This particular issue was reported as a problem with BUILDING an EXE from within an EXE using an embedded Icon inside the EXE

    I’m glad you like my blog. Positive feedback encourages more production!

    Thanks Malcolm

  3. Cristi Popa says:

    Calvin,

    I learned allot from your examples, unfortunately I cannot pass over this one.

    Using your example I always get an error message at the "Copy file" program line.

    using your:

    #define ICONFILE "iconscomputerdisk01.ico", and running the exe from c:MyExe, the error message looks like:

    File ‘c:MyExeiconscomputerdisk01.ico’ does not exist.

    If I run the exe in the project folder, where the exe was built, the exe runs fine (the exe finds the ico file in the right folder). But if I move the exe in a different folder, where the  ico file does not physically exists, I always get this error.

    What could be the problem?

    I use VFP 9.

    Thanks