Ver Mensaje Individual
  #10  
Antiguo 21-05-2014
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
radenf,

Cita:
Empezado por radenf
...La última actualización de Adobe Reader modifica la AcroPdf.dll a la versión 11.0.7.79, con lo cual el componente deja de funcionar al igual que los programas que lo utilizan...
Cita:
Empezado por dec
...No mostrar, pero, "lanzar" los documentos PDF de manera que el usuario los visualice usando el programa que prefiera y tenga instalado: Acrobat Reader u otro...
Cita:
Empezado por radenf
...Esto me ha abierto los ojos a la vulnerabilidad de los programas que desarrollamos cuando se utilizan componentes de terceros...
Revisa este código:
Código Delphi [-]
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure ExecuteApp(AppName, CmdLine, WindowName : String);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  HWindow : THandle;

implementation

{$R *.dfm}

// Verifica si un Proceso esta Activo
function ProcessExists(ExeFileName: String): Boolean;
var
   ContinueLoop: BOOL;
   FSnapshotHandle: THandle;
   FProcessEntry32: TProcessEntry32;

begin

   FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
   FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
   ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);

   while Integer(ContinueLoop) <> 0 do
   begin
      if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName))
      or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then
      begin
         Result := True;
         Exit;
      end;
      ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
   end;

   CloseHandle(FSnapshotHandle);

   Result := False;

end;

// Elimina un Proceso Activo
function KillTask(ExeFileName: String): Integer;
const
   PROCESS_TERMINATE = $0001;
var
   ContinueLoop: BOOL;
   FSnapshotHandle: THandle;
   FProcessEntry32: TProcessEntry32;

begin

   Result := 0;

   FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
   FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
   ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);

   while Integer(ContinueLoop) <> 0 do
   begin
      if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName))
      or  (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then
         Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,
                                                        BOOL(0),
                                                        FProcessEntry32.th32ProcessID),
                                            0));
      ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
   end;

   CloseHandle(FSnapshotHandle);

end;

// Visualiza un Archivo PDF en un TPanel (Ejecución)
procedure TForm1.ExecuteApp(AppName, CmdLine, WindowName : String);
var
   HndApp : THandle;
   i : Integer;
   Command : String;
   Parameters : String;

begin

   // Cierra cualquier instancia previa de Acrobat Reader que este activa
   if ProcessExists('AcroRd32.exe') then
      KillTask('AcroRd32.exe');

   // Abre una nueva instancia de Acrobat Reader con el Documento Seleccionado
   HndApp := ShellExecute(0, nil, PChar(AppName), PChar(CmdLine), nil, SW_SHOWMINIMIZED);

   // Busca la ventana de Acrobat Reader con el Documento Seleccionado y la asigna al TPanel
   if (HndApp > 32) then
   begin

      for i := 1 to 10 do
      begin
         HWindow := FindWindow(nil,PChar(WindowName));
         if HWindow > 0 then
            Break
         else
            Sleep(100);
      end;

      if HWindow > 0 then
      begin
         Windows.SetParent(HWindow, Panel1.Handle);
         Windows.MoveWindow(HWindow, 0, 0, ClientWidth, ClientHeight, True);
         Windows.ShowWindow(HWindow,SW_MAXIMIZE);
         Windows.SetFocus(HWindow);
      end;

   end;

end;

// Visualiza un Archivo PDF en un TPanel (LLamada)
procedure TForm1.Button1Click(Sender: TObject);
var
   AppName, CmdLine, WindowName : String;
   openDialog : TOpenDialog;
   Msg : String;

begin

   if (HWindow = 0) then
   begin

      openDialog := TOpenDialog.Create(self);
      openDialog.InitialDir := GetCurrentDir;
      openDialog.Options := [ofFileMustExist];
      openDialog.Filter := 'PDF files|*.pdf';
      openDialog.FilterIndex := 1;

      if openDialog.Execute then
      begin
         AppName := 'C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe';
         CmdLine := openDialog.FileName;
         WindowName := ExtractFileName(openDialog.FileName) + ' - Adobe Reader';
         ExecuteApp(AppName, CmdLine, WindowName);
      end
      else
      begin
         Msg := 'No Se Selecciono Ningún Archivo PDF a Visualizar';
         MessageDlg(Msg, mtInformation, [mbOK],0);
      end;

   end;

end;

// Cierra la ventana de un Archivo PDF en un TPanel
procedure TForm1.Button2Click(Sender: TObject);
begin
   if HWindow > 0 then
   begin
      SendMessage(HWindow, WM_CLOSE, 0, 0);
      HWindow := 0;
   end;
end;

// Maximiza la ventana de un Archivo PDF en un TPanel
procedure TForm1.Button3Click(Sender: TObject);
begin
   if HWindow > 0 then
      ShowWindow(HWindow, SW_MAXIMIZE);
end;

// Minimiza la ventana de un Archivo PDF en un TPanel
procedure TForm1.Button4Click(Sender: TObject);
begin
   if HWindow > 0 then
      ShowWindow(HWindow, SW_MINIMIZE);
end;

// Inicializa el Handle de la Ventanna del TPanel
procedure TForm1.FormCreate(Sender: TObject);
begin
   HWindow := 0;
end;

// Cierra la ventana de un Archivo PDF en un TPanel al finalizar la Aplicación
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   if HWindow > 0 then
      SendMessage(HWindow, WM_CLOSE, 0, 0);
   Action := caFree;
end;

end.
El código anterior en Delphi 7 bajo Windows 7 Professional x32, permite abrir un documento PDF en un TPanel por medio de la función ShellExecute, como se muestra en la siguiente imagen:



Nota:

1- En esta versión es mandatorio finalizar cualquier instancia previa de Acrobat Reader para poder visualizar el documento seleccionado en el TPanel.

2- Esta versión no depende de ningún componente de terceros, solo de las APIs de Windows y de la versión instalada de Acrobat Reader.

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 21-05-2014 a las 08:51:39.
Responder Con Cita