Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

Escape sequence to dotmatrix printer - Escape() API

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Funbeat - 03 Aug 2004 09:25 GMT
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);
    }
}
Glenn Wilson - 03 Aug 2004 11:24 GMT
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

> Hello everybody,
>
[quoted text clipped - 116 lines]
> }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.