• 异步机制


    直接上代码

    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);

             }

     

    }

  • 相关阅读:
    机器学习笔记
    python学习笔记-day8
    python学习笔记-day7
    python学习笔记-day6
    python学习笔记-day5
    python习题
    単語
    bat批处理----copy和xcopy区别
    C#
    VB
  • 原文地址:https://www.cnblogs.com/sysnap/p/4415305.html
Copyright © 2020-2023  润新知