Thread: c and printers
View Single Post
Old 03-17-2005, 05:28 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
Google found this for me:
Code:
#define PRINTDRIVER

#include <windows.h>
#include <string.h>
#include <print.h>
#include <stdio.h>

int PrintHPFile( const char *filename )
{
        char *pszPrInfo = new char[ 80 ];

        // Get the printer setup string from the ini file
        GetProfileString( "windows", "device", ",,,", pszPrInfo, 80 );

        // Format of ini file line = <Device,Driver,Port>
        char *pszDevice = strtok( pszPrInfo, "," );
        char *pszDriver = strtok( NULL, "," );
        char *pszPort = strtok( NULL, "," );

        // create a printer device context
        HDC PrintHandle = CreateDC( pszDriver, pszDevice, pszPort,
                        NULL );

        // get a job handle, allocate a buffer
        HPJOB PrintJob;
        char *buf = new char[ 1024 ];

        // open the file for reading
        FILE *hp_file = fopen( filename, "rb" );

        // Start the print job
        PrintJob = OpenJob( pszPort, filename, (HPJOB)PrintHandle );
        StartSpoolPage( PrintJob );

        // print the file, 1K at a time
        for( size_t i(0);; )
        {
                i = fread( buf, 1, 1023, hp_file );
                if( i>0 )
                        WriteSpool( PrintJob, buf, i );
                else
                        break;
        }

        // Close up print job
        EndSpoolPage( PrintJob );
        CloseJob( PrintJob );

        // Clean up
        DeleteDC( PrintHandle );
        fclose( hp_file );
        delete[] pszPrInfo;
        delete[] buf;
        return 1;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote