Inviare dati non elaborati a una stampante usando l'API Win32

Questo articolo illustra come inviare dati non elaborati a una stampante usando l'API Win32.

Versione originale del prodotto: Windows API
Numero KB originale: 138594

Riepilogo

A volte è necessario inviare dati specifici della stampante direttamente a una stampante, ignorando il driver. L'API Win32 offre un'operazione che funziona su stampanti locali e di rete. Questo metodo può essere usato per sostituire i PASSTHROUGH metodi di escape e SpoolFile() usati nelle versioni precedenti dell'API di Windows.

Esempio di codice

È possibile usare il codice seguente per inviare dati non elaborati direttamente a una stampante in Windows NT o Windows 95.

// 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(LPSTR 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))
    return FALSE;

    // Fill in the structure with info about this "document."
    DocInfo.pDocName = "My Document";
    DocInfo.pOutputFile = NULL;
    DocInfo.pDatatype = "RAW";
    // Inform the spooler the document is beginning.
    if((dwJob = StartDocPrinter(hPrinter, 1, (LPSTR)&DocInfo)) == 0)
    {
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // Start a page.
    if(!StartPagePrinter(hPrinter))
    {
      EndDocPrinter(hPrinter);
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // Send the data to the printer.
    if(!WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten))
    {
      EndPagePrinter(hPrinter);
      EndDocPrinter(hPrinter);
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // End the page.
    if(!EndPagePrinter(hPrinter))
    {
      EndDocPrinter(hPrinter);
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // Inform the spooler that the document is ending.
    if(!EndDocPrinter(hPrinter))
    {
      ClosePrinter(hPrinter);
      return FALSE;
    }
    // Tidy up the printer handle.
    ClosePrinter(hPrinter);
    // Check to see if correct number of bytes were written.
    if(dwBytesWritten != dwCount)
      return FALSE;
      return TRUE;
}

Il file seguente è disponibile per il download dall'Area download Microsoft:

RAWPRN.EXE

Per altre informazioni su come scaricare supporto tecnico Microsoft file, vedere Come ottenere i file di supporto Microsoft da Servizi online.

Microsoft ha analizzato questo file alla ricerca di virus. Microsoft ha usato il software di rilevamento dei virus più recente disponibile alla data di pubblicazione del file. Il file viene archiviato in server con sicurezza avanzata che consentono di evitare modifiche non autorizzate al file.