先来两个msdn中的例子吧,我暂时没时间翻译(忙啊,原谅一下吧,或者等我有时间[
][
])。
win9x的翻译可能比较困难,用vwin32的应该比较容易,我想好了再写吧。
//winNT
#include <windows.h>
#include <winioctl.h> // From the Win32 SDK /Mstools/Include
#include "ntddcdrm.h" // From the Windows NT DDK /Ddk/Src/Storage/Inc
/*
This code reads sectors 16 and 17 from a compact disc and writes
the contents to a disk file named Sector.dat
*/
{
HANDLE hCD, hFile;
DWORD dwNotUsed;
// Disk file that will hold the CD-ROM sector data.
hFile = CreateFile ("sector.dat",
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
// For the purposes of this sample, drive F: is the CD-ROM
// drive.
hCD = CreateFile ("////.//F:", GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
// If the CD-ROM drive was successfully opened, read sectors 16
// and 17 from it and write their contents out to a disk file.
if (hCD != INVALID_HANDLE_VALUE)
{
DISK_GEOMETRY dgCDROM;
PREVENT_MEDIA_REMOVAL pmrLockCDROM;
// Lock the compact disc in the CD-ROM drive to prevent accidental
// removal while reading from it.
pmrLockCDROM.PreventMediaRemoval = TRUE;
DeviceIoControl (hCD, IOCTL_CDROM_MEDIA_REMOVAL,
&pmrLockCDROM, sizeof(pmrLockCDROM), NULL,
0, &dwNotUsed, NULL);
// Get sector size of compact disc
if (DeviceIoControl (hCD, IOCTL_CDROM_GET_DRIVE_GEOMETRY,
NULL, 0, &dgCDROM, sizeof(dgCDROM),
&dwNotUsed, NULL))
{
LPBYTE lpSector;
DWORD dwSize = 2 * dgCDROM.BytesPerSector; // 2 sectors
// Allocate buffer to hold sectors from compact disc. Note that
// the buffer will be allocated on a sector boundary because the
// allocation granularity is larger than the size of a sector on a
// compact disk.
lpSector = VirtualAlloc (NULL, dwSize,
MEM_COMMIT|MEM_RESERVE,
PAGE_READWRITE);
// Move to 16th sector for something interesting to read.
SetFilePointer (hCD, dgCDROM.BytesPerSector * 16,
NULL, FILE_BEGIN);
// Read sectors from the compact disc and write them to a file.
if (ReadFile (hCD, lpSector, dwSize, &dwNotUsed, NULL))
WriteFile (hFile, lpSector, dwSize, &dwNotUsed, NULL);
VirtualFree (lpSector, 0, MEM_RELEASE);
}
// Unlock the disc in the CD-ROM drive.
pmrLockCDROM.PreventMediaRemoval = FALSE;
DeviceIoControl (hCD, IOCTL_CDROM_MEDIA_REMOVAL,
&pmrLockCDROM, sizeof(pmrLockCDROM), NULL,
0, &dwNotUsed, NULL);
CloseHandle (hCD);
CloseHandle (hFile);
}
}
//------------------------------------------------------------------------------
//win9x
// Code inside the Win32 DLL (the 32-bit side of the thunk)
// ReadSectorsFromCD() is exported by the DLL so that it can be called
// by Win32-based applications.
#include <windows.h>
#define CD_SECTOR_SIZE 2048
__declspec(dllexport)
BOOL WINAPI ReadSectorsFromCD (BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpBuff);
// Prototype for thunk function in 16-bit DLL.
BOOL FAR PASCAL ReadCDRomSectors (BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpBuffer);
/*-------------------------------------------------------------------
ReadSectorsFromCD()
Calls the thunked function, ReadCDRomSectors(). This function is
exported by the Win32 DLL and thus is callable from Win32-based
applications.
Parameters:
bDrive
Drive letter of CD-ROM drive to read from. Specified as
a character in the range 'A', 'B', 'C', ..., 'Z'. May be
upper- or lower-case.
dwStartSector
First sector to read.
wSectors
Number of sectors to read.
lpBuffer
Buffer to contain sector data. Must be large enough to
accommodate all sectors being read. Because this buffer is
provided to the 16-bit DLL via a thunk, its maximum
length is limited to 64K.
Example use:
// Read 5 sectors from CD-ROM drive E: starting at sector 16.
fResult = ReadSectorsFromCD ('E', 16, 5, lpBuff);
Return Value
Returns TRUE if successful, or FALSE if an error occurred
in reading or if a parameter was invalid.
-------------------------------------------------------------------*/
__declspec(dllexport)
BOOL WINAPI ReadSectorsFromCD (BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpBuff)
{
// Call 16-bit DLL to read sectors via a 32->16 thunk
return ReadCDRomSectors (bDrive, dwStartSector, wSectors, lpBuff);
}
//--------------------------------------------------------------------
// Contents of the thunk script
enablemapdirect3216 = true;
typedef unsigned short WORD; typedef unsigned long DWORD; typedef unsigned char BYTE, *LPBYTE;
typedef bool BOOL;
BOOL ReadCDRomSectors (BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpBuffer)
{
lpBuffer = inout;
}
//--------------------------------------------------------------------
// Code inside the 16-bit DLL. This code implements the 16-bit side of
// the thunk, and is where the calls to MSCDEX are made to read
// sectors from a compact disc.
#include <windows.h>
#include <string.h>
#include <ctype.h>
// Cooked-mode sector size in bytes
#define CD_SECTOR_SIZE 2048
// Maximum sector buffer size in bytes
#define MAX_BUFFER_LENGTH 65536
// Maximum number of sectors for each read request
#define MAX_CD_SECTORS_TO_READ ((MAX_BUFFER_LENGTH) / /
(CD_SECTOR_SIZE))
// Processor flags masks -- use for testing wFlags member of RMCS
#define CARRY_FLAG 0x0001
// Real-mode call structure for making DPMI Simulate Real Mode
// Interrupt calls.
typedef struct tagRMCS {
DWORD edi, esi, ebp, RESERVED, ebx, edx, ecx, eax;
WORD wFlags, es, ds, fs, gs, ip, cs, sp, ss;
} RMCS, FAR* LPRMCS;
BOOL FAR PASCAL __export ReadCDRomSectors (BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpBuffer);
BOOL FAR PASCAL MSCDEX_ReadSector (BYTE bDrive,
DWORD dwStartSector,
LPBYTE RMlpBuffer);
BOOL FAR PASCAL SimulateRM_Int (BYTE bIntNum, LPRMCS lpCallStruct);
void FAR PASCAL BuildRMCS (LPRMCS lpCallStruct);
/*-------------------------------------------------------------------
ReadCDRomSectors()
Reads a specified number of sectors from a CD-ROM.
Parameters:
bDrive
Drive letter of CD-ROM drive to read from. Specified as
a character in the range 'A', 'B', 'C', ..., 'Z'. May be
upper- or lower-case.
dwStartSector
First sector to read.
wSectors
Number of sectors to read.
lpBuffer
Buffer to contain sector data. Must be large enough to
accommodate all sectors being read. Since this buffer is
provided by the calling Win32 application, no more than
the first 64K bytes will be used.
Return Value
Returns TRUE if successful, or FALSE if an error occurred
in reading or if a parameter was invalid.
-------------------------------------------------------------------*/
BOOL FAR PASCAL __export ReadCDRomSectors (BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpBuffer)
{
BOOL fResult;
DWORD cbOffset;
DWORD i;
DWORD gdaBuffer; // Return value of GlobalDosAlloc().
LPBYTE RMlpBuffer; // Real-mode buffer pointer
LPBYTE PMlpBuffer; // Protected-mode buffer pointer
// Convert drive letter into drive number for MSCDEX call.
bDrive = toupper(bDrive) - 'A';
/*
Validate parameters:
bDrive must be between 0 and 25, inclusive.
lpBuffer must not be NULL.
wSectors must be between 1 and the maximum number of
sectors that can fit into a 64K buffer, inclusive.
*/
if (bDrive > 25 || !lpBuffer)
return FALSE;
if (!wSectors || (wSectors > MAX_CD_SECTORS_TO_READ))
return FALSE;
/*
Allocate buffer for MSCDEX call. This buffer must be below 1MB
because the MSCDEX function will be called using DPMI. Like real-
mode MSCDEX.EXE, CDFS implements the MSCDEX API as V86-mode
Interrupt 2Fh functions.
Free memory below 1MB is relatively scarce, so allocating a
small sector buffer increases the chances that it can be
allocated no matter how many other applications and MS-DOS
device drivers are running. Also, a small sector buffer leaves
more memory for other applications to use.
*/
gdaBuffer = GlobalDosAlloc (CD_SECTOR_SIZE);
if (!gdaBuffer)
return FALSE;
RMlpBuffer = (LPBYTE)MAKELONG(0, HIWORD(gdaBuffer));
PMlpBuffer = (LPBYTE)MAKELONG(0, LOWORD(gdaBuffer));
// Call MSCDEX to read each sector.
for (i = cbOffset = 0;
i < wSectors;
i++, cbOffset += CD_SECTOR_SIZE)
{
if (fResult = MSCDEX_ReadSector (bDrive,
dwStartSector + i,
RMlpBuffer))
_fmemcpy (lpBuffer + cbOffset, PMlpBuffer, CD_SECTOR_SIZE);
else
break;
}
GlobalDosFree (LOWORD(gdaBuffer));
return (fResult);
}
/*-------------------------------------------------------------------
MSCDEX_ReadSector()
Calls MSCDEX to read a single sector from a CD-ROM compact disc.
Parameters:
bDrive
Drive number of CD-ROM drive to read from. Expected to be
a number in the following series: 0 = A, 1 = B, 2 = C, etc.
dwStartSector
First sector of read.
RMlpBuffer
Real-mode segment
ffset pointer to a buffer that will receive
sector data. Must be large enough to accommodate a single
sector in cooked mode.
Return Value
Returns TRUE if successful, or FALSE if an error occurred in
reading.
-------------------------------------------------------------------*/
BOOL FAR PASCAL MSCDEX_ReadSector (BYTE bDrive,
DWORD dwStartSector,
LPBYTE RMlpBuffer)
{
RMCS callStruct;
BOOL fResult;
/*
Prepare DPMI Simulate Real Mode Interrupt call structure with
the register values used to make the MSCDEX Absolute read call.
Then, call MSCDEX using DPMI and check for errors in both the DPMI
call and the MSCDEX call.
*/
BuildRMCS (&callStruct);
callStruct.eax = 0x1508; // MSCDEX Absolute read
callStruct.ebx = LOWORD(RMlpBuffer); // Offset of sect buffer
callStruct.es = HIWORD(RMlpBuffer); // Segment of sect buffer
callStruct.ecx = bDrive; // 0=A, 1=B, 2=C, etc.
callStruct.edx = 1; // Read one sector
callStruct.esi = HIWORD(dwStartSector);
callStruct.edi = LOWORD(dwStartSector);
if (fResult = SimulateRM_Int (0x2F, &callStruct))
fResult = !(callStruct.wFlags & CARRY_FLAG);
return fResult;
}
/*-------------------------------------------------------------------
SimulateRM_Int()
Allows protected-mode software to execute real-mode interrupts
such as calls to MS-DOS, MS-DOS TSRs, MS-DOS device drivers.
This function implements the "Simulate Real Mode Interrupt"
function of the DPMI specification v0.9 and later.
Parameters:
bIntNum
Number of the interrupt to simulate.
lpCallStruct
Call structure that contains params (register values)
for bIntNum.
Return Value
SimulateRM_Int returns TRUE if it succeeded or FALSE if
it failed.
Comments
lpCallStruct is a protected-mode selector
ffset address, not
a real-mode segment
ffset address.
-------------------------------------------------------------------*/
BOOL FAR PASCAL SimulateRM_Int (BYTE bIntNum, LPRMCS lpCallStruct) {
BOOL fRetVal = FALSE; // Assume failure
_asm {
push di
mov ax, 0300h ; DPMI Simulate Real Mode Interrupt
mov bl, bIntNum ; Number of the interrupt to simulate
mov bh, 01h ; Bit 0 = 1; all other bits must be 0
xor cx, cx ; No words to copy from PM to RM stack
les di, lpCallStruct ; Real mode call structure
int 31h ; Call DPMI
jc END1 ; CF set if error occurred
mov fRetVal, TRUE
END1:
pop di
}
return (fRetVal);
}
/*-------------------------------------------------------------------
BuildRMCS()
Initializes a real-mode call structure by zeroing all its members.
Parameters:
lpCallStruct
Points to a real-mode call structure
Return Value
None.
Comments
lpCallStruct is a protected-mode selector
ffset address,
not a real-mode segment
ffset address.
-------------------------------------------------------------------*/
void FAR PASCAL BuildRMCS (LPRMCS lpCallStruct)
{
_fmemset (lpCallStruct, 0, sizeof(RMCS));
}