//Example of copying device's block UINT uiErrModeSav = SetErrorMode( SEM_FAILCRITICALERRORS); WCHAR szDevName[_MAX_PATH]; if (!GetDeviceName(szDevName, ARRLEN(szDevName), argv[1])) { wprintf(L"Could not recognize %s/n", argv[1]); SetErrorMode(uiErrModeSav); return 1; } HANDLE hDev = OpenBlockDevice(szDevName, false); if (hDev == INVALID_HANDLE_VALUE) { DWORD dwErr = GetLastError(); wprintf(L"Could not open %s, " L"error %d (0x%08x)/n", argv[1], dwErr, dwErr); SetErrorMode(uiErrModeSav); return 2; } // To communicate with device, memory must be on 64kb boundary, const int kBufferSize = 64 * 1024; void *buffer = VirtualAlloc( NULL, kBufferSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (!buffer) { DWORD dwErr = GetLastError(); wprintf (L"Could not reserve memory, " L"error %d (0x%08x)/n", dwErr, dwErr); CloseHandle(hDev); SetErrorMode(uiErrModeSav); return 3; } // OK, Copy data DWORD dwTickStart = GetTickCount(); unsigned __int64 ui64BytesRead = 0; DWORD dwErr = ERROR_SUCCESS; if (_wcsicmp(argv[2], L"readto") == 0) { HANDLE hFile = CreateFile( argv[3], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if (hFile != INVALID_HANDLE_VALUE) { wprintf(L"%s --> %s:/n", szDevName, argv[3]); for ( ; ; ) { DWORD dwRead = 0; if (!ReadFile(hDev, buffer, kBufferSize, &dwRead, NULL)) { dwErr = GetLastError(); wprintf(L"Read error %d (0x%08x)/n", dwErr, dwErr); break; } if (dwRead == 0) break; DWORD dwWrit = 0; if (!WriteFile(hFile, buffer, dwRead, &dwWrit, NULL) || dwWrit != dwRead) { dwErr = GetLastError(); wprintf(L"Write error %d (0x%08x)/n", dwErr, dwErr); break; } ui64BytesRead += dwWrit; wprintf(L"%I64u/r", ui64BytesRead); } CloseHandle(hFile); if (dwErr != ERROR_SUCCESS) DeleteFile(argv[3]); } else { dwErr = GetLastError(); wprintf (L"Could not open %s, error %d " L"(0x%08x)/n", argv[3], dwErr, dwErr); } } else { wprintf (L"Command %s not recognized./n", argv[2]); dwErr = ERROR_NOT_SUPPORTED; } if (dwErr == ERROR_SUCCESS && ui64BytesRead != 0) { wprintf(L"Transferred %I64u bytes in %d " L"seconds./n", ui64BytesRead, (GetTickCount() - dwTickStart) / 1000); } // Clean up VirtualFree(buffer, 0, MEM_RELEASE); CloseHandle(hDev); SetErrorMode(uiErrModeSav); return dwErr == ERROR_SUCCESS ? 0 : 4;