Purpose
DosOpen opens a physical or logical disk and returns a handle to be used to perform operations upon the disk specified.
Syntax
#define INCL DOSFILEMGR #include os2.h
APIRET DosOpen
Parameters
pszFileName PSZ) input
If DosOpen fails, this value has no meaning. Otherwise, the raw file system should always reutn FILE_EXISTED (1).
OPEN_SHARE_DENYNONE - Allows other processes to read/write from disk.
OPEN_SHARE_DENYREADWRITE - Locks disk from access by other processes and file systems.
Invalid flags are OPEN_ACCESS_WRITEONLY, OPEN_ACCESS_READONLY, OPEN_SHARE_DENYREAD, OPEN_SHARE_DENYWRITE, and OPEN_FLAGS_DASD.
Valid but unimplemented flags are OPEN_FLAGS_NOINHERIT, OPEN_FLAGS_RANDOMSEQUENTIAL, OPEN_FLAGS_RANDOM, OPEN_FLAGS_SEQUENTIAL, OPEN_FLAGS_NO_LOCALITY, OPEN_FLAGS_NO_CACHE, OPEN_FLAGS_FAIL_ON_ERROR, and OPEN_FLAGS_WRITE_THROUGH.
Returns
ulrc APIRET) returns
DosOpen returns one of the following values
12
Remarks
A successful DosOpen request returns a handle for accessing the disk. The read/write pointer is set at the first byte of the disk. The position of the pointer can be changed with DosSetFilePtr or by read and write operations on the disk.
The direct open bit (OPEN_FLAGS_DASD) is not used with the raw file system. However, when using the raw file system to access logical partitions and disk locking is required, the following logic should be used. First, the application should lock the disk by passing the handle to DosDevIOCtl, Category 8, DSK_LOCKDRIVE. Second, the application should perform the desired operations on the disk. Lastly, the application should unlock the disk using DosDevIOCtl Category 8, DSK_UNLOCKDRIVE.
If locking is desired when using the raw file system on physical disk, the OPEN_SHARE_DENYREADWRITE flag should be used. The disk will automatically be unlocked when the disk is closed with DosClose.
Related Functions
Example Code
The following is NOT a complete usable program. It is simply intended to provide an idea of how to use Raw I/O File System APIs (e.g. DosOpen, DosRead, DosWrite, DosSetFilePtr, and DosClose).
This example opens physical disk #1 for reading and physical disk #2 for writing. DosSetFilePtr is used to set the pointer to the beginning of the disks. Using DosRead and DosWrite, 10 megabytes of data is transferred from disk #1 to disk #2. Finally, DosClosed is issued to close the disk handles.
It is assumed that the size of each of the two disks is at least 10 megabytes.
#define INCL_DOSFILEMGR /* Include File Manager APIs */#define INCL_DOSMEMMGR /* Includes Memory Management APIs */ #define INCL_DOSERRORS /* DOS Error values */ #include os2.h> #include stdio.h> #include string.h> #define SIXTY_FOUR_K 0x10000 #define ONE_MEG 0x100000 #define TEN_MEG 10*ONE_MEG #define UNC_DISK1 "\\\\.\\Physical_Disk1" #define UNC_DISK2 "\\\\.\\Physical_Disk2" int main(void) { HFILE hfDisk1 = 0; /* Handle for disk #1 */ HFILE hfDisk2 = 0; /* Handle for disk #2 */ ULONG ulAction = 0; /* Action taken by DosOpen */ ULONG cbRead = 0; /* Bytes to read */ ULONG cbActualRead = 0; /* Bytes read by DosRead */ ULONG cbWrite = 0; /* Bytes to write */ ULONG ulLocation = 0; ULONG cbActualWrote = 0; /* Bytes written by DosWrite */ UCHAR uchFileName1[20] = UNC_DISK1, /* UNC Name of disk 1 */ uchFileName2[20] = UNC_DISK2; /* UNC Name of disk 2 */ PBYTE pBuffer = 0; ULONG cbTotal = 0; APIRET rc = NO_ERROR; /* Return code */ /* Open a raw file system disk #1 for reading */ rc = DosOpen(uchFileName1, /* File name */ hfDisk1, /* File handle */ ulAction, /* Action taken by DosOpen */ 0L, /* no file size */ FILE_NORMAL, /* File attribute */ OPEN_ACTION_OPEN_IF_EXISTS, /* Open existing disk */ OPEN_SHARE_DENYNONE | /* Access mode */ OPEN_ACCESS_READONLY, 0L); /* No extented attributes */ if (rc != NO_ERROR) { printf("DosOpen error rc = %u\n", rc); return(1); } /* endif */ /* Set the pointer to the begining of the disk */ rc = DosSetFilePtr(hfDisk1, /* Handle for disk 1 */ 0L, /* Offset must be multiple of 512 */ FILE_BEGIN, /* Begin of the disk */ ulLocation); /* New pointer location */ if (rc != NO_ERROR) { printf("DosSetFilePtr error rc = %u\n", rc); return(1); } /* endif */ /* Open a raw file system disk #2 for writing */ rc = DosOpen(uchFileName2, /* File name */ hfDisk2, /* File handle */ ulAction, /* Action taken by DosOpen */ 0L, /* no file size */ FILE_NORMAL, /* File attribute */ OPEN_ACTION_OPEN_IF_EXISTS, /* Open existing disk */ OPEN_SHARE_DENYNONE | /* Access mode */ OPEN_ACCESS_READWRITE, 0L); /* No extented attributes */ if (rc != NO_ERROR) { printf("DosOpen error rc = %u\n", rc); return(1); } /* endif */ /* Set the pointer to the begining of the disk */ rc = DosSetFilePtr(hfDisk2, /* Handle for disk 1 */ 0L, /* Offset must be multiple of 512 */ FILE_BEGIN, /* Begin of the disk */ ulLocation); /* New pointer location */ if (rc != NO_ERROR) { printf("DosSetFilePtr error rc = %u\n", rc); return(1); } /* endif */ /* Allocate 64K of memory for transfer operations */ rc = DosAllocMem((PPVOID) pBuffer, /* Pointer to buffer */ SIXTY_FOUR_K, /* Buffer size */ PAG_COMMIT | /* Allocation flags */ PAG_READ | PAG_WRITE); if (rc != NO_ERROR) { printf("DosAllocMem error rc = %u\n", rc); return(1); } /* endif */ cbRead = SIXTY_FOUR_K; while (rc == NO_ERROR cbTotal TEN_MEG) { /* Read from #1 */ rc = DosRead(hfDisk1, /* Handle for disk 1 */ pBuffer, /* Pointer to buffer */ cbRead, /* Size must be multiple of 512 */ cbActualRead); /* Actual read by DosOpen */ if (rc) { printf("DosRead error return code = %u\n", rc); return 1; } /* Write to disk #2 */ cbWrite = cbActualRead; rc = DosWrite(hfDisk2, /* Handle for disk 2 */ pBuffer, /* Pointer to buffer */ cbWrite, /* Size must be multiple of 512 */ cbActualWrote); /* Actual written by DosOpen */ if (rc) { printf("DosWrite error return code = %u\n", rc); return 1; } if (cbActualRead != cbActualWrote) { printf("Bytes read (%u) does not equal bytes written (%u)\n", cbActualRead, cbActualWrote); return 1; } cbTotal += cbActualRead; /* Update total transferred */ } printf("Transfer successfully %d bytes from disk #1 to disk #2.\n", cbTotal); /* Free allocated memmory */ rc = DosFreeMem(pBuffer); if (rc != NO_ERROR) { printf("DosFreeMem error return code = %u\n", rc); return 1; } rc = DosClose(hfDisk1); if (rc != NO_ERROR) { printf("DosClose error return code = %u\n", rc); return 1; } rc = DosClose(hfDisk2); if (rc != NO_ERROR) { printf("DosClose error return code = %u\n", rc); return 1; return NO_ERROR; }