/运行结果还有问题就是中文显示不出来,都是乱码,请各位大神指点/
// ThreadpoolAsyncIO.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <windows.h>
#include <tchar.h>
#include <iostream>
VOID CALLBACK OverlappedCompletionRoutine(PTP_CALLBACK_INSTANCE pInstance, PVOID pvContext, PVOID pOverlapped, ULONG IoResult, ULONG_PTR NumberOfBytesTransferred, PTP_IO pIo)
{
std::cout << "实际传输字节数:" << NumberOfBytesTransferred << std::endl;
}
int main()
{
PTP_IO m_pIo = nullptr;
LPVOID m_pVData = nullptr;
HANDLE hFile = nullptr;
try
{
LPCWSTR filename = _T("..\\Debug\\驾照模拟总结.txt");
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
perror("CreateFile failed.\n");
throw -1;
}
m_pIo = CreateThreadpoolIo(hFile, OverlappedCompletionRoutine, NULL, NULL);
if (m_pIo == nullptr)
{
perror("CreateThreadpoolIo failed.\n");
throw -1;
}
StartThreadpoolIo(m_pIo);
LARGE_INTEGER li;
GetFileSizeEx(hFile, &li);
m_pVData = VirtualAlloc(nullptr, li.QuadPart, MEM_COMMIT, PAGE_READWRITE);
if (m_pVData == nullptr)
{
perror("VirtualAlloc failure\n");
throw -1;
}
OVERLAPPED overlap = { 0 };
BOOL ret = ReadFile(hFile, m_pVData, li.QuadPart, nullptr, &overlap);
if (!ret && GetLastError() == ERROR_IO_PENDING)
{
WaitForSingleObject(hFile, INFINITE);
DWORD dBufSize = WideCharToMultiByte(CP_OEMCP, 0, (PWCHAR)m_pVData, -1, NULL, 0, NULL, NULL);
CHAR* strtmp = new char[dBufSize];
if (strtmp == NULL)
{
perror("memory alloc failed\n");
throw -1;
}
WideCharToMultiByte(CP_OEMCP, 0, (PWCHAR)m_pVData, -1, strtmp, dBufSize, nullptr, nullptr);
std::cout << "读取内容:" << strtmp << std::endl;
delete[]strtmp;
}
VirtualFree(m_pVData, 0, MEM_RELEASE);
CloseThreadpoolIo(m_pIo);
CloseHandle(hFile);
}
catch (...)
{
if(m_pVData != nullptr)
VirtualFree(m_pVData, 0, MEM_RELEASE);
if(m_pIo != nullptr)
CloseThreadpoolIo(m_pIo);
if(hFile != nullptr)
CloseHandle(hFile);
}
return 0;
}