void WriteScreenshotStreamToFile(_In_ const char* screenshotLocalId, _In_ XAppCaptureScreenshotFormatFlag screenshotFormat, _In_ const char* filePath)
{
const int MAX_DATA = 1024;
XAppCaptureScreenshotStreamHandle handle = nullptr;
BYTE buffer[MAX_DATA];
HANDLE file = INVALID_HANDLE_VALUE;
UINT64 totalBytesRead = 0;
UINT64 totalBytesToRead = 0;
if (FAILED_LOG(XAppCaptureOpenScreenshotStream(screenshotLocalId, screenshotFormat, &handle, &totalBytesToRead)))
{
return;
}
appLog.AddLog("%I64d bytes returned\n", totalBytesToRead);
file = CreateFileA(filePath, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (file == INVALID_HANDLE_VALUE)
{
appLog.AddLog("Could not create %s\n", filePath);
goto Cleanup;
}
while (totalBytesRead < totalBytesToRead)
{
uint32_t bytesRead = 0;
DWORD bytesWritten = 0;
if (FAILED_LOG(XAppCaptureReadScreenshotStream(handle, totalBytesRead, sizeof(buffer), buffer, &bytesRead)))
{
goto Cleanup;
}
BOOL result = WriteFile(file, buffer, bytesRead, &bytesWritten, NULL );
if (!result)
{
appLog.AddLog("Failed to write to %s\n", filePath);
goto Cleanup;
}
totalBytesRead += bytesRead;
}
Cleanup:
if (handle != nullptr)
{
FAILED_LOG(XAppCaptureCloseScreenshotStream(handle));
}
if (file != INVALID_HANDLE_VALUE)
{
CloseHandle(file);
}
appLog.AddLog("Wrote out %I64d bytes to %s\n", totalBytesRead, filePath);
}