Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Embedding and Saving files from your exes RCDATA resources

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

  • Embedding and Saving files from your exes RCDATA resources

    The following include file shows how easy it is to embed a file in your exes
    resource file, so that it can be saved by the program when it wishs. I create
    a lot of small utilities. And I like the option of just sending one small exe
    file. However it is sometimes necessary to have an external file included with
    your program. So I like the idea of still having one small exe file that can
    check for the needed file when it first runs and create it if necessary from the
    file it has stored in its own resource.

    This technique will work on all kinds of files(even binary) I have used it
    for DLL, EXE, DAT, INI, TXT, etc...

    Here is the MakeFile.INC
    Code:
    '************************************************************************
    ' MakeFile.Inc   - By William Burns  01/15/02
    '
    ' This function will allow you to extract a file saved in your resource file as RCDATA.
    '
    ' To Use:
    ' 1.) Create and compile an RC file with a line like:
    '                INEED RCDATA DISCARDABLE "c:\INEED.DLL"   'add another line for each file
    '
    ' 2.) Then include this file in your program using a line like: $INCLUDE "MakeFile.INC"
    '     Also include your resource file using a line like:        #RESOURCE "dWare95.PBR"
    '
    ' 3.) Then use the following line in your program to create the file:
    '  IF MakeFile("INEED","c:\path\MYNEWFILE.DLL") <> 1 THEN MSGBOX "Failed to create MYNEWFILE.DLL",,"Error"
    '
    '************************************************************************
    
    #IF NOT %DEF(%WINAPI)
        #INCLUDE "win32api.inc"                 'add win32api if not already added
    #ENDIF
    
    DECLARE FUNCTION MakeFile(ResName AS STRING, NewFile AS STRING) AS LONG
    
    '************************************************************************
    FUNCTION MakeFile(ResName AS STRING, NewFile AS STRING) AS LONG
        LOCAL lRet1   AS LONG,lRet2  AS LONG
        LOCAL dRet1   AS DWORD,dRet2 AS DWORD
        LOCAL sBuff   AS STRING
        LOCAL szName  AS ASCIIZ * 40
        LOCAL lFile   AS LONG
        LOCAL hDlg    AS LONG
        hDlg = 0                     'use null for current process
        szName = ResName             'change string to asciiz for api calls
        lRet1 = FindResource(hDlg, szName, BYVAL %RT_RCDATA)
        IF ISFALSE lRet1 THEN FUNCTION = 0:EXIT FUNCTION   'could not find the res so exit
        dRet1 = SizeofResource(hDlg,lRet1)
        lRet2 = LoadResource(hDlg,lRet1)
        IF ISFALSE lRet2 THEN FUNCTION = 0:EXIT FUNCTION   'could not load res to mem so exit
        dRet2 = LockResource(lRet2)
        sBuff = PEEK$(dRet2, dRet1)    ' load sBuff with the info we just put in memory
    
        lFile = FREEFILE                            'find next open file number
        OPEN NewFile FOR BINARY AS #lFile           'create the new file
            PUT$ #lFile, sBuff                      'write the buffer to the file
        CLOSE #lFile                                'close the file
        FUNCTION = 1                                'worked so return a 1
    END FUNCTION
    '************************************************************************

    To show how easy it is to use, here is a small program example that creates 2 files:
    Code:
    #COMPILE EXE
    #RESOURCE "MyResFile.PBR"
    $INCLUDE "MakeFile.INC"
    FUNCTION PBMAIN () AS LONG
      IF MakeFile("MYINI","c:\MySet.INI") <> 1 THEN MSGBOX "Failed to create MySet.ini",,"Error"
      IF MakeFile("MYDLL","c:\windows\system\DTYTRM.DLL") <> 1 THEN MSGBOX "Failed to create DTYTRM.DLL",,"Error"
    END FUNCTION
    Here is the MyResFile.RC file used in the sample above:
    Code:
    #include "resource.h"           
    MYINI RCDATA DISCARDABLE "MySet.INI"
    MYDLL RCDATA DISCARDABLE "DTYTRM.DLL"
    ------------------

    "I haven't lost my mind... its backed up on tape... I think??"



    [This message has been edited by William Burns (edited September 25, 2002).]
    "I haven't lost my mind... its backed up on tape... I think??" :D
Working...
X