Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/02/2009, 02:24
Avatar de elangelcaido
elangelcaido
 
Fecha de Ingreso: septiembre-2003
Ubicación: Oviedo
Mensajes: 1.068
Antigüedad: 20 años, 8 meses
Puntos: 4
Función ReadFile de API Windows

Hola,

Resolví hace tiempo cómo utilizar la función CreateFile (click aquí para el que le interese) pero ahora no encuentro la forma de utilizar la función ReadFile de la API de Windows (kernel32.dll).

En mi clase tengo definidos

Código:
class MiClase {   
  
        private String fichero = "fichero.txt";   
        private const short FILE_ATTRIBUTE_NORMAL = 0x80;   
        private const short INVALID_HANDLE_VALUE = -1;   
        private const uint GENERIC_READ = 0x80000000;   
        private const uint GENERIC_WRITE = 0x40000000;   
        private const uint CREATE_NEW = 1;   
        private const uint CREATE_ALWAYS = 2;   
        private const uint OPEN_EXISTING = 3;   
        private const uint FILE_ATTRIBUTE_ARCHIVE = 0x00000020;   
        private const uint FILE_FLAG_NO_BUFFERING = 0x20000000;   
        private const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;   
        private const int FILE_BEGIN = 0;   
        private const int FILE_CURRENT = 1;   
        private const int FILE_END = 2;   
  
        static int m_handleValue;   
//.....   
  
[StructLayout(LayoutKind.Sequential)]   
        private struct OVERLAPPED {   
            public int Internal;   
            public int InternalHigh;   
            public int Offset;   
            public int OffsetHigh;   
            public int hEvent;   
        }   
  
        [DllImport("kernel32.dll", SetLastError = true)]   
        private static extern int CreateFile(string lpFileName,uint dwDesiredAccess,uint dwShareMode,IntPtr lpSecurityAttributes,   
            uint dwCreationDisposition,uint dwFlagsAndAttributes,IntPtr hTemplateFile);   
  
        [DllImport("kernel32.dll")]   
        private static extern bool WriteFile(int hFile, byte[] lpBuffer,   
            int nNumberofBytesToWrite, ref int lpNumberOfBytesWritten,   
            ref OVERLAPPED lpOverlapped);   
  
        [DllImport("kernel32.dll")]   
        private static extern bool ReadFile(int hFile, byte[] lpBuffer, ref OVERLAPPED lpOverlapped);   
  
        [DllImport("kernel32.dll")]   
        private static extern int SetFilePointer(int hFile,   
        int lDistanceToMove, ref int lpDistanceToMoveHigh,   
        int dwMoveMethod);   
  
        [DllImport("kernel32.dll")]   
        private static extern bool CloseHandle(int hFile);  

//........

private int lectura() {   
            OVERLAPPED lpOverlapped = new OVERLAPPED();   
            byte[] lpBuffer = new byte[aEscribir.Length];   
            double bytesLeidos = 0;   
  
            //abrimos el fichero ya existente para lectura   
            int hFile=CreateFile(fichero, GENERIC_READ , 0, IntPtr.Zero, OPEN_EXISTING , 0, IntPtr.Zero);   
  
            ReadFile(hFile, lpBuffer, ref lpOverlapped);   
               
            Console.WriteLine("Bytes leidos: " + lpOverlapped.OffsetHigh);   
               
            CloseHandle(hFile);   
            return 0;   
        }   
}
Cuando intento ejecutar el método "lectura", me salta una excepción en la llamada al método ReadFile.

¿Alguien me puede poner un ejemplo de utilización de la función ReadFile?.

Ya sé que hay otras funciones que son mejores que ReadFile... pero tengo que usar esta.

Gracias por la ayuda.
__________________
Ta Luego! Al final sólo puede quedar uno...
________
lukos.org