// 003_异步IO_扩展版API.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <windows.h>
#include <stdio.h>
struct MyOverlapped : public OVERLAPPED {
MyOverlapped()
{
// 将OVERLAPPED部分填充为0
memset(this, 0, sizeof(OVERLAPPED));
pBuff = NULL;
}
char *pBuff;
~MyOverlapped() {
if (pBuff) {
delete pBuff;
}
}
};
VOID WINAPI ioProc(
_In_ DWORD dwErrorCode,/*错误码,*/
_In_ DWORD dwNumberOfBytesTransfered,/*实际完成的字节数*/
_Inout_ LPOVERLAPPED lpOverlapped/*重叠结构体,传入到ReadFile那个*/
)
{
MyOverlapped* pOv = (MyOverlapped*)lpOverlapped;
printf("读取偏移:[%d]的IO任务完成, 实际读取到的字节数:%d"
"读取到的内容是:%s",
pOv->Offset,
pOv->InternalHigh,
pOv->pBuff);
}
int main()
{
HANDLE hFile = INVALID_HANDLE_VALUE;
hFile = CreateFile(
L"003_异步IO_扩展版API.cpp",
GENERIC_READ,/*读写方式:只读*/
FILE_SHARE_READ,/*共享方式: 共享读*/
NULL,/*安全描述符*/
OPEN_EXISTING,/*创建标志:存在时才打开*/
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,/*文件属性和标志:正常属性|重叠IO标志 */
NULL);
MyOverlapped* ov = new MyOverlapped;
ov->Offset = 0;
ov->pBuff = new char[100];
memset(ov->pBuff, 0, 100);
ov->hEvent = NULL;
ReadFileEx(hFile, ov->pBuff, 50, ov, ioProc);
while ( 1 )
{
SleepEx(30, TRUE);
}
}