Access Violation ShellExecute in delphi7

2019-09-09 11:29发布

问题:

I am using ShellExecute the same way given below, to open a txt file in Delphi7, it gives me access violation in module BORdbk70.dll. Not sure what this issue is? I have added ShellApi in uses list.

//sAddr := 'www.google.com'; 
Above line does not gives any error but also not redirect to browser and 
  ShellExecute returns result as "5 = Windows 95 only: The operating system denied access to the specified file"

sAddr := 'c:\text\info.txt';
res := ShellExecute(Handle, nil, PChar(sAddr), nil, nil, SW_SHOW);
showmessage(inttostr(res));

回答1:

This example that I wrote for you, working good (without error). I tested with Delphi7 on Windows 8.1

You must know what is default application to open *.txt files in your Operation System. That application will try open your file. On my system for *.txt default application is Notepad++ and this example opened file info.txt in Notepad++

Full source (pas) code:

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellApi;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  sAddr : String;
  res : Integer;
begin
  sAddr := 'c:\text\info.txt';
  res := ShellExecute(Handle, 'open', PChar(sAddr), nil, nil, SW_SHOW);
  showmessage(inttostr(res));
end;
end.

This example working good with admin and normal user rights.



回答2:

var
file:string;
exe_start_map:string;
 begin
  exe_start_map:=(ExtractFileDir(Application.ExeName));
  file:=exe_start_map+'\samplefile.txt';
  ShellExecute(handle,'open',pchar(file),'','',SW_SHOWnormal);
 end;

you must add ShellApi in uses list