1

I am try to develop the driver for my printer,which is working on com port. it is a thermal printer most focus on receipt printing.but i don't have any driver for this printer so now i using windows "GENERIC \TEXT ONLY PRINTER" drivers.it working but the problem is i cant control the language .i have some doubt about the windows generic text only driver

1.is it possible to using generic\text only mode for various fonts and language ?

2.whether it has own language monitor and OEM driver?

3.is it possible to integrate any gpd file with generic mode driver

and also

Basically for any printer has three major component in driver that are language monitor,OEM uni driver,User Interface ,that driver must developed in WDK ,for any open source available for this kind for printer driver development.

any have idea about this .

Mr.Cool
  • 1,525
  • 10
  • 32
  • 51

2 Answers2

2

So I´m back now, and I told something wrong - it was not VB6, it was already C# - You will have to translate this (basically you can use the same api calls ..)

I also found this code somewhere in the net ... it did not escape from my brain

It was a service, used for an old DOS app to print to a HP Laserjet (The Dos app knows how to use PCL).

The call:

RawPrinterHelper.SendFileToPrinter(new PrinterSettings().PrinterName, fileName);

And the Helper Class:

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Runtime.InteropServices;

namespace RAW_Print
{
    public class RawPrinterHelper
    {
        [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, 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(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);

        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;

            di.pDocName = "your doc name";
            di.pDataType = "RAW";

            if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
            {
                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 = Marshal.GetLastWin32Error();
            }
            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);
            br.Close();
            fs.Close();
            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;
        }
    }
}
nabuchodonossor
  • 2,095
  • 20
  • 18
  • actually this will like sent data to already installed printer right .actually what i need is once you gave print means GDI loads your driver and it covert to PCl or some other format (for this part all manufacture wrote some dll and .gpl which is used by spooler and GDI in windows) here i asking about how to write that files – Mr.Cool Nov 11 '13 at 10:23
  • Seems that I did not exactly understand your question. What I thought is you have a printer installed as GENERIC/TEXT and want to use PCL language to print to it. Are you forced to write a driver for an existing application, or are you writing an application in C using this thermal printer? – nabuchodonossor Nov 11 '13 at 10:52
  • now i installed my printer using Generic\Text only mode.that working fine but one drawback is i cant control the language .for example i set my printer default language as English but when i send other language means my printer does not not print correctly ,because data came to printer doesn't have any language and font information it only have the data, so for this case if i write own driver means i cant customized the data what i going to send.that's why i asking – Mr.Cool Nov 11 '13 at 11:17
0

In my ancient programming times with VB6 it was possible to open the printer as "RAW printer", that is in fact a driverless printing method - you have the complete work.

I´ll start a search in my old sources for this, I´m quite sure, you can translate this to c language if you need it.

nabuchodonossor
  • 2,095
  • 20
  • 18
  • oh thanks,that means there is no need for any pCl and post script conversion right ,can you share yr code? – Mr.Cool Nov 11 '13 at 09:56
  • No, you have to send PCL commands to the printer, and the code is in the other answer. I have it only in C#, but you see the api calls, and can use it in C also. – nabuchodonossor Nov 11 '13 at 10:05