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

J

Joe

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
 
G

Guest

Thank you very much for responding.
I will try it out.



Joe said:
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 said:
How to open cash drawer through receipt printer in c#?
 
G

Guest

I have a "MMF ECD200 Epson" cash drawer connected to "SAMSUNG SRP-350"
printer which is then connected to my computer.

It is fine to print a text file to the printer only, but I can not use the
code you sent to open the cash drawer.

this is what i am doing:

RawPrinterHelper.OpenCashDrawer1("SAMSUNG SRP-350")

Do you know where the problem is?

Thank you


Joe said:
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 said:
How to open cash drawer through receipt printer in c#?
 
J

Joe

Nope, sorry - the code I gave works for Epson and star printers.

It is the printer that controls the drawer - and usually the drawer does not
get involved - so look at your printer manual and see what character it
expects. Then look and the code and make sure it is sending the correct
character - it is only 1 byte - which is why you have to go through all this
stuff - sending 1 byte is far more difficult than a whole page.

good luck.

c#newbie said:
I have a "MMF ECD200 Epson" cash drawer connected to "SAMSUNG SRP-350"
printer which is then connected to my computer.

It is fine to print a text file to the printer only, but I can not use the
code you sent to open the cash drawer.

this is what i am doing:

RawPrinterHelper.OpenCashDrawer1("SAMSUNG SRP-350")

Do you know where the problem is?

Thank you


Joe said:
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 said:
How to open cash drawer through receipt printer in c#?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top