DosProtectRead

From EDM2
Jump to: navigation, search

Reads the specified number of bytes from a file, pipe, or device to a buffer location.

Syntax

DosProtectRead(hFile, pBuffer, cbRead, pcbActual, fhFileHandleLockID)

Parameters

hFile (HFILE) - input 
File handle obtained from DosOpen.
pBuffer (PVOID) - output 
Address of the buffer to receive the bytes read.
cbRead (ULONG) - input 
The length, in bytes, of pBuffer.
This is the number of bytes to be read.
pcbActual (PULONG) - output 
Pointer to the ULONG in which the number of bytes actually read is returned.
fhFileHandleLockID (FHLOCK) - input 
The filehandle lockid obtained from DosProtectOpen.

Return Code

ulrc (APIRET) - returns
DosProtectRead returns one of the following values:
  • 0 NO_ERROR
  • 5 ERROR_ACCESS_DENIED
  • 6 ERROR_INVALID_HANDLE
  • 26 ERROR_NOT_DOS_DISK
  • 33 ERROR_LOCK_VIOLATION
  • 109 ERROR_BROKEN_PIPE
  • 234 ERROR_MORE_DATA

Remarks

The requested number of bytes might not be read. If the value returned in pBuffer is zero, the process tried to read from the end of the file.

A value of zero for cbRead is not considered an error. In such a case, the system treats the request as a null operation.

The file pointer is moved to the desired position by reading data, writing data, or issuing DosProtectSetFilePtr.

If you issue DosProtectOpen with the Direct Open flag set to 1 in the fsOpenMode parameter, you have direct access to an entire disk or diskette volume, independent of the file system. You must lock the logical volume before accessing it, and you must unlock the logical volume when you are finished accessing it. Issue DosDevIOCtl for Category 8, DSK_LOCKDRIVE to lock the logical volume, and for Category 8, DSK_UNLOCKDRIVE to unlock the logical volume. While the logical volume is locked, no other process can access it.

Named-Pipe Considerations

A named pipe is read as one of the following:

  • A byte pipe in byte-read mode
  • A message pipe in message-read mode
  • A message pipe in byte-read mode.

A byte pipe must be in byte-read mode to be read; an error is returned if it is in message-read mode. All currently available data, up to the size requested, is returned.

A message pipe can be read in either message-read mode or byte-read mode. When the message pipe is in message-read mode, a read operation that is larger than the next available message returns only that message. pcbActual is set to indicate the size of the message returned.

A read operation that is smaller than the next available message returns with the number of bytes requested and an ERROR_MORE_DATA return code. When the reading of a message is resumed after ERROR_MORE_DATA is returned, a read operation always blocks until the next piece (or the rest) of the message can be transferred. DosPeekNPipe can be issued to determine how many bytes are left in the message.

A message pipe in byte-read mode is read as if it were a byte stream, and DosProtectRead skips over message headers. This is like reading a byte pipe in byte-read mode.

When blocking mode is set for a named pipe, a read operation blocks until data is available. In this case, the read operation never returns with pcbActual equal to zero, except at the end of the file. When the mode is set to message-read, messages are always read in their entirety, except when the message is bigger than the size of the read operation.

pcbActual can equal zero in nonblocking mode, but only when no data is available at the time of the read operation.

Example Code

This example opens or creates and opens a file named "DOSPROT.DAT". It writes a string to the file, positions the file pointer back to BOF, reads the string written, and finally closes it using DosProtect functions.

#define INCL_DOSFILEMGR          /* File Manager values */
#define INCL_DOSERRORS           /* DOS Error values    */
#include <os2.h>
#include <stdio.h>
#include <string.h>

int main(VOID) {
   HFILE  hfFileHandle   = 0L;
   ULONG  ulAction       = 0;
   ULONG  ulBytesRead    = 0;
   ULONG  ulWrote        = 0;
   ULONG  ulLocal        = 0;
   UCHAR  uchFileName[20]  = "dosprot.dat",
          uchFileData[100] = " ";
   FHLOCK FileHandleLock = 0;        /* File handle lock   */
   APIRET rc             = NO_ERROR; /* Return code */

   /* Open the file test.dat.  Make it read/write, open it */
   /* if it already exists and create it if it is new.     */
   rc = DosProtectOpen(uchFileName,             /* File path name          */
                &hfFileHandle,                  /* File handle             */
                &ulAction,                      /* Action taken            */
                100L,                           /* File primary allocation */
                FILE_ARCHIVED | FILE_NORMAL,    /* File attribute          */
                OPEN_ACTION_CREATE_IF_NEW |
                OPEN_ACTION_OPEN_IF_EXISTS,     /* Open function type      */
                OPEN_FLAGS_NOINHERIT |
                OPEN_SHARE_DENYNONE  |
                OPEN_ACCESS_READWRITE,          /* Open mode of the file   */
                0L,                             /* No extended attribute   */
                &FileHandleLock);               /* File handle lock id     */
   if (rc != NO_ERROR) {
      printf("DosProtectOpen error: return code = %u\n", rc);
      return 1;
   } else {
     printf ("DosProtectOpen: Action taken = %u\n", ulAction);
   } /* endif */

   /* Write a string to the file */
   strcpy (uchFileData, "testing...\n3...\n2...\n1\n");
   rc = DosProtectWrite (hfFileHandle,       /* File handle                  */
                  (PVOID) uchFileData,       /* String to be written         */
                  sizeof (uchFileData),      /* Size of string to be written */
                  &ulWrote,                  /* Bytes actually written       */
                  FileHandleLock);           /* File handle lock id          */
   if (rc != NO_ERROR) {
      printf("DosProtectWrite error: return code = %u\n", rc);
      return 1;
   } else {
      printf ("DosProtectWrite: Bytes written = %u\n", ulWrote);
   } /* endif */

   /* Move the file pointer back to the beginning of the file */
   rc = DosProtectSetFilePtr (hfFileHandle,    /* File Handle          */
                       0L,                     /* Offset               */
                       FILE_BEGIN,             /* Move from BOF        */
                       &ulLocal,               /* New location address */
                       FileHandleLock);        /* File handle lock id  */
   if (rc != NO_ERROR) {
      printf("DosSetFilePtr error: return code = %u\n", rc);
      return 1;
   }

   /* Read the first 100 bytes of the file */
   rc = DosProtectRead (hfFileHandle,         /* File Handle                 */
                 uchFileData,                 /* String to be read           */
                 100L,                        /* Length of string to be read */
                 &ulBytesRead,                /* Bytes actually read         */
                 FileHandleLock);             /* File handle lock id         */
   if (rc != NO_ERROR) {
      printf("DosProtectRead error: return code = %u\n", rc);
      return 1;
   } else {
      printf("DosProtectRead: Bytes read = %u\n%s\n", ulBytesRead, uchFileData);
   } /* endif */

   rc = DosProtectClose(hfFileHandle, FileHandleLock);   /* Close the file */
   if (rc != NO_ERROR) {
      printf("DosProtectClose error: return code = %u\n", rc);
      return 1;
    }
   return NO_ERROR;
}

Related Functions