1

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));
Trupti
  • 63
  • 11
  • 1
    It's a pure debugger issue. Nothing much to worry about. You shouldn't use `ShellExecute` anyway because it doesn't report errors properly. Use `ShellExecuteEx`. – David Heffernan Mar 24 '15 at 09:03
  • And FWIW, you cannot possibly know what error `ShellExecute` does return since your code ignores the return value. – David Heffernan Mar 24 '15 at 09:33
  • @DavidHeffernan - Thanks David, but now the other question arises that how it was running previously and suddenly started behaving like this. There is no change in system and/or in code as well. I have also visited this page - http://qc.embarcadero.com/wc/qcmain.aspx?d=11751 – Trupti Mar 24 '15 at 10:12
  • You are talking about a >10 year old IDE. I think you'll just have to put up with it. At the very least, see if using `ShellExecuteEx`, which is a much better option anyway, somehow avoids the problem. – David Heffernan Mar 24 '15 at 10:35
  • @DavidHeffernan - Yeah true :). because when I run my program with administrative rights this function got executed successfully. Issue is coming with debugging only which you have also mentioned. Thanks again. – Trupti Mar 24 '15 at 10:48
  • possible answer: https://stackoverflow.com/questions/50861106/application-verifier-reports-access-violation-in-call-to-shellexecuteex – Gabriel Jul 09 '18 at 07:03

2 Answers2

0

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.

mikia
  • 454
  • 1
  • 4
  • 14
0
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