Date: 02-16-2022
Return to Index
created by gbSnippets
'Sometimes a programmer wants to extract a resource to a file.
'To do this, include a resource with a "rcdata" type and compile
'the resource file. Here's an example:
camera rcdata "camera.wav"
'The resource can be loaded into memory and saved to a file using
'the following function.
'Primary Code:
'Credit: William Burns http://www.powerbasic.com/support/pbforums/showthread.php?t=23527
Function MakeFile(ResName AS String, NewFile AS String) As Long
Local lRet1 As Long,lRet2 As Long, dRet1 AS DWord,dRet2 AS DWord
Local sBuff AS String, szName As AsciiZ * 40, lFile As Long, 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
'now save the item from memory to a file
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
'Compilable Example: (Jose Includes)
'The gbsnippets.pbr has a file "alarm.wav" in it. This example shows
'how to save the resource to "alarm_extracted.wav"
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
Global hDlg as DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
If MakeFile("camera", "extracted.wav") <> 1 Then
MsgBox("Error creating resource file")
Else
MsgBox("Resource successfully saved")
End If
End If
End Function
Function MakeFile(ResName AS String, NewFile AS String) As Long
Local lRet1 As Long,lRet2 As Long, dRet1 AS DWord,dRet2 AS DWord
Local sBuff AS String, szName As AsciiZ * 40, lFile As Long, hProcess As Long
hProcess = 0 'use null for current process
szName = ResName 'change string to asciiz for api calls
lRet1 = FindResource(hProcess, szName, ByVal %RT_RCDATA)
If IsFalse lRet1 Then Function = 0:Exit Function 'could not find the res so exit
dRet1 = SizeofResource(hProcess,lRet1)
lRet2 = LoadResource(hProcess,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
'now save the item from memory to a file
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
'gbs_00351
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm