Monday, September 03, 2007

Creating a VFP Resource File

What is a resource file? A resource file is sort of a container file. Using resource file, you only have to distribute one single file instead of distribute many files. You can use this file to put any file such as image (BMP, JPG, PNG, etc). You can use this to distribute a hidden report file. You can also use this file in conjunction with my OwnerDrawn menu class. In short, you can use this file for many purposes.

How to create it? Very simple! Here is the step:
1. Create a new program (for the Main program)
2. Put a QUIT command into it
3. Create a new project.
4. Add the Main program into the project
5. Uncheck the project debug info
6. Build the project as an EXE, but name it as "myResource.LIB" (give the extension yourself)

Once you created your LIB file, how can you put your file into it? You must put the raw data into the resource file. There are three WinAPI functions to help you to put the raw data into it. BeginUpdateResource(), UpdateResource() and EndUpdateResource().

Let's look at the example. Suppose you want to put an image file ("myPNG.PNG") into it, here's how you do it:
[CODE]

*** Updated: Nov 23, 2007 - 07:45 AM

*** API Declaration
Declare Long BeginUpdateResource in Kernel32 ;
String cFileName, Long bDeleteExistingResources

Declare Long UpdateResource in Kernel32 as UpdateResourceStr ;
Long hUpdate, String lpType, String lpName, ;
Short wLanguage, String lpData, Long nLenData

Declare Long UpdateResource in Kernel32 as UpdateResourceStr2 ;
Long hUpdate, Long lpType, String lpName, ;
Short wLanguage, String lpData, Long nLenData

Declare Long EndUpdateResource in Kernel32 ;
Long hUpdate, Long fDiscard

*** Start code
cImageFile = 'myPNG.PNG'
cResFile = 'myResource.LIB'
cResGroup = 'MYIMAGES' && a resource section group, make uppercase
cResName = upper( JustStem( cImageFile ))
&& a resource name, make uppercase


cRawData = FileToStr( cImageFile )
hResFile = BeginUpdateResource( cResFile, .F. )

If (hResFile != 0)
UpdateResourceStr( hResFile, cResGroup, cResName, 0, ;
cRawData, len( cRawData))
EndUpdateResource( hResFile, .F. )

endif
[/CODE]

Suppose you want to put an image file that has a BMP extension, you better put it into the predefined resource section group. Change the code above into this:

[CODE]
*** Start code
cImageFile = 'myImage.BMP'
cResFile = 'myResource.LIB'
nResGroup = 2 && RT_BITMAP, a predefined resource section group
cResName = upper( JustStem( cImageFile ))
&& a resource name, make uppercase

cRawData = FileToStr( cImageFile )
cRawData = substr( cRawData, 15 ) && eliminate BITMAPFILEHEADER structure (14 chars)
hResFile = BeginUpdateResource( cResFile, .F. )
If (hResFile != 0)

UpdateResourceStr2( hResFile, nResGroup, cResName, 0, ;
cRawData, len( cRawData))
EndUpdateResource( hResFile, .F. )

endif
[/CODE]

That's it, try and experiment yourself. In the next tips, I will show you how to use the resource file.

Happy coding!




2 comments:

Anonymous said...

This is really fantastic !
Thanks very much for sharing.
Regards

Cesar Chalom

Herman Tan said...

Hi Cesar,

Thanks for your feedback!