Fabrizio
Fabrizio

Reputation: 8043

How to edit a file using a specified editor?

Currently, I'm using the following function in order to open a file using the default editor and be sure my application waits until the user closes the editor window.

function EditAndWait(const AFileName : string) : boolean;
var
  Info: TShellExecuteInfo;
begin
  FillChar(Info, SizeOf(Info), 0);
  Info.cbSize := SizeOf(Info);
  Info.lpVerb := 'edit';
  Info.lpFile := PAnsiChar(AFileName);
  Info.nShow := SW_SHOW;
  Info.fMask := SEE_MASK_NOCLOSEPROCESS;
  Result := ShellExecuteEx(@Info);
  if(Result) and (Info.hProcess <> 0) then 
  begin
    WaitForSingleObject(Info.hProcess, Infinite);
    CloseHandle(Info.hProcess);
  end;
end;

I would like write a similar function which allow to specify the editor executable to use for editing.

function EditAndWait(const AFileName : string; const AEditor : string) : boolean;
begin
  //...
end;

Upvotes: 1

Views: 145

Answers (1)

Hwau
Hwau

Reputation: 880

As David said, it can be done by running the editor program and passing the file as a parameter.

There are several ways to do it. This is the most similar to the current function:

function EditAndWait(const AFileName : string; const AEditor : string) : boolean;
var
  Info: TShellExecuteInfo;
begin
  FillChar(Info, SizeOf(Info), 0);
  Info.cbSize := SizeOf(Info);
  Info.lpVerb := 'open';
  Info.lpFile := PChar(AEditor);
  Info.nShow := SW_SHOW;
  Info.fMask := SEE_MASK_NOCLOSEPROCESS;
  Info.lpParameters := PChar(AFileName);
  Result := ShellExecuteEx(@Info);
  if(Result) and (Info.hProcess <> 0) then 
  begin
    CloseHandle(Info.hProcess);
    WaitForSingleObject(Info.hProcess, Infinite);
  end;
end;

Upvotes: 1

Related Questions