unit uTagLink;

interface

  function LinkTo( Link: string; DefDir: string; Arguments: string ): string;

implementation

uses
   SysUtils, ShellAPI, Dialogs, Windows, uUtility;

function LinkTo( Link: string; DefDir: string; Arguments: string ): string;
// Attempt to open/link to the passed link. Spawn whatever application is required
var
  Err:         integer;
begin
  // explicitly set the default directory if one is not given
  if DefDir = '' then
  begin
    DefDir := ExtractFilePath( Link );
  end;

  try
    Err := ShellExecute(   0,                    // owner's handle
                           PChar('Open'),        // operation to perform (open, print or explore)
                           PChar(Link),          // filename or folder
                           PChar(Arguments),     // parameters
                           PChar(DefDir),        // default directory
                           SW_SHOWNORMAL         // shown when opened?
    );
    if Err <= 32 then
    begin
        case Err of
             0:	                        result := 'ERROR.ShellExecute The operating system is out of memory or resources.';
             ERROR_FILE_NOT_FOUND:	result := 'ERROR.ShellExecute The specified file was not found.';
             ERROR_PATH_NOT_FOUND:	result := 'ERROR.ShellExecute The specified path was not found.';
             ERROR_BAD_FORMAT:	        result := 'ERROR.ShellExecute The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).';
             SE_ERR_ACCESSDENIED:	result := 'ERROR.ShellExecute The operating system denied access to the specified file.';
             SE_ERR_ASSOCINCOMPLETE:    result := 'ERROR.ShellExecute The filename association is incomplete or invalid.';
             SE_ERR_DDEBUSY:	        result := 'ERROR.ShellExecute The DDE transaction could not be completed because other DDE transactions were being processed.';
             SE_ERR_DDEFAIL:	        result := 'ERROR.ShellExecute The DDE transaction failed.';
             SE_ERR_DDETIMEOUT:	        result := 'ERROR.ShellExecute The DDE transaction could not be completed because the request timed out.';
             SE_ERR_DLLNOTFOUND:	result := 'ERROR.ShellExecute The specified dynamic-link library was not found.';
             SE_ERR_NOASSOC:	        result := 'ERROR.ShellExecute There is no application associated with the given filename extension.';
             SE_ERR_OOM:	        result := 'ERROR.ShellExecute There was not enough memory to complete the operation.';
             SE_ERR_SHARE:	        result := 'ERROR.ShellExecute A sharing violation occurred.';
             else                       result := 'ERROR.ShellExecute Error number '+IntToStr(Err)+' was reported.';
        end;
        MessageDlg( 'ERROR.ShellExecute Link failed!: '+^M^J+
                    '  '''+Link+' '+Arguments+''''+^M^J+
                    '  in path: '''+DefDir+^M^J+
                    ' WinAPI Error: '''+result+''''
                    , mtError
                    , [mbOK]
                    , 0
        );
    end else
    begin
      result := 'ShellExecute: '+Link+'  PID: '+IntToStrComma(Err);
    end;
  except
  end;
end;

end.
