Article ID: 124649
Article Last Modified on 3/7/2005
35708 SAMPLE: Raw.exe Sends Binary Data to Printer w/Device Driver
// The Abort Procedure for printing (a generic AbortProc)
// This procedure needs to listed be in the Exports section
// of the .def file
BOOL CALLBACK AbortProc( HDC hDC, int Error )
{
MSG msg;
while(PeekMessage(&msg, NULL, 0, 0, TRUE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return TRUE;
}
// The function that prints one line without formfeed
BOOL PrintLineNow( char *pData, WORD cbBytes )
{
#define LINEBUFFER_LENGTH 80 //or dynamically alloc enough space
HDC hDC;
char pOutput[LINEBUFFER_LENGTH+sizeof(WORD)];
DOCINFO di;
ABORTPROC lpfnAbortProc;
// Buffer size is LINEBUFFER_LENGTH bytes
if( cbBytes > LINEBUFFER_LENGTH )
return FALSE;
// Get the DC using the RAW.DRV driver
if( (hDC = CreateDC( "RAW", NULL, "LPT1", NULL ) ) == NULL )
return FALSE;
// Set up an Abort Procedure
if((lpfnAbortProc = MakeProcInstance(AbortProc, hInst)) == NULL)
{
DeleteDC( hDC );
return FALSE;
}
if( SetAbortProc( hDC, lpfnAbortProc ) <= 0 )
{
DeleteDC( hDC );
FreeProcInstance( lpfnAbortProc );
return FALSE;
}
// Start a Document
di.cbSize = sizeof( DOCINFO );
di.lpszDocName = "MyDoc";
di.lpszOutput = NULL;
if( StartDoc( hDC, &di ) <= 0 )
{
DeleteDC( hDC );
FreeProcInstance( lpfnAbortProc );
return FALSE;
}
// The start of a page
if( StartPage( hDC ) <= 0 )
{
DeleteDC( hDC );
FreeProcInstance( lpfnAbortProc );
return FALSE;
}
// Put data in the buffer and send to the printer
*(WORD *)pOutput = cbBytes;
memcpy( &(pOutput[sizeof(WORD)]), pData, cbBytes );
if( Escape( hDC, PASSTHROUGH, 0, pOutput, NULL ) <= 0 )
{
DeleteDC( hDC );
FreeProcInstance( lpfnAbortProc );
return FALSE;
}
// The end of the page
if( EndPage( hDC ) < 0 )
{
DeleteDC( hDC );
FreeProcInstance( lpfnAbortProc );
return FALSE;
}
// End the Document
if( EndDoc( hDC ) < 0 )
{
DeleteDC( hDC );
FreeProcInstance( lpfnAbortProc );
return FALSE;
}
// Clean up
DeleteDC( hDC );
FreeProcInstance( lpfnAbortProc );
return TRUE; // Success
}
Additional query words: NEWFRAME FF Mailing Label Form Feed GdiPrn
Keywords: kbhowto kb16bitonly KB124649