Tips

 

How to embed data into a bound executable program

 

You might ask “Why would I want to do this?”.  Surely it is much easier to enter data into the source code in the editor.  You might also mumble something about executable program code being irreversible.

 

There are two very specific reasons why you might want to embed some data in a program after it has been bound:

(a)   if you want to change the TrueBASIC toolbox icon for a custom icon of your own.

(b)   If you want to change the security status of your program.

 

In both cases the strategy is virtually the same, and depends on the fact that when a program is bound into an executable file, any string constants are preserved as ASCii characters.  In simple terms, if you try to read a bound program file what you see is a massive jumble of meaningless symbols with an occasional recognizable word here and there.  These recognizable words are string constants.

 

If you know the image string of the standard TrueBASIC icon image, then you can scan the executable program file to determine its location.  Once you know where it is, you can over-write it with another icon image string of exactly the same size.   You cannot do this in the source code because the icon image is only incorporated during the binding process. The ICONEX program uses this principle to exchange the TrueBASIC icon with any user selected icon. 

 

The second reason why you might want to embed data in an executable file is as a means of controlling some sort of security aspect.  Let us suppose you have produced a marketable program which you post on your website for potential customers to try out.  Despite all the legislation, the world is full of thieves and vagabonds who will download your program and try to use it for nothing if they can get away with it.  Knowing this, you cleverly cripple some of the most important features of your program so that the user cannot file anything or cannot print anything, for example.  You do this with an IF....THEN clause that refers to an internal string constant known only to you.  For example:

 

LET unlock$=”XYZ123”

IF unlock$=”XYZ123” then

LET printflag=0 ! prevents printing

LET fileflag=0    ! prevents saving to file

ELSE

LET printflag=1 ! allows printing

LET fileflag=1    ! allows saving to file

END IF

 

The only way these features can be “unlocked” is to pay you and in return you supply a tiny self-destruct program that looks for the first instance of the string “XYZ123” and changes it to something else.  Clearly this is a very simple example of what can be an extremely robust security system.   

How to use AUTORUN

 

In their infinite wisdom, the designers of the Windows operating system included a special feature that scans any CD that you put into your CD drive to see if it contains a batch file called AUTORUN.INF

 

If Windows finds this file then it automatically runs whatever program is defined in this very simple text file. For example,  if AUTORUN.INF contains:

 

[AUTORUN]

open=myfile.exe

 

then Windows will promptly start up and run the program called myfile.exe.

 

Let us suppose you wish to sell, circulate or distribute several of your programs on a CD.  You cannot always be certain that your potential customers or users are as technically competent as you are.  Indeed they may be true dinosaurs, so anything you can do to make things easier for them is going to be good news.

 

For example, you might use AUTORUN.INF to start off a program that lists what is stored on the CD.  You could even let the user select items from that list to download onto their hard drive.  You might even give them a Power Point presentation of what each of your programs does.  All this can be achieved with CHAIN and RETURN.

 

Before we go any further it is important to remember that your potential customer or user may not have certain DLL files in their C:WINDOWSSYSTEM directory, in which case they will not be able to run your programs, nor will they be able to run the program defined in AUTORUN.INF, so make sure the following DLL files are in the root directory on the CD, and the same files should be downloaded alongside any program that the user elects to download.  

XNMBA420.DLL

XNMTE420.DLL

TB510.DLL

The easiest way to do this is to ZIP up your program with these DLL files, or to include the DLL files in a self extracting SETUP wizard.

 

If you have the latest version of TrueBASIC 5.5b19 then there is no requirement for these DLL files.

 

Here is a very basic start up program for a CD that demonstrates some of the features you may wish to use.

 

REM: CD_AUTO.TRU program

REM: Author: J.R.Arscott

REM: Date 20 OCT 2003

 

LIBRARY "C:TBsilverTBlibstruectrl.trc"

LIBRARY "C:TBsilverTBlibstruedial.trc"

 

DIM prog$(1)

DIM list$(1)

DIM ppfile$(1)

 

 

REM: read text file

REM: 3 lines of text per program

REM: line 1=brief description of program

REM: line 2=name of setup wizard file e.g. setup_myfile.exe

REM: line 3=name of Power Point file if any exists

 

CALL readtitles("auto.txt",list$(),prog$(),ppfile$())

 

CALL TC_init

CALL TC_setunitstopixels

CALL TC_getscreensize(lscr,rscr,bscr,tscr)

LET midxscr=int((lscr+rscr)/2)    ! mid x axis

LET midyscr=int((tscr+bscr)/2)    ! mid y axis

 

CALL TC_setRect(0,midxscr-300,midxscr+300,midyscr+220,midyscr-180)

CALL TC_Win_SetTitle (0,"Download File Selection")

CALL TC_show(0)

 

REM: set graphics window

ASK PIXELS xpix,ypix

SET WINDOW 0,xpix-1,ypix-1,0

CLEAR

 

REM: read image files

CALL Read_image("MS BMP",logo$,"logo.bmp")

 

REM: show your logo at lower left point x,y

REM: make sure there is room for the logo and the program list

LET x=90                          ! insert your own value

LET y=320                         ! insert your own value

BOX SHOW logo$ AT x,y

 

CALL TC_win_switch(0)

 

WHEN exception in

     DO

 

        REM: show list of program titles

        LET info$="Select a title then press Download or Slide show."

        CALL TD_list(info$,"Download|Slide show|Quit",list$(),choice,1,result)

 

        IF result=1 then

           LET message$="You have selected:|" & list$(choice) & "|Confirm you want to         install this program."

        CALL TD_message("DOWNLOAD",message$,"Confirm|Cancel",1,confirmation)

           IF confirmation=1 then      ! run setup wizard

              LET nextprogram$="!" & prog$(choice)

              CHAIN nextprogram$,RETURN

           ELSE

              LET message$=""

           END IF

        ELSEIF result=2 then

           REM: presentation has been selected

           IF ppfile$(choice)>"" then

        LET nextprogram$="!rundll32.exe url.dll,FileProtocolHandler " & ppfile$(choice)

              CHAIN nextprogram$,RETURN

           ELSE

              LET message$=" There is no presentation available for this program."

              CALL TD_message("INFORMATION",message$,"OK",1,ok)

           END IF

        ELSE

           EXIT DO

        END IF

 

     LOOP

 

USE

     LET fatal$="A malfunction was detected.|" & extext$

     CALL TD_warn(fatal$,"QUIT",1,result)

END WHEN

 

CALL TC_cleanup

 

END

 

SUB readtitles(username$,list$(),prog$(),ppfile$())

 

    CLOSE #11

    OPEN #11: name username$, create old, org text, access input

    LET total=0

    DO while more #11

       LET total=total+1

       MAT redim list$(total)

       LINE INPUT #11: list$(total)

       MAT REDIM prog$(total)

       LINE INPUT #11: prog$(total)

       MAT redim ppfile$(total)

       LINE INPUT #11: ppfile$(total)

    LOOP

    CLOSE #11

 

END SUB

 

Note that an external text file is used to provide the names of programs and files that reside on the CD.  A text file is a very simple and flexible way to store a list of names that might change in the future.  It is much easier to change the contents of a text file than it is to change the source code and rebind the program.

 

Notice how CHAIN is used to invoke RUNDLL32.EXE which in turn will start up the appropriate application to handle the designated file.

 

LET nextprogram$="!rundll32.exe url.dll,FileProtocolHandler " & ppfile$(choice)

 

If the filename extension had been .XLS then CHAIN would have started up Excel.  Similarly if the extension had been .DOC then WORD would have been started up.