http://qs1969.pair.com?node_id=161345

svad has asked for the wisdom of the Perl Monks concerning the following question:

Hello, friends!

I am stuck with a problem that I can not 'invoke' a PDF file from my perl program with Win32::Process(...).

system("path-to-file.pdf");
works well, but it is creates a console window (whereas I am writing GUI(Tk) utility) and console window is not desirable.

Following

use Win32::Process; sub ErrorReport{ print Win32::FormatMessage(Win32::GetLastError()); } my $procobj; Win32::Process::Create( $procobj, "d:\\Adobe\\Acrobat 5.0\\Acrobat\\Acrobat.exe", "x D:\\WORK\\Documentation\\cd-writing\\perl-technology\\root-cd-tk\ +\files\\PSAX1250_IG_R610_Iss1-mp.pdf", 0, NORMAL_PRIORITY_CLASS, "." ) || die ErrorReport();
works okay on my machine (for given PDF file and path to PDF viewer, but this solution is even worse because I can not guess which PDF viewer has (if at all) user on its machine.

If anyone have an idea, please help me!

Warmest wishes,
Vadim.

Replies are listed 'Best First'.
Re: How to 'invoke' a PDF file on Win32
by jeffenstein (Hermit) on Apr 23, 2002 at 15:58 UTC

    On Win2k, "cmd /c start <filename>" may do what you want. An cmd window does pop up, but immediately goes away.

Re: How to 'invoke' a PDF file on Win32
by jsprat (Curate) on Apr 23, 2002 at 16:32 UTC
    Win32::Process is a standard module with ActiveState.
    Try perldoc Win32::Process. Plenty of examples!
      I tried namely
      perldoc Win32::Process
      ! The main problem is I can not 'invoke' pdf files directly using it.

      OTOH I've almost solved this by determining of associated to '.pdf' command line and invoking that file. So, following works (with console window appearing for half of a second and then disappearing):

      use Win32::Process; sub ErrorReport{ print Win32::FormatMessage(Win32::GetLastError()); } my ($ftype) = `assoc .pdf`=~/=(.*)/; print $ftype,"\n"; my ($apath) = `ftype $ftype`=~/=(.*)/; print $apath,"\n"; my $procobj; $apath=~s/"%1"$//; $apath=~s/^"//; $apath=~s/"\s*$//; Win32::Process::Create( $procobj, "$apath", "x D:\\WORK\\Documentation\\cd-writing\\perl-technology\\root-cd-tk\ +\files\\PSAX1250_IG_R610_Iss1-mp.pdf", 0, NORMAL_PRIORITY_CLASS, "." ) || die ErrorReport();
      This works good. May be it is needed to search in registry instead of `assoc` and `ftype`. When I'll come with a final solution I'll probably report this in 'code' section.
        Here's a better solution, using the ShellExecute API. This works as-is:

        #!/usr/bin/perl -w use strict; #use diagnostics; use warnings; use Win32::API; my $ShellExecute = new Win32::API("shell32", "ShellExecute", [qw(N P P + P P N)], 'N'); my $hWnd; $ShellExecute->Call($hWnd, 'open', 'c:/rc.d.pdf', undef, undef, 1);

        Try perldoc Win32::API, then find a good API reference. A lot of VB oriented sites will have good info.