VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Save any embedded resource in your EXE file as an independent file

by Byte Masters TEAM (4 Submissions)
Category: Files/File Controls/Input/Output
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 16th February 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Save any embedded resource in your EXE file as an independent file

Rate Save any embedded resource in your EXE file as an independent file



a VB6 resource file and save the resource to disk as an
independent file.

Usage instructions :

Open the resource file in your project with "Resource Editor"
Click "Add Custom resource" and Add the file that you want to embed in your
Final EXE file.
Now Double-Click the newly added resource and remember the "Type" and "ID" values
These 2 values will be used when saving this resource as a file and you can change them
to what ever you want.

The fucntion SaveRestoFile takes 3 params

1- ResID  : ID of embedded resource
2- ResType  : Type of embedded resource
3- FilePath  : where will resource will be saved * Directory must exist !

The return of this function is a boolean value to report success or failure.

Now Paste this Code i your Project :

----------------------------------------------------------------------------------------------------------------------------
IN a Module :
----------------------------------------------------------------------------------------------------------------------------

Option Explicit


Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const CREATE_ALWAYS = 2
Private Const CREATE_NEW = 1

Public Function SaveRestoFile(ResID As Long, ResType As String, FilePath As String) As Boolean

Dim ArrayOfBits()           As Byte
Dim hFile                   As Long
Dim lBytesWritten           As Long

    On Local Error Resume Next

    ReDim ArrayOfBits(Len(LoadResData(ResID, ResType)))
    ArrayOfBits = LoadResData(ResID, ResType)

 
        hFile = CreateFile(FilePath, GENERIC_WRITE Or GENERIC_READ, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
        If hFile <> -1 Then
           If WriteFile(hFile, ArrayOfBits(0), UBound(ArrayOfBits) + 1, lBytesWritten, 0) Then
              FlushFileBuffers hFile
              CloseHandle hFile
              SaveRestoFile = True
           End If
        End If


End Function



----------------------------------------------------------------------------------------------------------------------------
IN a Form  :
----------------------------------------------------------------------------------------------------------------------------
Private Sub Command1_Click()

'Save the backup INI file in case the original file was corrupted or missing !

SaveRestoFile 101, "INI", "C:\Settings.ini"

End Sub




Download this snippet    Add to My Saved Code

Save any embedded resource in your EXE file as an independent file Comments

No comments have been posted about Save any embedded resource in your EXE file as an independent file. Why not be the first to post a comment about Save any embedded resource in your EXE file as an independent file.

Post your comment

Subject:
Message:
0/1000 characters