CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Mar 2009
    Posts
    20

    Question urgent ----exception problem anyone solution

    hey any one tell me the solution to the exception generated ..............in lpt.write line ...




    Code:
    using System.IO;
    using Microsoft.Win32.SafeHandles;
    
    using System;
    using System.Threading;
    using System;
    using System.Runtime.InteropServices;
    public class PrintFactory
    {
        public const uint FILE_SHARE_READ = 0x00000001;
        public const uint FILE_SHARE_WRITE = 0x00000002;
        public const uint FILE_SHARE_DELETE = 0x00000004;
        public const uint FILE_FLAG_WRITE_THROUGH = 0x80000000;
        public const uint FILE_FLAG_OVERLAPPED = 0x40000000;
        public const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
        public const uint FILE_FLAG_RANDOM_ACCESS = 0x10000000;
        public const uint FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000;
        public const uint FILE_ATTRIBUTE_NORMAL = 0x80;
        public const short INVALID_HANDLE_VALUE = -1;
        public const uint GENERIC_READ = 0x80000000;
        public const uint GENERIC_WRITE = 0x40000000;
        public const uint CREATE_NEW = 1;
        public const uint CREATE_ALWAYS = 2;
        public const uint OPEN_EXISTING = 3;
    
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
            uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
            uint dwFlagsAndAttributes, IntPtr hTemplateFile);
        
        [DllImport("msvcrt")]
        static extern int _getch();
        
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern unsafe int WriteFile(IntPtr handle, IntPtr buffer,
          int numBytesToWrite, IntPtr numBytesWritten, NativeOverlapped* lpOverlapped);
        
        public enum EMoveMethod : uint
        {
            Begin = 0,
            Current = 1,
            End = 2
        }
      //  [DllImport("coredll")]
        //static extern Boolean WriteFile(IntPtr fFile, Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite,
          //      out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);
        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern unsafe uint SetFilePointer(
            [In] SafeFileHandle hFile,
            [In] int lDistanceToMove,
            [Out] int* lpDistanceToMoveHigh,
            [In] EMoveMethod dwMoveMethod);
    
        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern uint SetFilePointer(
            [In] SafeFileHandle hFile,
            [In] int lDistanceToMove,
            [Out] out int lpDistanceToMoveHigh,
            [In] EMoveMethod dwMoveMethod);
        [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Ansi)]
        static extern bool WriteFile(IntPtr hFile, System.Text.StringBuilder lpBuffer,
         uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten,
         [In] ref System.Threading.NativeOverlapped lpOverlapped);
    
        [DllImport("kernel32.dll")]
        static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer,
           uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten,
           [In] ref System.Threading.NativeOverlapped lpOverlapped);
    
    
    
       
        public PrintFactory()
        {
            IntPtr ptr = CreateFile("\\\\.\\N:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, IntPtr.Zero);
            //IntPtr ptr = CreateFile("\\\\.\\N:", GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
    
            
            
            
            
            
            
            /* Is bad handle? INVALID_HANDLE_VALUE */
            if (ptr.ToInt32() == -1)
            {
                /* ask the framework to marshall the win32 error code to an exception */
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
            else
            {
                String Temp = "This is a test print";
             Byte[] Buff = new System.Byte[512];
                //Check to see if your printer support ASCII encoding or Unicode.
                FileStream lpt = new FileStream(ptr, FileAccess.ReadWrite); 
                //If unicode is supported, use the following:
              //  Buff = System.Text.Encoding.Unicode.GetBytes(Temp);
               // Buff = System.Text.Encoding.ASCII.GetBytes(Temp);
                uint outt; IntPtr p=(IntPtr)0;
                //System.Threading.NativeOverlapped b=0;
                //WriteFile(ptr,Buff,512,out outt,ref b);
                
        //        Buff = System.Text.Encoding.ASCII.GetBytes(prnCmdCut);
               /*
    
                lpt.Read(Buff, 0, Buff.Length);
                for (int i = 0; i < 512; i++)
                {
                    try
                    {
                        if (i &#37; 16 == 0)
                            Console.WriteLine();
                        Console.Write(String.Format("{0:X}", (int)Buff[i])+" ");
                       
                    }
                    catch (Exception e) { }
                }*/
                lpt.WriteByte(71);    --------------->this line generates exception
                lpt.Close();
               // Console.Write(Temp);
                _getch();
            }
        }
    }
    class enter
    {
    
    
    
         public static void Main()
        {
            new PrintFactory();
      
            
        }
    }

    exception

    IO operation will not work. Most likely the file will become too long or the handle was not opened to support synchronous IO operations.
    Last edited by vikrant3mahajan; March 9th, 2009 at 01:10 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured