[DUG] ShellExecute encoding
John Bird
johnkbird at paradise.net.nz
Wed Jul 29 14:54:14 NZST 2009
I solved this - in case its of interest here's how I did it.
The file URL is encoded by the browser if it does not already start with
file:/// and have only "/" characters rather than "\" in it. In order to
pass a file URL to ShellExecute with a Tag in it, one cannot therefore just
execute the html file which is the normal way, but instead execute the
default browser and pass the ready URL to it
Here is the code I used, note the routine can be used to get the program to
execute any extension. In my case it allows me to send a url like
C:\HelpDir\HelpFile.HTM#TAGNAME and the browser will get the correct URL and
position on the page at that tag.
//pass aTag as tagname to jump to in web page WITHOUT the # before it
//have to use this rather than doing ShellExecute on the help file directly
as the browser
//will re-encode the URL including turning the # into a %23 unless the
format is already correct
procedure xcOpenHTMLFile(aURL:string;aTag:string);
var
lparams:string;
i:integer;
sExe:string;
begin
lparams:=aURL+'#'+aTag; //#
for I := 1 to length(lparams) do
if lparams[i]='\' then lparams[i]:='/';
lparams:='file:///'+lparams; //puts argument into encoding form the
browser expects
sEXE:=xfGetExeByExtension('.htm') ;
if (sEXE <> '') and FileExists(sEXE) then
ShellExecute(Application.Mainform.Handle,'OPEN', PChar(sEXE),
PCHar(lparams), nil, SW_SHOWNORMAL) ;
end;
//from http://delphi.about.com/cs/adptips2003/a/bltip0103_3.htm
function xfGetExeByExtension(sExt : string) : string;
var
sExtDesc:string;
begin
with TRegistry.Create do
begin
try
RootKey:=HKEY_CLASSES_ROOT;
if OpenKeyReadOnly(sExt) then
begin
sExtDesc:=ReadString('') ;
CloseKey;
end;
if sExtDesc <>'' then
begin
if OpenKeyReadOnly(sExtDesc + '\Shell\Open\Command') then
begin
Result:= ReadString('') ;
end
end;
finally
Free;
end;
end;
if Result <> '' then
begin
if Result[1] = '"' then
begin
Result:=Copy(Result,2,-1 + Pos('"',Copy(Result,2,MaxINt))) ;
end
end;
end;
John
More information about the Delphi
mailing list