100% found this document useful (3 votes)
136 views4 pages

Print Direct

The document defines a C# class for printing raw bytes to a printer. It includes methods for opening and closing a printer handle, starting and ending a print job and page, and writing bytes to the printer. It also defines structures for print job info and demonstrates how to send a string to the printer by first converting it to bytes.

Uploaded by

api-3849768
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
136 views4 pages

Print Direct

The document defines a C# class for printing raw bytes to a printer. It includes methods for opening and closing a printer handle, starting and ending a print job and page, and writing bytes to the printer. It also defines structures for print job info and demonstrates how to send a string to the printer by first converting it to bytes.

Uploaded by

api-3849768
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

public class printthroughdriver

// structure and api declarions:

[structlayout(layoutkind.sequential, charset=charset.ansi)]

internal class docinfoa

[marshalas(unmanagedtype.lpstr)] public string pdocname;

[marshalas(unmanagedtype.lpstr)] public string poutputfile;

[marshalas(unmanagedtype.lpstr)] public string pdatatype;

[dllimport("winspool.drv", entrypoint="openprintera", setlasterror=true,


charset=charset.ansi, exactspelling=true,
callingconvention=callingconvention.stdcall)]

internal static extern bool openprinter([marshalas(unmanagedtype.lpstr)] string


szprinter, out intptr hprinter, long pd);

[dllimport("winspool.drv", entrypoint="closeprinter", setlasterror=true,


exactspelling=true, callingconvention=callingconvention.stdcall)]

internal static extern bool closeprinter(intptr hprinter);

[dllimport("winspool.drv", entrypoint="startdocprintera", setlasterror=true,


charset=charset.ansi, exactspelling=true,
callingconvention=callingconvention.stdcall)]

internal static extern bool startdocprinter( intptr hprinter, int32 level, [in,
marshalas(unmanagedtype.lpstruct)] docinfoa di);

[dllimport("winspool.drv", entrypoint="enddocprinter", setlasterror=true,


exactspelling=true, callingconvention=callingconvention.stdcall)]

internal static extern bool enddocprinter(intptr hprinter);

[dllimport("winspool.drv", entrypoint="startpageprinter", setlasterror=true,


exactspelling=true, callingconvention=callingconvention.stdcall)]

internal static extern bool startpageprinter(intptr hprinter);

[dllimport("winspool.drv", entrypoint="endpageprinter", setlasterror=true,


exactspelling=true, callingconvention=callingconvention.stdcall)]

internal static extern bool endpageprinter(intptr hprinter);

[dllimport("winspool.drv", entrypoint="writeprinter", setlasterror=true,


exactspelling=true, callingconvention=callingconvention.stdcall)]

internal static extern bool writeprinter(intptr hprinter, intptr pbytes, int32


dwcount, out int32 dwwritten );

[dllimport("kernel32.dll", entrypoint="getlasterror", setlasterror=false,


exactspelling=true, callingconvention=callingconvention.stdcall)]

internal static extern int32 getlasterror();

// sendbytestoprinter()

// when the function is given a printer name and an unmanaged array

// of bytes, the function sends those bytes to the print queue.

// returns true on success, false on failure.

private static bool sendbytestoprinter( string szprintername, intptr pbytes, int32


dwcount)

int32 dwerror = 0, dwwritten = 0;

intptr hprinter = new intptr(0);

docinfoa di = new docinfoa();

bool bsuccess = false; // assume failure unless you specifically succeed.

di.pdocname = "c# debug print raw document";

di.pdatatype = "raw";

// open the printer.

if( openprinter( szprintername, out hprinter, 0 ) )

// start a document.

if( startdocprinter(hprinter, 1, di) )

// start a page.

if( startpageprinter(hprinter) )

// write your bytes.

bsuccess = writeprinter(hprinter, pbytes, dwcount, out dwwritten);

endpageprinter(hprinter);

}
enddocprinter(hprinter);

closeprinter(hprinter);

// if you did not succeed, getlasterror may give more information

// about why not.

if( bsuccess == false )

dwerror = getlasterror();

return bsuccess;

public static bool sendstringtoprinter( string szprintername, string szstring )

intptr pbytes;

int32 dwcount;

// how many characters are in the string?

dwcount = szstring.length;

// assume that the printer is expecting ansi text, and then convert

// the string to ansi text.

pbytes = marshal.stringtocotaskmemansi(szstring);

// send the converted ansi string to the printer.

sendbytestoprinter(szprintername, pbytes, dwcount);

marshal.freecotaskmem(pbytes);

return true;

internal struct docinfo


{

[marshalas(unmanagedtype.lpwstr)]public string pdocname;

[marshalas(unmanagedtype.lpwstr)]public string poutputfile;

[marshalas(unmanagedtype.lpwstr)]public string pdatatype;

you have make sure that you're sending the proper bytes to the printer. if you're
just sending a byte representation of the string of commands you're not actually
sending the bytes for the actual commands.

You might also like