delphi7中的访问冲突ShellExecute

Access Violation ShellExecute in delphi7

我按照下面给出的相同方式使用 ShellExecute 在 Delphi7 中打开一个 txt 文件,它在模块 BORdbk70.dll 中出现访问冲突。 不确定这是什么问题?我在使用列表中添加了 ShellApi

//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));

我为您编写的这个示例运行良好(没有错误)。我在 Windows 8.1

上使用 Delphi7 进行了测试

您必须知道在操作系统中打开 *.txt 文件的默认应用程序是什么。该应用程序将尝试打开您的文件。在我的系统上,*.txt 默认应用程序是 Notepad++,此示例在 Notepad++

中打开文件 info.txt

完整源代码:

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.

此示例适用于管理员和普通用户权限。

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;

您必须在使用列表中添加 ShellApi