Re: Escape sequence to dotmatrix printer - Escape() API

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Glenn Wilson (ircomm_at_hotemail.com)
Date: 08/03/04


Date: Tue, 3 Aug 2004 20:24:07 +1000

We used to add chr(27) to the text to represent the escape character. adding
chr(27)+"R" + <Rest of string>

BTW: I Think it is CHR(27) Look at the ANSII Character Codes, It has been a
While but we used to use them in Linux based MUDS to transmit Color codes
and Cursor movements to ANSI Terminals.

Glenn

"Funbeat" <funbeat@netcourrier.com> wrote in message
news:59f69652.0408030025.fc0b3b7@posting.google.com...
> Hello everybody,
>
>
> I'm using a dotmatrix printer (EPSON LX-300+) in order to print line
> by line.
> Starting from the MSDN Article #322091 "HOW TO: Send Raw Data to a
> Printer by Using Visual C# .NET"
> (http://support.microsoft.com/default.aspx?scid=kb;EN-US;322091),
> all is fine till there ;
>
> I'm trying to configure the printer to choose the character set (I'm
> French).
> By means of the printer user guide, I identified the sequence : ESC R
> 1
>
> I'm facing with the following problem :
>
> To send an escape sequence, 2 functions (APIs) are necessary :
> - CreateDC(), to obtain a device context of the printer ;
> - Escape(), to send the sequence using the DC.
>
> I always have the same error with Escape() : "Descripteur non valide"
> / "Invalid Descriptor" and a return value of zero.
> I have tried with other sequence (bold, italic...) with no success.
>
>
> Any ideas ?
>
> Any help will be appreciated.
> Thanks in advance.
>
> Jean-Louis.
>
> Here is the code (some is omitted for clearness) :
> --------------
> .
> .
> .
> [DllImport("gdi32.dll", EntryPoint="Escape", SetLastError=true,
> ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
> public static extern long Escape(long hdc, long nEscape, long nCount,
> string lpInData, object lpOutData);
>
> [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
> internal struct structDevMode
> {
> [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public String
> dmDeviceName;
> [MarshalAs(UnmanagedType.U2)] public short dmSpecVersion;
> [MarshalAs(UnmanagedType.U2)] public short dmDriverVersion;
> [MarshalAs(UnmanagedType.U2)] public short dmSize;
> [MarshalAs(UnmanagedType.U2)] public short dmDriverExtra;
> [MarshalAs(UnmanagedType.U4)] public int dmFields;
> [MarshalAs(UnmanagedType.I2)] public short dmOrientation;
> [MarshalAs(UnmanagedType.I2)] public short dmPaperSize;
> [MarshalAs(UnmanagedType.I2)] public short dmPaperLength;
> [MarshalAs(UnmanagedType.I2)] public short dmPaperWidth;
> [MarshalAs(UnmanagedType.I2)] public short dmScale;
> [MarshalAs(UnmanagedType.I2)] public short dmCopies;
> [MarshalAs(UnmanagedType.I2)] public short dmDefaultSource;
> [MarshalAs(UnmanagedType.I2)] public short dmPrintQuality;
> [MarshalAs(UnmanagedType.I2)] public short dmColor;
> [MarshalAs(UnmanagedType.I2)] public short dmDuplex;
> [MarshalAs(UnmanagedType.I2)] public short dmYResolution;
> [MarshalAs(UnmanagedType.I2)] public short dmTTOption;
> [MarshalAs(UnmanagedType.I2)] public short dmCollate;
> [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public String
> dmFormName;
> [MarshalAs(UnmanagedType.U2)] public short dmLogPixels;
> [MarshalAs(UnmanagedType.U4)] public int dmBitsPerPel;
> [MarshalAs(UnmanagedType.U4)] public int dmPelsWidth;
> [MarshalAs(UnmanagedType.U4)] public int dmPelsHeight;
> [MarshalAs(UnmanagedType.U4)] public int dmNup;
> [MarshalAs(UnmanagedType.U4)] public int dmDisplayFrequency;
> [MarshalAs(UnmanagedType.U4)] public int dmICMMethod;
> [MarshalAs(UnmanagedType.U4)] public int dmICMIntent;
> [MarshalAs(UnmanagedType.U4)] public int dmMediaType;
> [MarshalAs(UnmanagedType.U4)] public int dmDitherType;
> [MarshalAs(UnmanagedType.U4)] public int dmReserved1;
> [MarshalAs(UnmanagedType.U4)] public int dmReserved2;
> }
>
> [DllImport("GDI32.dll", EntryPoint="CreateDC", SetLastError=true,
> CharSet=CharSet.Unicode, ExactSpelling=false,
> CallingConvention=CallingConvention.StdCall)]
> internal static extern long CreateDC([MarshalAs(UnmanagedType.LPTStr)]
> string pDrive,
> [MarshalAs(UnmanagedType.LPTStr)] string pName,
> [MarshalAs(UnmanagedType.LPTStr)] string pOutput,
> ref structDevMode pDevMode);
> .
> .
> .
>
> public static void Init ()
> {
> const long PASSTHROUGH = 19;
> const long NULL = 0;
> long ret;
>
> string escCar = ((char)27).ToString();
> string escString = escCar + "R1";
>
> try
> {
> structDevMode pDevMode = new structDevMode();
> long hdcPrint = CreateDC( "winspool", "EPSON LX", "", ref pDevMode);
> ret = Escape(hdcPrint, PASSTHROUGH, escString.Length, escString,
> NULL);
>
> // ALWAYS true here \/
> if (ret == 0)
> {
> Win32Exception ex = new Win32Exception();
> Console.WriteLine ex.Message;
> }
> }
> catch(Exception _ex)
> {
> Console.WriteLine (_ex);
> }
> }



Relevant Pages

  • Re: % hack in password field
    ... you can use the optional ESCAPE clause to specify the escape character. ... FirstSQL/J Object/Relational DBMS ...
    (comp.lang.java.programmer)
  • Re: Escape character treatment in string library functions
    ... '\r' etc special making it difficult to parse even char by char. ... In C, the escape character only affects string literals, i.e. strings ...
    (comp.programming)
  • Re: shell
    ... I have tried putting btcserver1 string in single quotes and still ... So why aren't you using the CORRECT use of double-quotes to delimit the ... character entity for the space character would ...
    (microsoft.public.outlook.general)
  • Re: Using _ as a character and not as a wildcard
    ... The databases have been prefixed with 'pl_'. ... > I can see in BOL that '_' is seen as a wildcard that can substitute any ... > single character, but how do I tell my query to use '_' as a part of the ... character after the defined escape character as a literal, ...
    (microsoft.public.sqlserver.server)