Page 1 of 1

[windows] Tip: Attaching files to your executable

Posted: Wed Sep 11, 2002 6:21 am
by BackupUser
Code updated for 5.20+

Restored from previous forum. Originally posted by PB.

> Can somebody give me a quick example of including the dll and the
> writing it to the users temp drectory and then deleting it after I
> am through using it.

Here's how I've always done it. If you're including an EXE or DLL,
it's a good idea to compress it before including it to make your
executable smaller... just something like UPX from this web site:
http://upx.sourceforge.net/

Code: Select all

If OpenWindow(0,200,200,500,200,"Test",#PB_Window_SystemMenu)
  
  TextGadget(1,20,20,450,21,"")
  ;
  ; Get the official temp folder for the current user.
  tmpdir$=Space(255)
  GetTempPath_(255,tmpdir$)
  
  If Right(tmpdir$,1) = "\" 
    tmpdir$=tmpdir$+"\" 
  EndIf
  ;
  ; Now we write the included file (in this case, a fake DLL) to this folder.
  ;
  Repeat
    ;
    ; Next line chooses a random filename for the file.  We include our app name so that if the
    ; deletion of it fails later, a power user will recognize that the file belongs to our app
    ; and can be safely deleted if desired.
    ;
    tmpfile$=tmpdir$+"AppName"+Str(Random(9999))
    ;
  Until FileSize(tmpfile$)=-1 ; Make sure temp filename doesn't already exist.
  ;
  ; Now write the temp file to the temp folder.
  ;
  If CreateFile(0,tmpfile$)
    WriteData(0,?StartTestFile,?EndTestFile-?StartTestFile)
    CloseFile(0)
    ;
    ; Even mark it as "hidden" if you're really paranoid.  :)
    SetFileAttributes_(tmpfile$,#FILE_ATTRIBUTE_HIDDEN)
  EndIf
  ;
  SetGadgetText(1,"Temp file created as: "+Chr(34)+tmpfile$+Chr(34))
  ;
  Repeat
    ev=WaitWindowEvent()
  Until ev=#PB_Event_CloseWindow
  ;
  ; Delete temp file when user closes this app.
  ;
  SetFileAttributes_(tmpfile$,#FILE_ATTRIBUTE_NORMAL) ; In case user or another app modified it.
  DeleteFile_(tmpfile$) ; Doesn't report if delete failed, but power users will know it can be.
  ;
EndIf
;
End
;
DataSection
StartTestFile:
IncludeBinary "Test.dll"
EndTestFile:
EndDataSection 

PB - Registered PureBasic Coder