Escape sequence to dotmatrix printer - Escape() API

F

Funbeat

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);
}
}
 
G

Glenn Wilson

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 said:
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);
}
}
 

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