by J.G.Harston, February 2008
You can open a file in an application, equivalent to double-clicking on it in a filer window, by using ShellExecute, as follows:
SYS "ShellExecute", @hwnd%, 0, file$, 0, 0, 1
This loads the file into the application that is associated with the file by reference to its file extension. For instance, a file with a “.doc” extension will be loaded into whatever application is associated with the “.doc” extension.
There may be times when you want to load a file into a specific application regardless of extension. For instance, you may want to load a batch file into a text editor. The simplest solution is to do:
SYS "ShellExecute", @hwnd%, 0, "notepad", file$, 0, 1
OSCLI "notepad "+file$
There are several problems with this.
Firstly, a minor one, you are depending on an application called 'notepad' existing on the computer the program is run on.
Secondly, and much more importantly, you must never force a user to use your choice of application, you must use the user's choice. Many people regard notepad as a very bad text editor and use something else.
The user's configured applications are named in the registry and referenced by the file extension they respond to. You can load a file to the user's text editor using the following code:
INPUT "File: "file$ root$=FNRegistry_RdStr(&80000000,".txt",""):REM HKEY_CLASSES_ROOT IF root$="" THEN ERROR 100, "No file association" command$=FNRegistry_RdStr(&80000000, root$+"\shell\open\command","") IF command$="" THEN ERROR 100, "No open command" A%=INSTR(command$,"%1"):IF A% THEN command$=LEFT$(command$,A%-1)+MID$(command$,A%+2) A%=INSTR(command$,""""""):IF A% THEN command$=LEFT$(command$,A%-1)+MID$(command$,A%+2) command$=FNExpandEnvironmentStrings(command$) SYS "ShellExecute", @hwnd%, 0, command$, file$, 0, 1 END
Here we read the default application which opens text files (.txt) from the registry and use “ShellExecute” to open the requested file in the chosen application. Effectively, this bypasses the automatic file association and executes the file as though it had a “.txt” extension.
The “.txt” extension in the code above can be changed to any other extension. For instance to open a HTML document or URL in the user's choice of web browser, and not force them to use Internet Explorer you would change the relevant line above to:
The above code uses the following code from the Registry library and a routine called FNExpandEnvironmentStrings:
DEF FNRegistry_RdInt(hk%,Key$,Item$):LOCAL Want%:Want%=4 DEF FNRegistry_RdBig(hk%,Key$,Item$):LOCAL Want%:Want%=4 DEF FNRegistry_RdStr(hk%,Key$,Item$):LOCAL Want%:Want%=1 DEF FNRegistry_Rd( hk%,Key$,Item$):LOCAL Want%:Want%=1 LOCAL K%,R%,L%,T%,S$,Buf% SYS "RegOpenKeyEx",hk%,Key$,0,&20001,^K% TO R% IF R%=0 THEN SYS "RegQueryValueEx",K%,Item$,0,^T%,0,^L%:DIM Buf% LOCAL L% SYS "RegQueryValueEx",K%,Item$,0,^T%,Buf%,^L% TO R% SYS "RegCloseKey",K% ENDIF IF Want%=4 :IF R%=0:=0 IF Want%=4 :=!Buf% IF R%:="" IF T%=1:Buf%?(L%-1)=0:=$$Buf% K%=Buf%:WHILE L%:S$+=CHR$?K%:K%+=1:L%-=1:ENDWHILE =S$ DEF FNExpandEnvironmentStrings(S$) LOCAL B%:DIM B% LOCAL 255 SYS "ExpandEnvironmentStrings", S$, B%, 256 =$$B%