Article View: comp.os.os2.programmer.misc
Article #273Take a look at me now!!
From: S�bastien JEAN-LOUIS
Date: Wed, 22 Jul 1998 00:00
Date: Wed, 22 Jul 1998 00:00
306 lines
9236 bytes
9236 bytes
Good morning,
Please help me converting this source because WinAPI on printing are easy
but OS2 ones are tremendous!!!
Thanx
/***************************************************************************
*****************/
//ALL THE FUNCTION WITH FRENCH NAMES ARE MINE AND DON'T NEED TO BE CONVERTED
//ONLY WINDOWS FUNCTION NEED TO BE:
//AddJob,GetLastError,GetJob,SetJob,CloseHandle,ScheduleJob,GetProfileString
//OpenPrinter,CreateFile,WriteFile,ClosePrinter <- The most
important for me!
//AND THE VARIABLE:
//GENERIC_WRITE,FILE_SHARE_WRITE,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,INVALID_H
ANDLE_VALUE
#include "sysdpt.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <direct.h>
#define OUTIMPR
//MY INCLUDES
#include "globalnt.h"
#include "outil.h"
#include "outimpr.h"
#include "ressdpcl.h"
#define TAILLE_BUF 64000
/*------------------------------Imprime/Print------------------------------
Send a file to designed printer or a default one
------------------------------------------------------------------------*/
BOOL Imprime(char *Imprimante,char *Fichier,char *NomDocument,short
Copies,BOOL GardeFichier)
{
char *ImprimanteDefaut;
BOOL Res=TRUE;
#ifdef DEBUGAGI
agiprintf("D�but Imprime");
#endif
//Test de validit� de l'imprimante
if(!TesteImprimante(Imprimante))
//L'imprimante donn�e n'est pas valide : recherche de l'imprimante par
d�faut
{ ImprimanteDefaut=RecupImprimanteDefaut();
if(!ImprimanteDefaut)
{ LogWar(PTLOG,ID_PRINTER_DEF_NOK);
Res=FALSE;
}
//Copie du fichier dans le spool
else
{ Res=SpooleFichier(Fichier,ImprimanteDefaut,NomDocument,Copies);
ImprimanteDefaut=Libere(ImprimanteDefaut);
}
}
else
Res=SpooleFichier(Fichier,Imprimante,NomDocument,Copies);
if(!GardeFichier)
SupprimeFichier(Fichier);
#ifdef DEBUGAGI
agiprintf("Fin Imprime");
#endif
return(Res);
}
/*----------------------SpooleFichier/SpoolFile-----------------------------
-
Create a print job on a printer and warn the spooler
------------------------------------------------------------------------*/
BOOL SpooleFichier(char *Doc,char *Printer,char *NomJob,short Copies)
{
HANDLE hPrinter=(HANDLE)0;
HANDLE hPrint;
ADDJOB_INFO_1 *Ajout=(ADDJOB_INFO_1 *)0;
JOB_INFO_1 *pJobInfo=(JOB_INFO_1 *)0;
DWORD NbOctets=0;
DWORD Taille=0;
BOOL res=FALSE;
DWORD Error=0;
DWORD Temp=0;
int hFic;
long NbCar;
char *Buf;
#ifdef DEBUGAGI
agiprintf("D�but SpoolFichier");
#endif
//Ouverture de l'imprimante fournie
res=OpenPrinter(Printer,&hPrinter,0);
if(!res)
{ LogWar(PTLOG,ID_PRINTER_NOK,Printer,Doc);
res=FALSE;
}
else
//Premi�re cr�ation de job pour r�cup�rer le nombre d'octets n�cessaires �
la
//structure contenant les informations sur le job
{
res=AddJob(hPrinter,(DWORD)1,(unsigned char *)Ajout,0,&NbOctets);
if( (GetLastError()!=ERREUR_ADD_JOB) && (!res) && NbOctets)
{ Ajout=(ADDJOB_INFO_1 *)Alloue(NbOctets);
//Deuxi�me cr�ation : cr�ation du job sur l'impriamante
res=AddJob(hPrinter,(DWORD)1,(unsigned char *)Ajout,NbOctets,(DWORD
*)&Taille);
if(res)
{
//Le job a �t� cr�� sur un spool local : modification du libell� du
document
//Cette modification n'est pas possible sur un queue d�localis�e
if(Ajout->JobId!=0xFFFFFFFF)
{
// res=SetJob(hPrinter,Ajout->JobId,0,NULL,JOB_CONTROL_PAUSE);
res=GetJob(hPrinter,Ajout->JobId,1,(LPBYTE)pJobInfo,0,&NbOctets);
if(NbOctets)
{ pJobInfo=(JOB_INFO_1 *)Alloue(NbOctets);
res=GetJob(hPrinter,Ajout->JobId,1,(LPBYTE)pJobInfo,NbOctets,&Taille);
pJobInfo->pDocument=_Alloue(strlen(NomJob)+1);
strcpy(pJobInfo->pDocument,NomJob);
}
}
hPrint=CreateFile(Ajout->Path,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_ALWAY
S,FILE_ATTRIBUTE_NORMAL,NULL);
if(hPrint==INVALID_HANDLE_VALUE)
{ LogWar(PTLOG,ID_PRN_COPY_NOK,NomJob,Printer);
}
else
{
hFic=OuvreFicR((char *)Doc);
NbCar = PosFicFin(hFic);
if(NbCar>0)
{
Buf=Alloue(TAILLE_BUF);
for(;Copies;Copies--)
{
FicPosit(hFic,0L);
do
{
res=LitFichier(hFic,Buf,TAILLE_BUF);
res=WriteFile(hPrint,Buf,res,&Taille,NULL);
}while(res && Taille==TAILLE_BUF);
}
FermeFichier(hFic);
Buf=Libere(Buf);
if(Ajout->JobId!=0xFFFFFFFF)
{ res=SetJob(hPrinter,Ajout->JobId,1,(LPBYTE)pJobInfo,0);
if(pJobInfo->pDocument)
pJobInfo->pDocument=_Libere(pJobInfo->pDocument);
pJobInfo=(JOB_INFO_1 *)Libere((char *)pJobInfo);
}
res=CloseHandle(hPrint);
if(Ajout->JobId==0xFFFFFFFF)
{ res=ScheduleJob(hPrinter,Ajout->JobId);
if(!res)
{ LogWar(PTLOG,ID_PRN_SCHED_NOK,NomJob,Printer);
}
}
}
}
}
else
{ LogWar(PTLOG,ID_PRN_AJOUT_NOK,NomJob,Printer);
}
}
else
{ LogWar(PTLOG,ID_PRN_AJOUT_NOK,NomJob,Printer);
}
//JRR 23/10/95
if(Ajout)
Ajout=(ADDJOB_INFO_1 *)Libere((char *)Ajout);
ClosePrinter(hPrinter);
}
if(res)
LogInf(PTLOG,ID_PRINTER_DEF,NomJob,Printer);
#ifdef DEBUGAGI
agiprintf("Fin SpoolFichier");
#endif
return(res);
}
/*---------------------------RecupImprimanteDefaut-----------------------
Get the default printer
------------------------------------------------------------------------*/
char *RecupImprimanteDefaut()
{ char *Buffer=(char*)0;
char *ImprimanteDefaut=(char *)0;
DWORD Res,i;
ImprimanteDefaut=Alloue(LG_CHEMIN+1);
Res=GetProfileString("windows","device",NULL,ImprimanteDefaut,LG_CHEMIN);
if(!Res)
ImprimanteDefaut=Libere(ImprimanteDefaut);
else
{ for(i=0;i<Res;i++)
if(ImprimanteDefaut[i]==',')
{ ImprimanteDefaut[i]=(char)0;
break;
}
}
return(ImprimanteDefaut);
}
/*------------------------------TesteImprimante--------------------------
Test if printer exist
------------------------------------------------------------------------*/
BOOL TesteImprimante(char *printer)
{ char **Imprimantes=(char **)0;
BOOL Trouve=FALSE;
HANDLE hPrinter;
short i;
ListeImprimantes(&Imprimantes);
for(i=0;Imprimantes && Imprimantes[i];i++)
{
if(Imprimantes[i]==GTROU) continue;
if(!strcmp(Imprimantes[i],printer))
{
if(OpenPrinter(printer,&hPrinter,0))
{
ClosePrinter(hPrinter);
Trouve=TRUE;
}
break;
}
}
if(Imprimantes)
Imprimantes=(char**)LibereTable((void **)Imprimantes);
return(Trouve);
}
/*-----------------------------ListeImprimantes--------------------------
Make a list of all the printers on local or network PC
------------------------------------------------------------------------*/
BOOL ListeImprimantes(char ***printers)
{
LPPRINTER_INFO_2 TabPrinters;
DWORD NbOctets,NbPrinters,i;
DWORD Flags = PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL;
// appel pour conna�tre la taille du tableau
EnumPrinters(Flags ,NULL,2,NULL,0,&NbOctets,&NbPrinters);
TabPrinters=(LPPRINTER_INFO_2)Alloue(NbOctets+4);
if(EnumPrinters(Flags
,NULL,2,(LPBYTE)TabPrinters,NbOctets,&NbOctets,&NbPrinters))
{
for(i=0;i<NbPrinters;i++)
*printers=AjouteTabChar((char **)*printers,(char *)(TabPrinters +
i)->pPrinterName);
}
Libere((char*)TabPrinters);
return(TRUE);
}
/***************************************************************************
*********************************/
--
sebjl@bigfoot.com
Technical Details
Message-ID:
<6p53bl$cta$1@front5.grolier.fr>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 1049cc,9b589b59d712f77a,start X-Google-Attributes: gid1049cc,public X-Google-Thread: 10595e,9b589b59d712f77a,start X-Google-Attributes: gid10595e,public X-Google-Thread: 11307a,9b589b59d712f77a,start X-Google-Attributes: gid11307a,public X-Google-Thread: 10fb18,9b589b59d712f77a,start X-Google-Attributes: gid10fb18,public X-Google-Thread: f5cc2,9b589b59d712f77a,start X-Google-Attributes: gidf5cc2,public From: "S�bastien JEAN-LOUIS" <sebjl@bigfoot.com> Subject: Take a look at me now!! Date: 1998/07/22 Message-ID: <6p53bl$cta$1@front5.grolier.fr> X-Deja-AN: 373877868 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 X-Trace: front5.grolier.fr 901124277 13226 195.36.153.200 (22 Jul 1998 16:17:57 GMT) Organization: Grolier Interactive Europe NNTP-Posting-Date: 22 Jul 1998 16:17:57 GMT Newsgroups: comp.os.os2.programmer.misc,comp.os.os2.programmer.porting,comp.os.os2.programmer.tools,comp.os.os2.utilities,fr.comp.os.os2