2015-11-14 16:45:41 +00:00
|
|
|
/*
|
2018-05-13 20:15:41 +00:00
|
|
|
* PROJECT: ReactOS Clipboard Viewer
|
|
|
|
* LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
|
|
|
|
* PURPOSE: Clipboard file format helper functions.
|
|
|
|
* COPYRIGHT: Copyright 2015-2018 Ricardo Hanke
|
|
|
|
* Copyright 2015-2018 Hermes Belusca-Maito
|
2015-11-14 16:45:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precomp.h"
|
|
|
|
|
2015-11-28 01:22:34 +00:00
|
|
|
static HGLOBAL ClipboardReadMemoryBlock(HANDLE hFile, DWORD dwOffset, DWORD dwLength)
|
|
|
|
{
|
|
|
|
HGLOBAL hData;
|
|
|
|
LPVOID lpData;
|
|
|
|
DWORD dwBytesRead;
|
|
|
|
|
|
|
|
hData = GlobalAlloc(GHND, dwLength);
|
|
|
|
if (!hData)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
lpData = GlobalLock(hData);
|
|
|
|
if (!lpData)
|
|
|
|
{
|
|
|
|
GlobalFree(hData);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SetFilePointer(hFile, dwOffset, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
|
|
|
|
{
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
GlobalFree(hData);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadFile(hFile, lpData, dwLength, &dwBytesRead, NULL))
|
|
|
|
{
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
GlobalFree(hData);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
|
|
|
|
return hData;
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:08:09 +00:00
|
|
|
static BOOL ClipboardReadMemory(HANDLE hFile, DWORD dwFormat, DWORD dwOffset, DWORD dwLength, WORD FileIdentifier, PVOID lpFormatName)
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
|
|
|
HGLOBAL hData;
|
2016-02-21 11:33:54 +00:00
|
|
|
DWORD dwTemp = 0;
|
2015-11-28 01:22:34 +00:00
|
|
|
|
|
|
|
hData = ClipboardReadMemoryBlock(hFile, dwOffset, dwLength);
|
|
|
|
if (!hData)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if ((dwFormat >= 0xC000) && (dwFormat <= 0xFFFF))
|
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
if (FileIdentifier == CLIP_FMT_31)
|
|
|
|
dwTemp = RegisterClipboardFormatA((LPCSTR)lpFormatName);
|
|
|
|
else if ((FileIdentifier == CLIP_FMT_NT) || (FileIdentifier == CLIP_FMT_BK))
|
|
|
|
dwTemp = RegisterClipboardFormatW((LPCWSTR)lpFormatName);
|
|
|
|
|
2015-11-28 01:22:34 +00:00
|
|
|
if (!dwTemp)
|
|
|
|
{
|
|
|
|
GlobalFree(hData);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dwTemp = dwFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetClipboardData(dwTemp, hData))
|
|
|
|
{
|
|
|
|
GlobalFree(hData);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-02-13 20:57:39 +00:00
|
|
|
static BOOL ClipboardWriteMemory(HANDLE hFile, DWORD dwFormat, DWORD dwOffset, PDWORD pdwLength)
|
|
|
|
{
|
|
|
|
HGLOBAL hData;
|
|
|
|
LPVOID lpData;
|
|
|
|
DWORD dwBytesWritten;
|
|
|
|
|
|
|
|
hData = GetClipboardData(dwFormat);
|
|
|
|
if (!hData)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lpData = GlobalLock(hData);
|
|
|
|
if (!lpData)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*pdwLength = GlobalSize(hData);
|
|
|
|
|
|
|
|
if (SetFilePointer(hFile, dwOffset, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
|
|
|
|
{
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WriteFile(hFile, lpData, *pdwLength, &dwBytesWritten, NULL))
|
|
|
|
{
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-28 01:22:34 +00:00
|
|
|
static BOOL ClipboardReadPalette(HANDLE hFile, DWORD dwOffset, DWORD dwLength)
|
|
|
|
{
|
|
|
|
LPLOGPALETTE lpPalette;
|
|
|
|
HPALETTE hPalette;
|
|
|
|
HGLOBAL hData;
|
|
|
|
|
|
|
|
hData = ClipboardReadMemoryBlock(hFile, dwOffset, dwLength);
|
|
|
|
if (!hData)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpPalette = GlobalLock(hData);
|
|
|
|
if (!lpPalette)
|
|
|
|
{
|
|
|
|
GlobalFree(hData);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
hPalette = CreatePalette(lpPalette);
|
|
|
|
if (!hPalette)
|
|
|
|
{
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
GlobalFree(hData);
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
GlobalFree(hData);
|
|
|
|
|
|
|
|
if (!SetClipboardData(CF_PALETTE, hPalette))
|
|
|
|
{
|
|
|
|
DeleteObject(hPalette);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:08:09 +00:00
|
|
|
static BOOL ClipboardReadMetafile(HANDLE hFile, DWORD dwOffset, DWORD dwLength)
|
|
|
|
{
|
|
|
|
HMETAFILE hMf;
|
|
|
|
HGLOBAL hData;
|
|
|
|
LPVOID lpData;
|
|
|
|
|
|
|
|
hData = ClipboardReadMemoryBlock(hFile, dwOffset, dwLength);
|
|
|
|
if (!hData)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpData = GlobalLock(hData);
|
|
|
|
if (!lpData)
|
|
|
|
{
|
|
|
|
GlobalFree(hData);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
hMf = SetMetaFileBitsEx(dwLength, lpData);
|
|
|
|
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
GlobalFree(hData);
|
|
|
|
|
|
|
|
if (!hMf)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetClipboardData(CF_METAFILEPICT, hMf))
|
|
|
|
{
|
|
|
|
DeleteMetaFile(hMf);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-28 01:22:34 +00:00
|
|
|
static BOOL ClipboardReadEnhMetafile(HANDLE hFile, DWORD dwOffset, DWORD dwLength)
|
|
|
|
{
|
|
|
|
HENHMETAFILE hEmf;
|
|
|
|
HGLOBAL hData;
|
|
|
|
LPVOID lpData;
|
|
|
|
|
|
|
|
hData = ClipboardReadMemoryBlock(hFile, dwOffset, dwLength);
|
|
|
|
if (!hData)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpData = GlobalLock(hData);
|
|
|
|
if (!lpData)
|
|
|
|
{
|
|
|
|
GlobalFree(hData);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
hEmf = SetEnhMetaFileBits(dwLength, lpData);
|
|
|
|
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
GlobalFree(hData);
|
|
|
|
|
|
|
|
if (!hEmf)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetClipboardData(CF_ENHMETAFILE, hEmf))
|
|
|
|
{
|
|
|
|
DeleteEnhMetaFile(hEmf);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL ClipboardReadBitmap(HANDLE hFile, DWORD dwOffset, DWORD dwLength)
|
|
|
|
{
|
|
|
|
HGLOBAL hData;
|
|
|
|
HBITMAP hBitmap;
|
|
|
|
LPBITMAP lpBitmap;
|
|
|
|
|
|
|
|
hData = ClipboardReadMemoryBlock(hFile, dwOffset, dwLength);
|
|
|
|
if (!hData)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpBitmap = GlobalLock(hData);
|
|
|
|
if (!lpBitmap)
|
|
|
|
{
|
|
|
|
GlobalFree(hData);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpBitmap->bmBits = lpBitmap + sizeof(BITMAP) + 1;
|
|
|
|
|
|
|
|
hBitmap = CreateBitmapIndirect(lpBitmap);
|
|
|
|
|
|
|
|
GlobalUnlock(hData);
|
|
|
|
GlobalFree(hData);
|
|
|
|
|
|
|
|
if (!hBitmap)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetClipboardData(CF_BITMAP, hBitmap))
|
|
|
|
{
|
|
|
|
DeleteObject(hBitmap);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-14 16:45:41 +00:00
|
|
|
void ReadClipboardFile(LPCWSTR lpFileName)
|
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
CLIPFILEHEADER ClipFileHeader;
|
|
|
|
CLIPFORMATHEADER ClipFormatArray;
|
|
|
|
NTCLIPFILEHEADER NtClipFileHeader;
|
|
|
|
NTCLIPFORMATHEADER NtClipFormatArray;
|
|
|
|
PVOID pClipFileHeader;
|
|
|
|
PVOID pClipFormatArray;
|
|
|
|
DWORD SizeOfFileHeader, SizeOfFormatHeader;
|
|
|
|
|
|
|
|
WORD wFileIdentifier;
|
|
|
|
WORD wFormatCount;
|
|
|
|
DWORD dwFormatID;
|
|
|
|
DWORD dwLenData;
|
|
|
|
DWORD dwOffData;
|
|
|
|
PVOID szName;
|
|
|
|
|
2015-11-28 01:22:34 +00:00
|
|
|
HANDLE hFile;
|
|
|
|
DWORD dwBytesRead;
|
|
|
|
BOOL bResult;
|
|
|
|
int i;
|
|
|
|
|
2016-02-13 17:04:40 +00:00
|
|
|
/* Open the file for read access */
|
|
|
|
hFile = CreateFileW(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2015-11-28 01:22:34 +00:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:08:09 +00:00
|
|
|
/* Just read enough bytes to get the clipboard file format ID */
|
|
|
|
if (!ReadFile(hFile, &wFileIdentifier, sizeof(wFileIdentifier), &dwBytesRead, NULL))
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:08:09 +00:00
|
|
|
/* Set data according to the clipboard file format ID */
|
|
|
|
switch (wFileIdentifier)
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
case CLIP_FMT_31:
|
|
|
|
SizeOfFileHeader = sizeof(CLIPFILEHEADER);
|
|
|
|
SizeOfFormatHeader = sizeof(CLIPFORMATHEADER);
|
|
|
|
pClipFileHeader = &ClipFileHeader;
|
|
|
|
pClipFormatArray = &ClipFormatArray;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLIP_FMT_NT:
|
|
|
|
case CLIP_FMT_BK:
|
|
|
|
SizeOfFileHeader = sizeof(NTCLIPFILEHEADER);
|
|
|
|
SizeOfFormatHeader = sizeof(NTCLIPFORMATHEADER);
|
|
|
|
pClipFileHeader = &NtClipFileHeader;
|
|
|
|
pClipFormatArray = &NtClipFormatArray;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MessageBoxRes(Globals.hMainWnd, Globals.hInstance, ERROR_INVALID_FILE_FORMAT, 0, MB_ICONSTOP | MB_OK);
|
|
|
|
goto done;
|
2015-11-28 01:22:34 +00:00
|
|
|
}
|
2016-02-13 17:04:40 +00:00
|
|
|
|
2016-02-13 16:08:09 +00:00
|
|
|
/* Completely read the header */
|
2016-02-13 17:04:40 +00:00
|
|
|
SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
|
2016-02-13 16:08:09 +00:00
|
|
|
if (!ReadFile(hFile, pClipFileHeader, SizeOfFileHeader, &dwBytesRead, NULL) ||
|
|
|
|
dwBytesRead != SizeOfFileHeader)
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:08:09 +00:00
|
|
|
/* Get header data */
|
|
|
|
switch (wFileIdentifier)
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
case CLIP_FMT_31:
|
|
|
|
assert(wFileIdentifier == ((CLIPFILEHEADER*)pClipFileHeader)->wFileIdentifier);
|
|
|
|
wFormatCount = ((CLIPFILEHEADER*)pClipFileHeader)->wFormatCount;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLIP_FMT_NT:
|
|
|
|
case CLIP_FMT_BK:
|
|
|
|
assert(wFileIdentifier == ((NTCLIPFILEHEADER*)pClipFileHeader)->wFileIdentifier);
|
|
|
|
wFormatCount = ((NTCLIPFILEHEADER*)pClipFileHeader)->wFormatCount;
|
|
|
|
break;
|
2015-11-28 01:22:34 +00:00
|
|
|
}
|
2016-02-13 17:04:40 +00:00
|
|
|
|
|
|
|
/* Loop through the format data array */
|
2016-02-13 16:08:09 +00:00
|
|
|
for (i = 0; i < wFormatCount; i++)
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
if (SetFilePointer(hFile, SizeOfFileHeader + i * SizeOfFormatHeader, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
|
|
|
|
{
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadFile(hFile, pClipFormatArray, SizeOfFormatHeader, &dwBytesRead, NULL))
|
|
|
|
{
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get format data */
|
|
|
|
switch (wFileIdentifier)
|
|
|
|
{
|
|
|
|
case CLIP_FMT_31:
|
|
|
|
dwFormatID = ((CLIPFORMATHEADER*)pClipFormatArray)->dwFormatID;
|
|
|
|
dwLenData = ((CLIPFORMATHEADER*)pClipFormatArray)->dwLenData;
|
|
|
|
dwOffData = ((CLIPFORMATHEADER*)pClipFormatArray)->dwOffData;
|
|
|
|
szName = ((CLIPFORMATHEADER*)pClipFormatArray)->szName;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLIP_FMT_NT:
|
|
|
|
case CLIP_FMT_BK:
|
|
|
|
dwFormatID = ((NTCLIPFORMATHEADER*)pClipFormatArray)->dwFormatID;
|
|
|
|
dwLenData = ((NTCLIPFORMATHEADER*)pClipFormatArray)->dwLenData;
|
|
|
|
dwOffData = ((NTCLIPFORMATHEADER*)pClipFormatArray)->dwOffData;
|
|
|
|
szName = ((NTCLIPFORMATHEADER*)pClipFormatArray)->szName;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (dwFormatID)
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
|
|
|
case CF_OWNERDISPLAY:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CF_DSPBITMAP:
|
2018-05-13 20:15:41 +00:00
|
|
|
case CF_BITMAP:
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
bResult = ClipboardReadBitmap(hFile, dwOffData, dwLenData);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CF_DSPMETAFILEPICT:
|
2018-05-13 20:15:41 +00:00
|
|
|
case CF_METAFILEPICT:
|
2016-02-13 16:08:09 +00:00
|
|
|
{
|
|
|
|
bResult = ClipboardReadMetafile(hFile, dwOffData, dwLenData);
|
2015-11-28 01:22:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:08:09 +00:00
|
|
|
case CF_DSPENHMETAFILE:
|
2018-05-13 20:15:41 +00:00
|
|
|
case CF_ENHMETAFILE:
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
bResult = ClipboardReadEnhMetafile(hFile, dwOffData, dwLenData);
|
2015-11-28 01:22:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CF_PALETTE:
|
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
bResult = ClipboardReadPalette(hFile, dwOffData, dwLenData);
|
2015-11-28 01:22:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
if ((dwFormatID < CF_PRIVATEFIRST) || (dwFormatID > CF_PRIVATELAST))
|
2015-11-28 01:22:34 +00:00
|
|
|
{
|
2016-02-13 16:08:09 +00:00
|
|
|
bResult = ClipboardReadMemory(hFile, dwFormatID, dwOffData, dwLenData, wFileIdentifier, szName);
|
2015-11-28 01:22:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bResult)
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (hFile != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(hFile);
|
|
|
|
|
2015-11-14 16:45:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-13 17:04:40 +00:00
|
|
|
void WriteClipboardFile(LPCWSTR lpFileName, WORD wFileIdentifier)
|
2015-11-14 16:45:41 +00:00
|
|
|
{
|
2016-02-13 17:04:40 +00:00
|
|
|
CLIPFILEHEADER ClipFileHeader;
|
|
|
|
CLIPFORMATHEADER ClipFormatArray;
|
|
|
|
NTCLIPFILEHEADER NtClipFileHeader;
|
|
|
|
NTCLIPFORMATHEADER NtClipFormatArray;
|
|
|
|
PVOID pClipFileHeader;
|
|
|
|
PVOID pClipFormatArray;
|
|
|
|
DWORD SizeOfFileHeader, SizeOfFormatHeader;
|
|
|
|
|
|
|
|
WORD wFormatCount;
|
|
|
|
DWORD dwFormatID;
|
|
|
|
DWORD dwLenData;
|
|
|
|
DWORD dwOffData;
|
|
|
|
// PVOID szName;
|
|
|
|
|
|
|
|
HANDLE hFile;
|
|
|
|
DWORD dwBytesWritten;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Create the file for write access */
|
|
|
|
hFile = CreateFileW(lpFileName, GENERIC_WRITE, 0, NULL,
|
|
|
|
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
wFormatCount = CountClipboardFormats();
|
|
|
|
|
|
|
|
/* Select the file format and setup the header according to the clipboard file format ID */
|
|
|
|
switch (wFileIdentifier)
|
|
|
|
{
|
|
|
|
case CLIP_FMT_31:
|
|
|
|
SizeOfFileHeader = sizeof(CLIPFILEHEADER);
|
|
|
|
SizeOfFormatHeader = sizeof(CLIPFORMATHEADER);
|
|
|
|
pClipFileHeader = &ClipFileHeader;
|
|
|
|
pClipFormatArray = &ClipFormatArray;
|
|
|
|
|
|
|
|
ClipFileHeader.wFileIdentifier = CLIP_FMT_31; // wFileIdentifier
|
|
|
|
ClipFileHeader.wFormatCount = wFormatCount;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLIP_FMT_NT:
|
|
|
|
case CLIP_FMT_BK:
|
|
|
|
SizeOfFileHeader = sizeof(NTCLIPFILEHEADER);
|
|
|
|
SizeOfFormatHeader = sizeof(NTCLIPFORMATHEADER);
|
|
|
|
pClipFileHeader = &NtClipFileHeader;
|
|
|
|
pClipFormatArray = &NtClipFormatArray;
|
|
|
|
|
|
|
|
NtClipFileHeader.wFileIdentifier = CLIP_FMT_NT; // wFileIdentifier
|
|
|
|
NtClipFileHeader.wFormatCount = wFormatCount;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MessageBoxRes(Globals.hMainWnd, Globals.hInstance, ERROR_INVALID_FILE_FORMAT, 0, MB_ICONSTOP | MB_OK);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the header */
|
|
|
|
SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
|
|
|
|
if (!WriteFile(hFile, pClipFileHeader, SizeOfFileHeader, &dwBytesWritten, NULL) ||
|
|
|
|
dwBytesWritten != SizeOfFileHeader)
|
|
|
|
{
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute where the data should start (after the file header and the format array) */
|
|
|
|
dwOffData = SizeOfFileHeader + wFormatCount * SizeOfFormatHeader;
|
|
|
|
|
|
|
|
/* Loop through each format and save the data */
|
|
|
|
i = 0;
|
|
|
|
dwFormatID = EnumClipboardFormats(0);
|
|
|
|
while (dwFormatID)
|
|
|
|
{
|
2016-02-13 20:57:39 +00:00
|
|
|
if (i >= wFormatCount)
|
|
|
|
{
|
|
|
|
/* Must never happen! */
|
|
|
|
assert(FALSE);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-13 17:04:40 +00:00
|
|
|
|
2016-02-13 20:57:39 +00:00
|
|
|
/* Write the clipboard data at the specified offset, and retrieve its length */
|
|
|
|
if (!ClipboardWriteMemory(hFile, dwFormatID, dwOffData, &dwLenData))
|
|
|
|
goto Cont;
|
2016-02-13 17:04:40 +00:00
|
|
|
|
2016-02-13 20:57:39 +00:00
|
|
|
/* Write the format data header */
|
2016-02-13 17:04:40 +00:00
|
|
|
switch (wFileIdentifier)
|
|
|
|
{
|
|
|
|
case CLIP_FMT_31:
|
2016-02-13 20:57:39 +00:00
|
|
|
ZeroMemory(pClipFormatArray, sizeof(CLIPFORMATHEADER));
|
2016-02-13 17:04:40 +00:00
|
|
|
((CLIPFORMATHEADER*)pClipFormatArray)->dwFormatID = dwFormatID;
|
|
|
|
((CLIPFORMATHEADER*)pClipFormatArray)->dwLenData = dwLenData;
|
|
|
|
((CLIPFORMATHEADER*)pClipFormatArray)->dwOffData = dwOffData;
|
2016-02-13 20:57:39 +00:00
|
|
|
RetrieveClipboardFormatName(Globals.hInstance,
|
|
|
|
dwFormatID,
|
|
|
|
FALSE,
|
|
|
|
((CLIPFORMATHEADER*)pClipFormatArray)->szName,
|
|
|
|
ARRAYSIZE(((CLIPFORMATHEADER*)pClipFormatArray)->szName));
|
2016-02-13 17:04:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CLIP_FMT_NT:
|
|
|
|
case CLIP_FMT_BK:
|
2016-02-13 20:57:39 +00:00
|
|
|
ZeroMemory(pClipFormatArray, sizeof(NTCLIPFORMATHEADER));
|
2016-02-13 17:04:40 +00:00
|
|
|
((NTCLIPFORMATHEADER*)pClipFormatArray)->dwFormatID = dwFormatID;
|
|
|
|
((NTCLIPFORMATHEADER*)pClipFormatArray)->dwLenData = dwLenData;
|
|
|
|
((NTCLIPFORMATHEADER*)pClipFormatArray)->dwOffData = dwOffData;
|
2016-02-13 20:57:39 +00:00
|
|
|
RetrieveClipboardFormatName(Globals.hInstance,
|
|
|
|
dwFormatID,
|
|
|
|
TRUE,
|
|
|
|
((NTCLIPFORMATHEADER*)pClipFormatArray)->szName,
|
|
|
|
ARRAYSIZE(((NTCLIPFORMATHEADER*)pClipFormatArray)->szName));
|
2016-02-13 17:04:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SetFilePointer(hFile, SizeOfFileHeader + i * SizeOfFormatHeader, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
|
|
|
|
{
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WriteFile(hFile, pClipFormatArray, SizeOfFormatHeader, &dwBytesWritten, NULL))
|
|
|
|
{
|
|
|
|
ShowLastWin32Error(Globals.hMainWnd);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust the offset for the next data stream */
|
|
|
|
dwOffData += dwLenData;
|
|
|
|
|
2016-02-13 20:57:39 +00:00
|
|
|
Cont:
|
2016-02-13 17:04:40 +00:00
|
|
|
i++;
|
|
|
|
dwFormatID = EnumClipboardFormats(dwFormatID);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (hFile != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(hFile);
|
|
|
|
|
2015-11-14 16:45:41 +00:00
|
|
|
return;
|
|
|
|
}
|