【问题标题】:Access Violation ShellExecute in delphi7delphi7中的访问冲突ShellExecute
【发布时间】:2015-03-24 08:35:30
【问题描述】:

我正在使用下面给出的相同方式使用 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));

【问题讨论】:

  • 这是一个纯调试器问题。没什么好担心的。无论如何,您都不应该使用ShellExecute,因为它不能正确报告错误。使用ShellExecuteEx
  • FWIW,你不可能知道ShellExecute 确实返回了什么错误,因为你的代码忽略了返回值。
  • @DavidHeffernan - 谢谢大卫,但现在另一个问题出现了,它以前是如何运行的,然后突然开始表现得像这样。系统和/或代码也没有变化。我也访问过这个页面 - qc.embarcadero.com/wc/qcmain.aspx?d=11751
  • 你说的是一个 >10 岁的 IDE。我想你只需要忍受它。至少,看看是否使用ShellExecuteEx,无论如何这是一个更好的选择,以某种方式避免了这个问题。
  • @DavidHeffernan - 是的,没错 :)。因为当我以管理权限运行我的程序时,此功能已成功执行。问题仅与您提到的调试有关。再次感谢。

标签: shell delphi-7


【解决方案1】:

我为你写的这个例子,运行良好(没有错误)。我在 Windows 8.1 上使用 Delphi7 进行了测试

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

完整的源代码(pas):

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.

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

【讨论】:

    【解决方案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;
    

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

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多