Search code examples
vb6embedded-resource

How to use embedded resource to copy file without ever making a local copy?


If you have an embedded resource in your VB6 project that contains a binary file, what code would cause this file to be copied to another location, without ever making a copy of the file on the local system?

I have done something like this before in .NET but I fear it is not possible in VB6.


Solution

  • From http://support.microsoft.com/kb/q194409/ :

     Public Function SaveResItemToDisk( _
                    ByVal iResourceNum As Integer, _
                    ByVal sResourceType As String, _
                    ByVal sDestFileName As String _
                    ) As Long
            '=============================================
            'Saves a resource item to disk
    
        'Returns 0 on success, error number on failure
        '=============================================
    
        'Example Call:
        ' iRetVal = SaveResItemToDisk(101, "CUSTOM", "C:\myImage.gif")
    
        Dim bytResourceData()   As Byte
        Dim iFileNumOut         As Integer
    
        On Error GoTo SaveResItemToDisk_err
    
        'Retrieve the resource contents (data) into a byte array
        bytResourceData = LoadResData(iResourceNum, sResourceType)
    
        'Get Free File Handle
        iFileNumOut = FreeFile
    
        'Open the output file
        Open sDestFileName For Binary Access Write As #iFileNumOut
    
            'Write the resource to the file
            Put #iFileNumOut, , bytResourceData
    
        'Close the file
        Close #iFileNumOut
    
        'Return 0 for success
        SaveResItemToDisk = 0
    
        Exit Function
    SaveResItemToDisk_err:
        'Return error number
        SaveResItemToDisk = Err.Number
    End Function