Print Page | Close Window

ShellExec and a bit more (delphi)

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: Sample Code
Forum Description: Share Debenu Quick PDF Library sample code with other forum members
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=139
Printed Date: 26 Nov 24 at 5:17AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: ShellExec and a bit more (delphi)
Posted By: Ingo
Subject: ShellExec and a bit more (delphi)
Date Posted: 01 Nov 05 at 12:13AM
Often i've read questions about how to show/open a pdf-file with QuickPDF. Why you want to do this with QuickPDF? Everybody who's working with pdf-files have installed a pdf-reader... so why not use it? For this you can use ShellExecute or (better) ShellExec ('cause it supports long file names and offers you more features).

If you don't want to work with the real name "acrord32" you can use my second example as well. All in Delphi but i think other languages offers the same.

Some more special code is "Keybd_Event...". You know pressing the shift-key while opening a pdf-file supress the loading of many dlls (from the acrobat reader) which you often don't need. So in my code first i simulate a pressed shift key and after ShellExec i simulate a released shift key.

Best regards,
Ingo

. . .
   uses ShellAPI
. . .
function ShellExec( op, fn, par, dir: PChar;
show: Word; wait: Boolean): LongInt;
var
ih: Word;
OK: Boolean;
Info: TShellExecuteInfo;
begin
FillChar(Info, SizeOf(Info), Chr(0));
Info.cbSize := SizeOf(Info);
Info.fMask := SEE_MASK_NOCLOSEPROCESS;
Info.lpVerb := op;
Info.lpFile := fn;
Info.lpParameters := par;
Info.lpDirectory := dir;
Info.nShow := show;
OK := Boolean(ShellExecuteEx(@Info));
if OK then
begin
if wait then
begin
while
WaitForSingleObject(Info.hProcess,100)
= WAIT_TIMEOUT do
Application.ProcessMessages;
OK := GetExitCodeProcess(
Info.hProcess, DWord(Result));
end
else
Result := 0;
end;
if not OK then Result := -1;
end;
. . .
// Anywhere in your program ... perhaps at an onclick-event:
. . .
   Keybd_Event(vk_Shift,0,0,0);
   ShellExec('open',PChar('acrord32.exe'),PChar('/s ' + ExtractFilePath(Application.ExeName) + 'example.pdf'),nil,SW_SHOWNORMAL,True);
   Keybd_Event(vk_Shift,0,KEYEVENTF_KEYUP,0);

// and a second solution...

. . .
ShellExec('open',PChar(GetProgramAssociation('pdf')),PChar('/s ' + ExtractFilePath(Application.ExeName) + 'example.pdf'),nil,SW_SHOWNORMAL,True);
. . .


-------------
Cheers,
Ingo




Replies:
Posted By: DafCorpX
Date Posted: 06 Dec 05 at 10:49AM

Nice code just wanted to show abnother way to open or print the pdf file.

uses ShellAPI

ShellExecute( Form1.Handle, 'OPEN', PChar(TheFullPDFFilenameWithPath), nil, nil, sw_shownormal);


ShellExecute( Form1.Handle, 'PRINT', PChar(TheFullPDFFilenameWithPath), nil, nil, sw_shownormal);

This will either OPEN the file with it's associated progranm or PRINT it with the same program

Saves the tedious way to find AcroRead or whatever program that's associated with PDF files.

Goodluck



-------------
//Dan



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk