Ver Mensaje Individual
  #15  
Antiguo 21-08-2018
Avatar de escafandra
[escafandra] escafandra is offline
Miembro Premium
 
Registrado: nov 2007
Posts: 2.197
Reputación: 20
escafandra Tiene un aura espectacularescafandra Tiene un aura espectacular
Como me parece que newtron comentaba que le pedían varios visores a la vez, ha modifocado un poquito el código para que RunAndWaitShell devuelva el ThreadId que ejecuta cada vez que será enviado de vuelta mediante el mensaje de finalizacion del vosor concreto. De esta forma tenemos identificado el proceso que se cierra cuyo ThreadId corresponde al que obtuvimos al iniciarlo.


Pongo Un ejemplo con las modificaciones:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, StdCtrls;

const
  RS_FINISH = WM_USER + 1;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    procedure OnRunAndWaitShell(var Msg: TMessage); message RS_FINISH;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function RunAndWaitShell(Handle: THandle; Operation, FileName, Parameters, Directory: String; nShowCmd: Integer): DWORD;
function ThRunAndWaitShell(var Info: TShellExecuteInfoA): BOOL; stdcall;
begin
  ShellExecuteExA(@Info);
  WaitForSingleObject(Info.hProcess, INFINITE);
  SendMessage(Info.wnd, RS_FINISH, GetCurrentThreadId, 0);
end;
const
{$J+}
  Info: TShellExecuteInfoA = ();
begin
  with Info do
  begin
    cbSize:= SizeOf(Info);
    fMask:= SEE_MASK_NOCLOSEPROCESS;
    wnd:= Handle;
    lpVerb:= PAnsiChar(Operation);
    lpFile:= PAnsiChar(FileName);
    lpParameters:= PAnsiChar(Parameters + #0);
    lpDirectory:= PAnsiChar(Directory);
    nShow:= nShowCmd;
    hInstApp:= 0;
  end;
  CloseHandle(CreateThread(nil, 0, @ThRunAndWaitShell, @Info, 0, Result));
{$J-}
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption:= IntToStr(RunAndWaitShell(Handle, 'open', 'd:/Archivo1.txt', '', '', SW_SHOW));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Label2.Caption:= IntToStr(RunAndWaitShell(Handle, 'open', 'd:/Archivo2.pdf', '', '', SW_SHOW));
end;

procedure TForm1.OnRunAndWaitShell(var Msg: TMessage);
begin
  // Fin de ejecución
  ShowMessage('Fin ' + IntToStr(Msg.WParam));
end;

end.



Espero que con esto quede soluicionada la duda.


Saludos.

Última edición por escafandra fecha: 21-08-2018 a las 12:50:45.
Responder Con Cita