1. // 003_异步IO_扩展版API.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    2. //
    3. #include <iostream>
    4. #include <windows.h>
    5. #include <stdio.h>
    6. struct MyOverlapped : public OVERLAPPED {
    7. MyOverlapped()
    8. {
    9. // 将OVERLAPPED部分填充为0
    10. memset(this, 0, sizeof(OVERLAPPED));
    11. pBuff = NULL;
    12. }
    13. char *pBuff;
    14. ~MyOverlapped() {
    15. if (pBuff) {
    16. delete pBuff;
    17. }
    18. }
    19. };
    20. VOID WINAPI ioProc(
    21. _In_ DWORD dwErrorCode,/*错误码,*/
    22. _In_ DWORD dwNumberOfBytesTransfered,/*实际完成的字节数*/
    23. _Inout_ LPOVERLAPPED lpOverlapped/*重叠结构体,传入到ReadFile那个*/
    24. )
    25. {
    26. MyOverlapped* pOv = (MyOverlapped*)lpOverlapped;
    27. printf("读取偏移:[%d]的IO任务完成, 实际读取到的字节数:%d"
    28. "读取到的内容是:%s",
    29. pOv->Offset,
    30. pOv->InternalHigh,
    31. pOv->pBuff);
    32. }
    33. int main()
    34. {
    35. HANDLE hFile = INVALID_HANDLE_VALUE;
    36. hFile = CreateFile(
    37. L"003_异步IO_扩展版API.cpp",
    38. GENERIC_READ,/*读写方式:只读*/
    39. FILE_SHARE_READ,/*共享方式: 共享读*/
    40. NULL,/*安全描述符*/
    41. OPEN_EXISTING,/*创建标志:存在时才打开*/
    42. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,/*文件属性和标志:正常属性|重叠IO标志 */
    43. NULL);
    44. MyOverlapped* ov = new MyOverlapped;
    45. ov->Offset = 0;
    46. ov->pBuff = new char[100];
    47. memset(ov->pBuff, 0, 100);
    48. ov->hEvent = NULL;
    49. ReadFileEx(hFile, ov->pBuff, 50, ov, ioProc);
    50. while ( 1 )
    51. {
    52. SleepEx(30, TRUE);
    53. }
    54. }