Hi RobertRioja,
another way that will answer your question, with exe inside resource.
Limits : Windows only, exe file size maxi about 3.999 Gb
In the PB file as well as in the Rc file, you must put the correct path of the file, for the resource file, \\ is the rule.
If you only have one file, MK-soft solution is a good one, if you have many files, resource file must be prefered. With resource file, only needed ones will be loaded.
As MK-sot says, Some virus scanner don't like it. So, a solution will be to first compress the exe file and then encrypt it .
When recreating it, do the operation backwards.
I've done some testes with PB 6 x64 C and i'ts Ok, exe file runs well.
First PB code (sorry, comment a almost in French but it's not a problem with inline tranlators)
Code: Select all
;https://www.purebasic.fr/english/viewtopic.php?p=588343#p588343
EnableExplicit
;- . ... Constantes
; id used to access the file specified in resource (rc file)
#RC_myfile_1000_abc = 1
;- .
;- . ... Structure
; to get resource values
Structure Fileinfo
*FirstAdress
*DataSize
EndStructure
;- .
;- . ... Macros
Macro _MakeIntResource(_value_)
(_value_ & $FFFF)
EndMacro
Macro _MakeLangID(_primary_, _sublang_)
(((_sublang_)<<10) | (_primary_))
EndMacro
;- .
;- . ... Procedures
; ------------ No Threadsafe
Procedure.i GetModuleHandle()
;// Mémorise l'instance
Static hInstance
Select hInstance
Case 0
hInstance = GetModuleHandle_(0)
EndSelect
ProcedureReturn hInstance
EndProcedure
Procedure.i Get_RC_DATA_Adress(id, *Fileinfo.Fileinfo)
;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;// FONCTION: Get_RC_DATA_Adress()
;// BUT: Retourne l'adresse de la ressource RC_DATA de l'id passé en paramètre
;// PARAMS: id - identifiant statique de la ressource RC_DATA
;// RETOURNE: Retourne le pointeur sur la mémoire de la resource spécifié en cas de succès
;// Retourne #True en cas d'erreur
;// Retourne #False en cas d'erreur
;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;// le handle du bloc d'informations de la ressource string
Protected findResource
;// le handle du bloc d'information des données de la ressource string
Protected loadResource_RT_RCDATA
;// l'adresse mémoire du block d'informations de la ressource chargée en mémoire Protected *lockResource
findResource = FindResourceEx_(#Null, #RT_RCDATA, _MakeIntResource(id), _MakeLangID(#LANG_NEUTRAL, #SUBLANG_NEUTRAL))
If findResource = #False
ProcedureReturn #False
EndIf
loadResource_RT_RCDATA = LoadResource_(GetModuleHandle(), findResource)
If loadResource_RT_RCDATA = #False
ProcedureReturn #False
EndIf
*Fileinfo\FirstAdress = LockResource_(loadResource_RT_RCDATA)
If *Fileinfo\FirstAdress = #False
ProcedureReturn #False
EndIf
; Fileinfo\DataSize is a long (MS Limitation), so be careful with ultra big file !
; most of the time, it's large enough
; maxi : 4294967295 byte about 3.99999 Gigabyte
*Fileinfo\DataSize = SizeofResource_(GetModuleHandle(), findResource)
If *Fileinfo\DataSize = 0
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure
;- .
Procedure Main()
Protected file.s = "G:\PureBasic\Applications codeurs\AddExe_To_Resource\xyz.exe"
Protected Fileinfo.Fileinfo
; Get File data from resource
If Get_RC_DATA_Adress(#RC_myfile_1000_abc, @Fileinfo) = #False
MessageRequester("Error","Failed to load file into memory")
End
EndIf
; Resource will be freeed by Windows, so do nothing
If CreateFile(0, file) = 0
MessageRequester("Error","Unable to create file")
End
EndIf
If WriteData(0, Fileinfo\FirstAdress, Fileinfo\DataSize) <> Fileinfo\DataSize
MessageRequester("Error","Data length written in the file is wrong")
End
EndIf
CloseFile(0)
EndProcedure
Main()
Then you have to attach a rc file with your PB code (menu compiler\compiler option\ and the select tab and put the rc file).
Here is an example, the file is txt format (end of line CRLF only, CR can cause problems), the txt file format is ANSI (may with another format it will be Ok but i do not tests).
#define LANG_NEUTRAL 0
#define SUBLANG_NEUTRAL 0
// id used by PB to find the specified file
#define RC_myfile_1000_abc 1
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
// exe file to put in
RC_myfile_1000_abc RCDATA "C:\\Users\\xxxx\\OneDrive\\Bureau\\PureIconManager_3_00_x64.exe"