CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  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.

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: urgent ----exception problem anyone solution

    Can you marshal the last Win32 error code?

    Code:
    int code = Marshal.GetLastWin32Error();
    Then see if you can find the error explanation on this page:

    http://help.netop.com/support/errorc...rror_codes.htm
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  3. #3
    Join Date
    Mar 2009
    Posts
    20

    Unhappy Re: urgent ----exception problem anyone solution

    87 The parameter is incorrect. ERROR_INVALID_PARAMETER


    this is the msg ....still i m nt able to figure out

  4. #4
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: urgent ----exception problem anyone solution

    When you call Read() on the FileStream object, it may be moving the stream position. Try calling

    Code:
    lpt.Seek(0, SeekOrigin.Begin);
    and then writing the byte to the stream.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: urgent ----exception problem anyone solution

    Does FileStream stream = File.OpenWrite ("\\\\.\\N:"); work?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Mar 2009
    Posts
    20

    Re: urgent ----exception problem anyone solution

    file stram does not support reading win32 devices so we cant use the way we have to use createf ile and the seeeking function still not giving any solution.....does any one knows how to p/invoke writeFile function and use it ....it works in vc++ but i want to use it in c#....

  7. #7
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: urgent ----exception problem anyone solution

    You should just be able to make external native functions in C# for the ReadFile() and WriteFile() Win32 functions contained in the kernell32.dll. I dont see why they wouldnt work unless something was wrong with the device. Are you getting a handle? Instead of using IntPtr, try using SafeFileHandle, it has an IsInvalid and IsClosed property which will tell you if the handle is invalid.

    I know that simply calling CreateFile() and returning an IntPtr and then checking the IntPtr as a 32 bit integer does not work correctly in some cases. Change you code to use a SafeFileHandle if you can. Other than that Im not sure what to tell you. I opened a file and read the file and wrote to the file by interop'ing the ReadFile() and WriteFile() today and it worked just fine with SafeFileHandle so give that a shot.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

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