Re: How to open cash drawer through receipt printer in c#?

From: Joe (Joe_at_microsoft.com)
Date: 01/19/05


Date: Wed, 19 Jan 2005 16:20:54 -0500

using System;

using System.IO;

using System.Runtime.InteropServices;

namespace PosigoCleaners

{

public class RawPrinterHelper

{

// Structure and API declarions:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]

public 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)]

public 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)]

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(UnmanagedType.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 );

[DllImport("kernel32.dll", EntryPoint="GetLastError", SetLastError=false,
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]

public static extern Int32 GetLastError();

public 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 = "My C#.NET RAW Document";

di.pDataType = "RAW";

if( OpenPrinter( szPrinterName, out hPrinter, 0 ) )

{

if( StartDocPrinter(hPrinter, 1, di) )

{

if( StartPagePrinter(hPrinter) )

{

bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);

EndPagePrinter(hPrinter);

}

EndDocPrinter(hPrinter);

}

ClosePrinter(hPrinter);

}

if( bSuccess == false )

{

dwError = GetLastError();

}

return bSuccess;

}

public static bool SendFileToPrinter( string szPrinterName, string
szFileName )

{

FileStream fs = new FileStream(szFileName, FileMode.Open);

BinaryReader br = new BinaryReader(fs);

Byte []bytes = new Byte[fs.Length];

bool bSuccess = false;

IntPtr pUnmanagedBytes = new IntPtr(0);

int nLength;

nLength = Convert.ToInt32(fs.Length);

bytes = br.ReadBytes( nLength );

pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);

Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);

bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);

Marshal.FreeCoTaskMem(pUnmanagedBytes);

return bSuccess;

}

public static bool SendStringToPrinter( string szPrinterName, string
szString )

{

IntPtr pBytes;

Int32 dwCount;

dwCount = szString.Length;

pBytes = Marshal.StringToCoTaskMemAnsi(szString);

SendBytesToPrinter(szPrinterName, pBytes, dwCount);

Marshal.FreeCoTaskMem(pBytes);

return true;

}

public static bool OpenCashDrawer1( string szPrinterName)

{

Int32 dwError = 0, dwWritten = 0;

IntPtr hPrinter = new IntPtr(0);

DOCINFOA di = new DOCINFOA();

bool bSuccess = false;

di.pDocName = "OpenDrawer";

di.pDataType = "RAW";

if( OpenPrinter( szPrinterName, out hPrinter, 0 ) )

{

if( StartDocPrinter(hPrinter, 1, di) )

{

if( StartPagePrinter(hPrinter) )

{

int nLength;

byte[] DrawerOpen = new byte[] { 07 };

nLength = DrawerOpen.Length;

IntPtr p = Marshal.AllocCoTaskMem(nLength);

Marshal.Copy(DrawerOpen, 0, p, nLength);

bSuccess = WritePrinter(hPrinter, p, DrawerOpen.Length, out dwWritten);

EndPagePrinter(hPrinter);

Marshal.FreeCoTaskMem(p);

}

EndDocPrinter(hPrinter);

}

ClosePrinter(hPrinter);

}

if( bSuccess == false )

{

dwError = GetLastError();

}

return bSuccess;

}

public static bool FullCut( string szPrinterName)

{

Int32 dwError = 0, dwWritten = 0;

IntPtr hPrinter = new IntPtr(0);

DOCINFOA di = new DOCINFOA();

bool bSuccess = false;

di.pDocName = "FullCut";

di.pDataType = "RAW";

if( OpenPrinter( szPrinterName, out hPrinter, 0 ) )

{

if( StartDocPrinter(hPrinter, 1, di) )

{

if( StartPagePrinter(hPrinter) )

{

int nLength;

byte[] DrawerOpen = new byte[] { 27, 100, 51 };

nLength = DrawerOpen.Length;

IntPtr p = Marshal.AllocCoTaskMem(nLength);

Marshal.Copy(DrawerOpen, 0, p, nLength);

bSuccess = WritePrinter(hPrinter, p, DrawerOpen.Length, out dwWritten);

EndPagePrinter(hPrinter);

Marshal.FreeCoTaskMem(p);

}

EndDocPrinter(hPrinter);

}

ClosePrinter(hPrinter);

}

if( bSuccess == false )

{

dwError = GetLastError();

}

return bSuccess;

}

}

}

forgive the formatting - but be thankful

"c#newbie" <c#newbie@discussions.microsoft.com> wrote in message
news:BE7B01EF-67A7-4B7C-958D-669F8018AB27@microsoft.com...
> How to open cash drawer through receipt printer in c#?



Relevant Pages

  • Re: How to print a spool file.
    ... public static extern bool OpenPrinterstring szPrinter, out IntPtr hPrinter, ... bool bSuccess = false; // Assume failure unless you specifically succeed. ...
    (microsoft.public.vstudio.development)
  • Re: qbasic to vb.net conversion help. please.
    ... You set bSuccess three times in your inner IF stmt, and then, when you come ... > String, ByVal szFileName As String, ByVal header As String, ByVal footer ... >> programmer helping programmers. ...
    (microsoft.public.dotnet.general)
  • How to getting an owner of file
    ... Private Declare Function GetSecurityDescriptorOwner Lib ... ByVal lpSystemName As String, _ ... bSuccess = GetFileSecurity(szfilename, ... Dim fldrPathName, fname, fowner As String ...
    (microsoft.public.vb.winapi)
  • File Owner
    ... ByVal lpSystemName As String, _ ... bSuccess = GetFileSecurity(szfilename, ... ' exit if any error ... Dim fldrPathName, fname, fowner As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: FTP CD command
    ... public const int GENERIC_WRITE = 0x40000000; ... string lpszProxyName, ... public static extern IntPtr InternetConnect ( ... public static extern bool FtpGetCurrentDirectory ( ...
    (microsoft.public.dotnet.languages.vb)