#include #include #include #include namespace fs = std::filesystem; using namespace std::chrono; namespace { // ------------------------------------------------------------------------------------------------ static void map_test_file (fs::path const& filePath, bool withPause) { withPause = false; // Open file HANDLE hFile = ::CreateFileW(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (!hFile) { std::cerr << "Failed opening file handle\n"; std::exit(-1); } // Get file size size_t fileSize {}; { LARGE_INTEGER sz {}; if (FALSE == ::GetFileSizeEx(hFile, &sz)) { std::cerr << "Failed getting file size\n"; std::exit(-1); } if (sz.QuadPart <= 0) // cannot map a 0 byte file { std::cerr << "Can't map an empty file\n"; std::exit(-1); } fileSize = (size_t)sz.QuadPart; } // Create a file mapping HANDLE hMap = ::CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL); if (!hMap) { std::cerr << "Failed creating mapping object\n"; std::exit(-1); } auto t0 = steady_clock::now(); // Map the entire file void* pData = ::MapViewOfFile( hMap, FILE_MAP_READ, 0, 0, fileSize ); auto t1 = steady_clock::now(); if (!pData) { std::cerr << "Failed mapping view of file\n"; std::exit(-1); } const auto dur = duration_cast(t1 - t0); std::cout << "Opened mapped file in " << dur.count() << "us\n"; ::UnmapViewOfFile(pData); ::CloseHandle(hMap); ::CloseHandle(hFile); } } // anon namespace // ================================================================================================ int main (int argc, char *argv[]) { // Check arguments if (argc < 2) { std::cerr << "Usage: map_file_test \n"; std::exit(-1); } // Get file to map const fs::path filePath = argv[1]; if (std::error_code ec {}; !fs::exists(filePath, ec)) { std::cerr << "Specified file \"" << filePath.string() << "\" doesn't exist\n"; std::exit(-1); } // Test mapping before loading DLL std::cout << "Testing without nvoglv64.dll loaded\n"; map_test_file(filePath, false); // Load troublesome DLL std::cout << "Loading nvoglv64.dll\n"; // Note: nvoglv64.dll lives in a different sub-folder on each system const fs::path dllPath = [] () { for (auto const& ent : fs::recursive_directory_iterator{R"(C:\Windows\System32\DriverStore\FileRepository)"}) { const fs::path curPath = ent.path(); if (curPath.filename() == "nvoglv64.dll") return curPath; } }(); HMODULE hMod = ::LoadLibraryW(dllPath.c_str()); if (!hMod) { std::cerr << "Failed loading nvoglv64.dll from " << dllPath.string() << "\n"; std::exit(-1); } // Test mapping after loading DLL std::cout << "Testing with nvoglv64.dll loaded\n"; map_test_file(filePath, true); // Cleanup ::FreeLibrary(hMod); return 0; }