L'auteur
Mike Gagnon Canada Membre Simple # 0000000025 enregistré le 14/10/2004 Gagnon Mike Pointe Cla H9R 3K8 de la société Carver Technologies Inc. Fiche personnelle
Note des membres
pas de note
Contributions > 03 - FRX : Etat, impression, Report
Utiliser PDFCreator silencieusement (sans nommer le fichier PDF)
# 0000000729
ajouté le 09/12/2009 17:24:14 et modifié le 09/12/2009
consulté 12455 fois
Niveau
initié Version(s) Foxpro : VFP 9.0 VFP 8.0 VFP 7.0
Description
Voici un bout de code pour utiliser PDFCreator sans avoir besoin de nommer le fichier PDF. Le code aussi detruit PDFCreator à chaque utilisation sinon cela peut causer un problème.
Code source :
Close Databases All
#Define TH32CS_SNAPPROCESS 2
#Define TH32CS_SNAPTHREAD 4
#Define TH32CS_SNAPMODULE 8
#Define MAX_PATH 260
#Define PE32_SIZE 296
Local cPrinterNow,cOldPrinter
cOldPrinter = Set ("Printer" ,2)
Declare Sleep In Win32API Integer nMilliseconds
Declare Integer CloseHandle In kernel32 Integer hObject
Declare Integer CreateToolhelp32Snapshot In kernel32;
INTEGER dwFlags, Integer th32ProcessID
Declare Integer Process32First In kernel32;
INTEGER hSnapshot, String @ lppe
Declare Integer Process32Next In kernel32;
INTEGER hSnapshot, String @ lppe
checkExe("PDFCreator.exe" )
Cd Home (2)+"\tastrade\data\"
PrinterNow = "pdfcreator"
If Upper (PrinterNow) <> Upper (cOldPrinter)
resetprinter(PrinterNow)
Endif
Select * From customer Into Cursor tmpCust
Create Report customer From tmpCust Fields company_name
oPDFC = Createobject ("PDFCreator.clsPDFCreator" ,"pdfcreator" )
oPDFC.cStart("/NoProcessingAtStartup" )
oPDFC.cOption("UseAutosave" ) = 1
oPDFC.cOption("UseAutosaveDirectory" ) = 1
oPDFC.cOption("PDFUserPass" ) =1
If !Directory ("c:\temp4\" )
Md "c:\temp4"
Endif
oPDFC.cOption("AutosaveDirectory" )="c:\temp4\"
oPDFC.cOption("AutosaveFormat" ) = 0
DefaultPrinter = oPDFC.cDefaultprinter
oPDFC.cDefaultprinter = "pdfcreator"
oPDFC.cClearCache
ReadyState = 0
oPDFC.cprinterstop=.F.
For i = 1 To 4
oPDFC.cOption("AutosaveFilename" ) = "toto" +Alltrim (Str (i))
Report Form customer To Printer Noconsole
nSleepSeconds = 5
nMilliseconds = nSleepSeconds * 1000
=Sleep(nMilliseconds)
Endfor
oPDFC.cDefaultprinter = cOldPrinter
oPDFC.cClearCache
Release oPDFC
resetprinter(cOldPrinter)
Procedure saveProcessData (lcBuffer)
Local m.execname
m.execname = Substr (lcBuffer, 37)
m.execname = Substr (m.execname, 1, At (Chr (0),m.execname)-1)
m.processid = buf2dword (Substr (lcBuffer, 9,4))
Insert Into csResult From Memvar
Return
Procedure resetprinter(tcPrinter)
Local oWsh
oWsh = Createobject ('WScript.Network' )
oWsh.SetDefaultPrinter(tcPrinter)
Endproc
Function buf2dword (lcBuffer)
Return Asc (Substr (lcBuffer, 1,1)) + ;
Asc (Substr (lcBuffer, 2,1)) * 256 +;
Asc (Substr (lcBuffer, 3,1)) * 65536 +;
Asc (Substr (lcBuffer, 4,1)) * 16777216
Function num2dword (lnValue)
#Define m0 256
#Define m1 65536
#Define m2 16777216
Local b0, b1, b2, b3
b3 = Int (lnValue/m2)
b2 = Int ((lnValue - b3*m2)/m1)
b1 = Int ((lnValue - b3*m2 - b2*m1)/m0)
b0 = Mod (lnValue, m0)
Return Chr (b0)+Chr (b1)+Chr (b2)+Chr (b3)
Function checkExe(tcExe)
Create Cursor csResult (;
execname C(250),;
processid N(16))
Local hSnapshot, lcBuffer
hSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0)
lcBuffer = num2dword(PE32_SIZE) + Repli(Chr (0), PE32_SIZE-4)
If Process32First (hSnapshot, @lcBuffer) = 1
= saveProcessData (lcBuffer)
Do While .T.
If Process32Next (hSnapshot, @lcBuffer) = 1
= saveProcessData (lcBuffer)
Else
Exit
Endif
Enddo
Else
Endif
= CloseHandle (hSnapshot)
Select csResult
Locate For Upper (execname) = Upper (tcExe)
If Found ()
Killprocess(csResult.ProcessID)
Endif
Procedure Killprocess
Lparameters tnProcessID
Local lnHandle
Declare Integer OpenProcess In Win32API ;
integer dwDesiredAccess, Integer bInheritHandle, Integer dwProcessID
Declare Integer TerminateProcess In Win32API ;
integer hProcess, Integer uExitCode
lnHandle = OpenProcess(1, 1, tnProcessID)
llReturn = TerminateProcess(lnHandle, 0) > 0
Endproc
Commentaires
Aucun commentaire enregistré ...