zoukankan      html  css  js  c++  java
  • 异步机制

    直接上代码

    VOID CALLBACK test_io_completion_routine(

      DWORD dwErrorCode,

      DWORD dwNumberOfBytesTransfered,

      LPOVERLAPPED lpOverlapped

    )

    {

             printf("dwErrorCode %d, dwNumberOfBytesTransfered %d ", dwErrorCode, dwNumberOfBytesTransfered);

             printf("%s ", (char*)(lpOverlapped + 1));

     

             free(lpOverlapped);

    }

     

    BOOL

    queue_read_req(

        HANDLE hFile,

             DWORD nNumberOfBytesToRead,

             DWORD Offset)

    {

             BOOL bResult = FALSE;

             OVERLAPPED* olv = (OVERLAPPED* )malloc(sizeof(OVERLAPPED) + nNumberOfBytesToRead);

             if(olv)

             {

                       DWORD nBytesRead = 0;

                       ZeroMemory(olv, sizeof(OVERLAPPED) + nNumberOfBytesToRead);

     

                       olv->Offset = Offset;

                       bResult = ReadFile(hFile, (void*)(olv + 1), nNumberOfBytesToRead, &nBytesRead, olv) ;

                       if (!bResult)

                       {

                                DWORD dwError = 0;

                                switch (dwError = GetLastError())

                                {

                                case ERROR_HANDLE_EOF:

                                         free(olv);

                                         break;

                                case ERROR_IO_PENDING:

                                         break;

                                default:

                                         break;

     

                                }

                       }

     

             }

     

             return bResult;

    }

     

    void test_io_port()

    {

             LPCWSTR FileTest = L"C:\NTDLL.txt";

             HANDLE hFile = CreateFileW(FileTest,               // file to open

                       GENERIC_READ,          // open for reading

                       FILE_SHARE_READ,       // share for reading

                       NULL,                  // default security

                       OPEN_EXISTING,         // existing file only

                       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file

                       NULL); // no attr. template

             if(hFile != INVALID_HANDLE_VALUE)

             {

                       BindIoCompletionCallback(hFile, test_io_completion_routine, 0);

     

                       for(int i = 0; i < 5; i++)

                       {

                                queue_read_req(hFile, 1024, i*1024 );

                       }

     

                       Sleep(5000);

     

                       CloseHandle(hFile);

             }

     

    }

  • 相关阅读:
    创业团队的狼性管理:慈不掌兵 义不聚财
    爱德华·斯诺登
    塞班岛
    大兴雷克萨斯深度剖析2013款LS460L_深圳大兴雷克萨斯_太平洋汽车网
    金字塔底端_百度百科
    左立_百度百科
    志大才疏_百度百科
    JS~json日期格式化
    MVVM架构~knockoutjs系列之从Knockout.Validation.js源码中学习它的用法
    JS~重写alter与confirm,让它们变成fancybox风格
  • 原文地址:https://www.cnblogs.com/sysnap/p/4415305.html
Copyright © 2011-2022 走看看