//---------------------------------------------------------------------------
bool __fastcall TRawPrint::FechaDispositivo(void)
{
 bool Retorno = true;

 try
  {
   if (DispAberto)
    {
     if (PaginaAberta)
      {
       if(!EndPagePrinter(hPrinter))
        Erro("Erro ao Finalizar Página");

       if(!EndDocPrinter(hPrinter))
        Erro("Erro ao Finalizar Documento");
       else
        {
         if(!ClosePrinter(hPrinter))
          {
           Erro("Erro ao Fechar a Impressora");
           Retorno = false;
          }
          DispAberto = false;
        }
      }
    }
  }
 catch(Exception & E)
  {
   Erro("Erro ao Fechar a Impressora.\r\n\r\n" + E.Message + "\r\n");
  }
 return Retorno;
};
Exemple #2
0
printer_job *printer_start_job(char *printer)
{
    printer_job *ret = smalloc(sizeof(printer_job));
    DOC_INFO_1 docinfo;
    int jobstarted = 0, pagestarted = 0;

    ret->hprinter = NULL;
    if (!OpenPrinter(printer, &ret->hprinter, NULL))
	goto error;

    docinfo.pDocName = "PuTTY remote printer output";
    docinfo.pOutputFile = NULL;
    docinfo.pDatatype = "RAW";

    if (!StartDocPrinter(ret->hprinter, 1, (LPSTR)&docinfo))
	goto error;
    jobstarted = 1;

    if (!StartPagePrinter(ret->hprinter))
	goto error;
    pagestarted = 1;

    return ret;

    error:
    if (pagestarted)
	EndPagePrinter(ret->hprinter);
    if (jobstarted)
	EndDocPrinter(ret->hprinter);
    if (ret->hprinter)
	ClosePrinter(ret->hprinter);
    sfree(ret);
    return NULL;
}
void mswin_printer_thread(void *arg)
{
    int fd = (int)arg;
    char pname[gp_file_name_sizeof];
    char data[4096];
    HANDLE hprinter = INVALID_HANDLE_VALUE;
    int count;
    DWORD written;
    DOC_INFO_1 di;

    /* Read from pipe and write to Windows printer.
     * First gp_file_name_sizeof bytes are name of the printer.
     */
    if (read(fd, pname, sizeof(pname)) != sizeof(pname)) {
	/* Didn't get the printer name */
	close(fd);
	return;
    }

    while ( (count = read(fd, data, sizeof(data))) > 0 ) {
	if (hprinter == INVALID_HANDLE_VALUE) {
	    if (!OpenPrinter(pname, &hprinter, NULL)) {
		close(fd);
		return;
	    }
	    di.pDocName = (LPTSTR)gs_product;
	    di.pOutputFile = NULL;
	    di.pDatatype = "RAW";
	    if (!StartDocPrinter(hprinter, 1, (LPBYTE) & di)) {
		AbortPrinter(hprinter);
		close(fd);
		return;
	    }
	}
	if (!StartPagePrinter(hprinter)) {
		AbortPrinter(hprinter);
		close(fd);
		return;
	}
	if (!WritePrinter(hprinter, (LPVOID) data, count, &written)) {
	    AbortPrinter(hprinter);
	    close(fd);
	    return;
	}
    }
    if (hprinter != INVALID_HANDLE_VALUE) {
	if (count == 0) {
	    /* EOF */
	    EndPagePrinter(hprinter);
	    EndDocPrinter(hprinter);
	    ClosePrinter(hprinter);
	}
	else {
	    /* Error */
	    AbortPrinter(hprinter);
	}
    }
    close(fd);
}
Exemple #4
0
void printer_finish_job(printer_job *pj)
{
    if (!pj)
	return;

    EndPagePrinter(pj->hprinter);
    EndDocPrinter(pj->hprinter);
    ClosePrinter(pj->hprinter);
    sfree(pj);
}
Exemple #5
0
void
printer_finish_job(void)
{
  if (printer) {
    EndPagePrinter(printer);
    EndDocPrinter(printer);
    ClosePrinter(printer);
    printer = 0;
  }
}
Exemple #6
0
static void printer_win_close_printjob(rdpPrintJob* printjob)
{
    rdpWinPrintJob* win_printjob = (rdpWinPrintJob*)printjob;

    DEBUG_WINPR("");

    if ( ! EndPagePrinter( ((rdpWinPrinter*)printjob->printer)->hPrinter ) )
        DEBUG_WINPR("EndPagePrinter failed");;
    if ( ! ClosePrinter( ((rdpWinPrinter*)printjob->printer)->hPrinter ) )
        DEBUG_WINPR("ClosePrinter failed");;

    ((rdpWinPrinter*)printjob->printer)->printjob = NULL;
    free(win_printjob) ;
}
Exemple #7
0
/*****************************************************************************
 End document
 Done on PRINT CLOSE
*****************************************************************************/
static void EndDocument(void)
{
   DeleteObject( SelectObject( pd.hDC, hOldFont ) );
   if (defaultprinter)
   {
     EndPage( pd.hDC );
     EndDoc( pd.hDC );
   }
   else
   {
     EndPagePrinter( hPrinter );
     EndDocPrinter( hPrinter );
   }
   documentON = FALSE;
}
Exemple #8
0
void Demo_WritePrinter(void)
{
	PRINTDLG  pd;

	memset(&pd, 0, sizeof(PRINTDLG));
	pd.lStructSize = sizeof(PRINTDLG);

	if ( PrintDlg(&pd)==IDOK )
	{
		HANDLE hPrinter;

		DEVMODE * pDevMode = (DEVMODE *) GlobalLock(pd.hDevMode);

		PRINTER_DEFAULTS prn;
		prn.pDatatype     = "NT EMF 1.008";
		prn.pDevMode      = pDevMode;
		prn.DesiredAccess = PRINTER_ACCESS_USE;

		if ( OpenPrinter((char *) pDevMode->dmDeviceName, & hPrinter, & prn) )
		{
			KFileDialog fd;

			if ( fd.GetOpenFileName(NULL, "spl", "Windows 2000 EMF Spool file") )
			{
				DOC_INFO_1 docinfo;

				docinfo.pDocName    = "Testing WritePrinter";
				docinfo.pOutputFile = NULL;
				docinfo.pDatatype   = "NT EMF 1.008";

				StartDocPrinter(hPrinter, 1, (BYTE *) & docinfo);
				StartPagePrinter(hPrinter);
				
				SendFile(hPrinter, fd.m_TitleName, true);
				
				EndPagePrinter(hPrinter);
				EndDocPrinter(hPrinter);
			}

			ClosePrinter(hPrinter);
		}

		if ( pd.hDevMode )	GlobalFree(pd.hDevMode);
		if ( pd.hDevNames ) GlobalFree(pd.hDevNames);
	}
}
Exemple #9
0
static void printer_win_close_printjob(rdpPrintJob* printjob)
{
	rdpWinPrintJob* win_printjob = (rdpWinPrintJob*) printjob;

	if (!EndPagePrinter(((rdpWinPrinter*) printjob->printer)->hPrinter))
	{

	}

	if (!ClosePrinter(((rdpWinPrinter*) printjob->printer)->hPrinter))
	{

	}

	((rdpWinPrinter*) printjob->printer)->printjob = NULL;

	free(win_printjob);
}
Exemple #10
0
DWORD
_RpcEndPagePrinter(WINSPOOL_PRINTER_HANDLE hPrinter)
{
    DWORD dwErrorCode;

    dwErrorCode = RpcImpersonateClient(NULL);
    if (dwErrorCode != ERROR_SUCCESS)
    {
        ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
        return dwErrorCode;
    }

    if (!EndPagePrinter(hPrinter))
        dwErrorCode = GetLastError();

    RpcRevertToSelf();
    return dwErrorCode;
}
Exemple #11
0
/*****************************************************************************
 * Go to next page
*****************************************************************************/
static void NextPage( void )
{
   DeleteObject( SelectObject( pd.hDC, hOldFont ) );
   if (defaultprinter)
   {
     EndPage( pd.hDC );
     StartPage( pd.hDC );
   }
   else
   {
     EndPagePrinter( hPrinter );
     StartPagePrinter( hPrinter );
   }
   hOldFont = SetFont( );
   PrepareDC( );
   column = row = 0;
   page++;
   PageHeader( page+1 );
}
int RawPrinter::print(QString text)
{
    if (!opened) return 1;

    unsigned long len;
    QByteArray bytes = text.toLatin1();
    const char *buf = bytes.data();

    DOC_INFO_1 info;

    info.pDocName = L"Document";
    info.pOutputFile = NULL;
    info.pDatatype = L"RAW";

    StartDocPrinter(hPrinter, 1, (LPBYTE)&info);
    StartPagePrinter(hPrinter);
    int ret = WritePrinter(hPrinter, (LPVOID*)buf, strlen(buf), &len);
    EndPagePrinter(hPrinter);
    EndDocPrinter(hPrinter);
    if(ret) QTextStream(stdout) << "Error: " << GetLastError() << "\n";
    return ret;
}
//---------------------------------------------------------------------------
bool __fastcall TRawPrint::NovaPagina(void)
{
 bool Resultado = true;
 try
  {
   if (DispAberto)
    {
     if (PaginaAberta)
      if (!EndPagePrinter(hPrinter))
       Erro("Erro ao finalizar a Página");

     if (!StartPagePrinter(hPrinter))
      Resultado = false;
     else
      PaginaAberta = true;
    }
  }
 catch(Exception & E)
  {
   Erro("Erro ao gerar nova Página.\r\n\r\n" + E.Message + "\r\n");
  }
  return(Resultado);
};
Exemple #14
0
int main (int argc, char* argv[])
{
	
	HANDLE				printer;
	int					fd;
	BYTE				buffer[256];
	DWORD				bytes, wrote;
	DOC_INFO_1			buffer1;
	PRINTER_DEFAULTS	print_defaults;

	if (argc < 3)
	{
		fprintf (stderr, "useage: %s <printername> <filename>\n", argv[0]);
		exit (-1);
	}

	printf ("This test program exercises the following win32 functions:\n");
	printf ("\tResetPrinter()\n");
	printf ("\tStartDocPrinter()\n");
	printf ("\tEndDocPrinter()\n");
	printf ("\tStartPagePrinter()\n");
	printf ("\tEndPagePrinter()\n");
	printf ("\tWritePrinter()\n");
	printf ("\n\n");

	printf ("This test program exercises both the StartPagePrinter()\n");
	printf ("and EndPagePrinter() Win32 functions.\n\n");

	if (!OpenPrinter (argv[1], &printer, NULL))
	{
		fprintf (stderr, "Unable to open %s!\n", argv[1]);
		exit (-1);
	}
	else
	{
		printf ("Printer [%s] opened successfully.\n\n", argv[1]);
	}

	/* set the printer defaults */
	print_defaults.pDatatype		= strdup ("RAW");
	print_defaults.DesiredAccess	= NULL;
	print_defaults.pDevMode			= NULL;
	if (ResetPrinter(printer, &print_defaults))
		printf ("ResetPrinter call succeeded\n");
	else
		PrintLastError();
	printf ("\n");

	/* StartDocPrinter */
	buffer1.pDocName	= strdup("Testing Printer");
	buffer1.pDatatype	= strdup("RAW");
	buffer1.pOutputFile	= NULL;

	printf ("Attempting to call StartDocPrinter() using DOC_INFO_1 :\n");
	print_doc_info_1 (&buffer1);
	printf ("\n");

	if (StartDocPrinter (printer, 1, (LPBYTE)&buffer1))
		printf ("StartDocPrinter called successfully\n");
	else
		PrintLastError();
	printf ("\n");


	if (StartPagePrinter(printer))
	{
		printf ("StartPagePrinter returned success.\n");

		if ((fd=_open(argv[2], _O_RDONLY)) == -1)
		{
			fprintf (stderr, "ERROR: Unable to open [%s] for read access!\n", argv[2]);
			ClosePrinter (printer);
			exit (-1);
		}

		while ((bytes=_read(fd, buffer, 256)) != 0)
		{
			if (!WritePrinter(printer, (LPVOID)buffer, bytes, &wrote))
			{
				fprintf (stderr, "ERROR: WritePrinter failed for [%d] bytes!\n", bytes);
				PrintLastError();
			}
			else
				printf ("Successfully wrote [%d] bytes to the printer\n", bytes);
		}
		_close (fd);

		if (EndPagePrinter (printer))
			printf ("EndPagePrinter returned success.\n");
		else
			PrintLastError();
	}
	else
		PrintLastError();
	printf ("\n");
			
	printf ("Attempting to call EndDocPrinter\n");
	if (EndDocPrinter (printer))
		printf ("EndDocPrinter called successfully\n");
	else
		PrintLastError();
	printf ("\n");



	if (!ClosePrinter(printer))
	{
		fprintf (stderr, "Error closing printer!\n");
		exit (-1);
	}
	else
	{
		printf ("Printer [%s] closed successfully.\n", argv[1]);
	}

	return 0;

}
Exemple #15
0
// **********************************************************************
// RawDataToPrinter - sends binary data directly to a printer
// 
// Params:
//   szPrinterName - NULL terminated string specifying printer name
//   lpData        - Pointer to raw data bytes
//   dwCount       - Length of lpData in bytes
// 
// Returns: TRUE for success, FALSE for failure.
// 
BOOL RawDataToPrinter( LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount )
{
    HANDLE     hPrinter;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob;
    DWORD      dwBytesWritten;

    // Need a handle to the printer.
    if( ! OpenPrinter( szPrinterName, &hPrinter, NULL ) )
    {
        PrintError( GetLastError(), TEXT("OpenPrinter") );
        return FALSE;
    }

    // Fill in the structure with info about this "document."
    DocInfo.pDocName = TEXT("My Document");
    DocInfo.pOutputFile = NULL;
    DocInfo.pDatatype = TEXT("RAW");
    // Inform the spooler the document is beginning.
    if( (dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo )) == 0 )
    {
        PrintError( GetLastError(), TEXT("StartDocPrinter") );
        ClosePrinter( hPrinter );
        return FALSE;
    }
    // Start a page.
    if( ! StartPagePrinter( hPrinter ) )
    {
        PrintError( GetLastError(), TEXT("StartPagePrinter") );
        EndDocPrinter( hPrinter );
        ClosePrinter( hPrinter );
        return FALSE;
    }
    // Send the data to the printer.
    if( ! WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )
    {
        PrintError( GetLastError(), TEXT("WritePrinter") );
        EndPagePrinter( hPrinter );
        EndDocPrinter( hPrinter );
        ClosePrinter( hPrinter );
        return FALSE;
    }

    /*********************************/
    // CODE USED TO READ THE PRINTER
    LPBYTE retData = NULL;
    LPDWORD bbr = NULL;

    if(ReadPrinter(hPrinter, retData, 1, bbr))
    {
        printf("OUT : %i", retData);
    }
    else
    {
        printf("Failed to read printer");
    }
    /*********************************/

    // End the page.
    if( ! EndPagePrinter( hPrinter ) )
    {
        PrintError( GetLastError(), TEXT("EndPagePrinter") );
        EndDocPrinter( hPrinter );
        ClosePrinter( hPrinter );
        return FALSE;
    }
    // Inform the spooler that the document is ending.
    if( ! EndDocPrinter( hPrinter ) )
    {
        PrintError( GetLastError(), TEXT("EndDocPrinter") );
        ClosePrinter( hPrinter );
        return FALSE;
    }
    // Tidy up the printer handle.
    ClosePrinter( hPrinter );
    // Check to see if correct number of bytes were written.
    if( dwBytesWritten != dwCount )
    {
        //printf( TEXT("Wrote %d bytes instead of requested %d bytes.\n"), dwBytesWritten, dwCount );
        return FALSE;
    }
    return TRUE;
}