Foros del Web » Programación para mayores de 30 ;) » .NET »

Función ReadFile de API Windows

Estas en el tema de Función ReadFile de API Windows en el foro de .NET en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 05/02/2009, 02:24
Avatar de elangelcaido  
Fecha de Ingreso: septiembre-2003
Ubicación: Oviedo
Mensajes: 1.068
Antigüedad: 20 años, 7 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
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:09.