Using system;using system.drawing;using system.drawing.printing;using system.io;using System.runtime.interopservices;using System.windows.forms;public class rawprinterhelper{//Structure and API Declarions: [StructLayout (LayoutKind.Sequential, Charset=charset.ansi)] public class Docinfoa {[MarshalAs (unmanaged TYPE.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)] public static extern bool OpenPrinter ([MarshalAs (UNMANAGEDTYPE.LPSTR)] string szprinter, Out IntPtr hprinter, IntPtr PD); [DllImport ("WinSpool. Drv ", entrypoint=" ClosePrinter ", Setlasterror=true, Exactspelling=true, callingconvention= Callingconvention.stdcall)] public static extern bool ClosePrinter (IntPtr hprinter); [DllImport ("WinSpool. Drv ", EntrypOint= "Startdocprintera", Setlasterror=true, Charset=charset.ansi, Exactspelling=true, CallingConvention= Callingconvention.stdcall)] public static extern bool StartDocPrinter (IntPtr hprinter, Int32 level, [In, MarshalAs (Unma nagedtype.lpstruct)] Docinfoa di); [DllImport ("WinSpool. Drv ", entrypoint=" Enddocprinter ", Setlasterror=true, Exactspelling=true, callingconvention= Callingconvention.stdcall)] public static extern bool Enddocprinter (IntPtr hprinter); [DllImport ("WinSpool. Drv ", entrypoint=" Startpageprinter ", Setlasterror=true, Exactspelling=true, callingconvention= Callingconvention.stdcall)] public static extern bool Startpageprinter (IntPtr hprinter); [DllImport ("WinSpool. Drv ", entrypoint=" Endpageprinter ", Setlasterror=true, Exactspelling=true, callingconvention= Callingconvention.stdcall)] public static extern bool Endpageprinter (IntPtr hprinter); [DllImport ("WinSpool. Drv ", entrypoint=" WritePrinter ", Setlasterror=true, Exactspelling=true, CALLINGCONVENTION=CALLINGCONVENTION.STdcall)] public static extern bool WritePrinter (IntPtr hprinter, IntPtr pbytes, Int32 dwcount, out Int32 dwwritten); SendBytesToPrinter ()//When the function was given a printer name and an unmanaged array//of bytes, the function se NDS those bytes to the print queue. Returns true on success, false on failure. public static bool SendBytesToPrinter (string szprintername, IntPtr pbytes, Int32 dwcount) {Int32 dwerror = 0, DWW Ritten = 0; IntPtr hprinter = new IntPtr (0); Docinfoa di = new Docinfoa (); BOOL bsuccess = false; Assume failure unless you specifically succeed. Di.pdocname = "My c#.net RAW Document"; Di.pdatatype = "RAW"; Open the printer. if (OpenPrinter (Szprintername.normalize (), out Hprinter, IntPtr.Zero)) {//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 do not succeed, GetLastError could give more information//for why not. if (bsuccess = = False) {dwerror = Marshal.GetLastWin32Error (); } return bsuccess; public static bool Sendfiletoprinter (string szprintername, String szfilename) {//Open the file. FileStream fs = new FileStream (szFileName, FileMode.Open); Create a binaryreader on the file. BinaryReader br = new BinaryReader (FS); Dim an array of bytes big enough to hold the file ' s contents. Byte []bytes = new Byte[fs. Length]; BOOL bsuccess = false; Your unmanaged pointer. IntPtr punmanagedbytes = new IntPtr (0); int nlength; Nlength = Convert.ToInt32 (fs. Length); Read the contents of the file into the array. bytes = br. Readbytes (nlength); Allocate some unmanaged memory for THose bytes. pUnmanagedBytes = Marshal.alloccotaskmem (nlength); Copy the managed byte array into the unmanaged array. Marshal.Copy (bytes, 0, pUnmanagedBytes, nlength); Send the unmanaged bytes to the printer. bsuccess = SendBytesToPrinter (szPrinterName, pUnmanagedBytes, nlength); Free the unmanaged memory so you allocated earlier. Marshal.freecotaskmem (punmanagedbytes); return bsuccess; public static bool Sendstringtoprinter (string szprintername, String szstring) {IntPtr pbytes; Int32 dwcount; How many characters is in the string? Fix from Nicholas Piasecki://dwcount = Szstring.length; Dwcount = (szstring.length + 1) * marshal.systemmaxdbcscharsize; Assume 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; }}
Public partial class usb:form{public USB () {InitializeComponent (); }//Structure and API declarions: [StructLayout (layoutkind.sequential, CharSet = charset.ansi)] public class DOC INFOA {[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)] public static extern bool OpenPrinter ([MarshalAs (UNMANAGEDTYPE.LPSTR)] string szprinter, O UT IntPtr hprinter, IntPtr PD); [DllImport ("WinSpool. Drv ", EntryPoint =" ClosePrinter ", SetLastError = True, ExactSpelling = true, CallingConvention = callingconvention.stdcal L)] public static extern bool ClosePrinter (IntPtr hprinter); [DllImport ("WinSpool. Drv ", EntryPoint =" StarTdocprintera ", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = Callingconvention.st Dcall)] public 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.STDCA LL)] public static extern bool Enddocprinter (IntPtr hprinter); [DllImport ("WinSpool. Drv ", EntryPoint =" Startpageprinter ", SetLastError = True, ExactSpelling = true, CallingConvention = Callingconvention.st Dcall)] public static extern bool Startpageprinter (IntPtr hprinter); [DllImport ("WinSpool. Drv ", EntryPoint =" Endpageprinter ", SetLastError = True, ExactSpelling = true, CallingConvention = Callingconvention.stdc All)] public static extern bool Endpageprinter (IntPtr hprinter); [DllImport ("WinSpool. Drv ", EntryPoint =" WritePrinter ", SetLastError = true, ExactSpelling = tRue, callingconvention = Callingconvention.stdcall)] public static extern bool WritePrinter (IntPtr hprinter, IntPtr pBy TES, Int32 dwcount, out Int32 dwwritten); SendBytesToPrinter ()//When the function was given a printer name and an unmanaged array//of bytes, the Functio n sends those bytes to the print queue. Returns true on success, false on failure. public static bool SendBytesToPrinter (string szprintername, IntPtr pbytes, Int32 dwcount) {Int32 dwerror = 0, D Wwritten = 0; IntPtr hprinter = new IntPtr (0); Docinfoa di = new Docinfoa (); BOOL bsuccess = false; Assume failure unless you specifically succeed. Di.pdocname = "My c#.net RAW Document"; Di.pdatatype = "RAW"; Open the printer. if (OpenPrinter (Szprintername.normalize (), out Hprinter, IntPtr.Zero)) {//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 do not succeed, GetLastError could give more information//for why not. if (bsuccess = = False) {dwerror = Marshal.GetLastWin32Error (); } return bsuccess; public static bool Sendfiletoprinter (string szprintername, String szfilename) {//Open the file. FileStream fs = new FileStream (szFileName, FileMode.Open); Create a binaryreader on the file. BinaryReader br = new BinaryReader (FS); Dim an array of bytes big enough to hold the file ' s contents. byte[] bytes = new Byte[fs. Length]; BOOL bsuccess = false; Your UnmanAged pointer. IntPtr punmanagedbytes = new IntPtr (0); int nlength; Nlength = Convert.ToInt32 (fs. Length); Read the contents of the file into the array. bytes = br. Readbytes (nlength); Allocate Some unmanaged memory for those bytes. pUnmanagedBytes = Marshal.alloccotaskmem (nlength); Copy the managed byte array into the unmanaged array. Marshal.Copy (bytes, 0, pUnmanagedBytes, nlength); Send the unmanaged bytes to the printer. bsuccess = SendBytesToPrinter (szPrinterName, pUnmanagedBytes, nlength); Free the unmanaged memory so you allocated earlier. Marshal.freecotaskmem (punmanagedbytes); return bsuccess; public static bool Sendstringtoprinter (string szprintername, String szstring) {IntPtr pbytes; Int32 dwcount; How many characters is in the string? Fix from Nicholas Piasecki://dwcount = Szstring.length; Dwcount = (sZstring.length + 1) * marshal.systemmaxdbcscharsize; Assume 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; private void Buttonsend_click (object sender, EventArgs e) {System.Windows.Forms.OpenFileDialog open = New System.Windows.Forms.OpenFileDialog (); string dados = ""; if (open. ShowDialog (). Equals (DialogResult.OK)) {StreamReader sr = new StreamReader (open. FileName); Dados = Sr. ReadToEnd (); Sr. Close (); } String printer = "Printer NAME"; for (int i = 0; i < i++) {sendstringtoprinter (printer, dados); } }}
Sending data to USB printer in C #?