Ausgabe am Drucker



  • Hallo Leute,

    ich lerne gerade c, und habe versucht den Inhalt einer Datei unter DOS
    auf den Drucker mit dem stdprn auszugeben, was nicht geklappt hat. Ich benutze Win2000, und einen Drucker der über USB angeschlossen ist. Gibt es eine Möglichkeit(BEFEHL)die Ausgabe am Drucker zu übergeben.

    DANKE IM VORAUS! 🙂



  • Original erstellt von bbatec:
    **Hallo Leute,

    ich lerne gerade c, und habe versucht den Inhalt einer Datei unter DOS
    auf den Drucker mit dem stdprn auszugeben, was nicht geklappt hat. Ich benutze Win2000, und einen Drucker der über USB angeschlossen ist. Gibt es eine Möglichkeit(BEFEHL)die Ausgabe am Drucker zu übergeben.

    DANKE IM VORAUS! :)**

    1.) Du arbeitest nicht unter DOS sondern unter Win2000 auf der DOS-Konsole (wichtiger Unterschied)

    2.) was mit ANSI C möglich ist:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_BUFF_SIZE     100
    
    int main(void)
    {
      // I could have used a CString instead of the buff[]
      // but I wanted to show how this is used with lightweight
      // ATL code and STD library
      char buff[MAX_BUFF_SIZE];
    
      FILE *ptr=NULL;
    
      // old fashioned file handle with
      // old fashioned open of the printer port
      ptr = fopen("LPT1","w");
    
      // laser printer setup string
      sprintf(buff,"\033E\033(s0p4102t1b16.66H\033&l1O");
      fprintf(ptr,buff);
    
      // old fashioned print
      fprintf(ptr,"Who of late doth make a thimble.\n");
      fprintf(ptr,"Is a lower bunk a status symbol??\n");
    
      // old fashioned close
      fclose(ptr);
    
      return 0;
    }
    

    Wenn dein Drucker NICHT an LPT1 angeschlossen ist (ist er ja bei dir nicht, da es sich um einen per USB angehängten Drucker handelt) dann musst du den Drucker über die "Verzeichnis- und Druckerfreigaben" freigeben und in deinem Programm den Drucker per "net use" Befehl auf LPT1 binden:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_BUFF_SIZE     100
    #define PRINTER_IS_REMOTE 1
    
    int main(void)
    {
      // I could have used a CString instead of the buff[]
      // but I wanted to show how this is used with lightweight
      // ATL code and STD library
      char buff[MAX_BUFF_SIZE];
    
      FILE *ptr=NULL;
    
      if (PRINTER_IS_REMOTE)
        {
          system("NET USE LPT1 /d");  // free up the port
          system("net use lpt1 \\\\Server\\PrinterShare");
        }
    
      // old fashioned file handle with
      // old fashioned open of the printer port
      ptr = fopen("LPT1","w");
    
      // laser printer setup string
      sprintf(buff,"\033E\033(s0p4102t1b16.66H\033&l1O");
      fprintf(ptr,buff);
    
      // old fashioned print
      fprintf(ptr,"Who of late doth make a thimble.\n");
      fprintf(ptr,"Is a lower bunk a status symbol??\n");
    
      // old fashioned close
      fclose(ptr);
    
      // drop the network link
      if (PRINTER_IS_REMOTE)
        {
          system("net use lpt1 /d");
        }
    
      return 0;
    }
    

    als letztes könntest du natürlich auch direkt mit der WinAPI arbeiten (was aber dann in DIESEM Forum "etwas" off-topic wäre:

    /*
     * Siehe MSDN
     * HOWTO: Send Raw Data to a Printer by Using the Win32 API
     * Article ID: Q138594
     * Microsoft Win32 Software Development Kit (SDK) version 3.5
     */
    
    /* 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;
    }
    

Anmelden zum Antworten