Extract Emf from Spoolfile
- From: as@xxxxxxxx
- Date: 7 Aug 2006 02:33:39 -0700
Hi,
I wrote a small application which extracts emf from a spoolerfile. I
read the articel from Microsoft India
(http://www.microsoft.com/india/msdn/articles/130.aspx), but just the
first page works. Can anybody help me. Do I need the shadow file. Here
is me routine.
Thanks.
bool CMBSpoolFile::GetMetaFileFromSpoolFile(TCHAR *SpoolFileName , int
PageNbr, TCHAR *MetaFileName, LPBYTE *pDevmode)
{
bool bRet = true;
try
{
HANDLE hFile = CreateFile( SpoolFileName,
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
HANDLE hMapFile = CreateFileMapping(
hFile, // handle to file
NULL, // security
PAGE_READONLY, // protection
0, // high-order DWORD of size
0, // low-order DWORD of size
NULL // object name
);
LPBYTE pMapFile = (LPBYTE)MapViewOfFileEx(
hMapFile, // handle to file-mapping object
FILE_MAP_READ, // access mode
0, // high-order DWORD of offset
0, // low-order DWORD of offset
0, // number of bytes to map
NULL // starting address
);
DWORD granularity = *((DWORD *)pMapFile);
pMapFile += sizeof(DWORD);
DWORD splheader = *((DWORD *)pMapFile);
pMapFile += splheader;
DWORD metafilelen = *((DWORD *)pMapFile);
pMapFile += sizeof(DWORD); // This is hack after comparison
with win9x.
for(int Nbr = 1 ; Nbr < PageNbr ; Nbr++)
{
pMapFile += metafilelen;
if(0)//pPDev->pResetDC[Nbr - 1] == FALSE
{
pMapFile += 20;
}
else
{
/* skip the reset devmode here */
pMapFile += 4; // This marker is the same as the one
after Devmode
DWORD offset = *((DWORD *)pMapFile); //this is multiple
of 4bytes.
pMapFile += offset + 4; // devmode + devmode-length
pMapFile += 16 + 4; //Regular 20 bytes
seperator(marker,...,metalen-tillhere,0000,startpagemarker)
}
metafilelen = *((DWORD *)pMapFile);
pMapFile += 4 ; //This has the metafile length!!!
//keep incrementing till we are on the last page.
}
if (metafilelen > 0)
{
HANDLE hMetaFile = CreateFile( MetaFileName,
GENERIC_READ | GENERIC_WRITE, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
CREATE_ALWAYS, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
DWORD numWritten;
WriteFile(
hMetaFile, // handle to output file
pMapFile, // data buffer
metafilelen, // number of bytes to write
&numWritten,
NULL // overlapped buffer
);
if(1)//pPDev->pResetDC[PageNbr - 1] == TRUE
{
LPBYTE ptr = pMapFile + metafilelen;
if(pDevmode)
{
ptr += 4; // This marker is the same as the
// one after Devmode
DWORD offset = *((DWORD *)ptr); //this is multiple
of 4bytes.
*pDevmode = (LPBYTE)malloc(offset);
memcpy(*pDevmode , ptr + 4 , offset);
}
}
CloseHandle(hMetaFile);
}
else
bRet = false;
UnmapViewOfFile( pMapFile); // starting address
CloseHandle(hMapFile);
CloseHandle(hFile);
}
catch (...)
{
return false;
}
return bRet;
}
.
- Follow-Ups:
- Re: Extract Emf from Spoolfile
- From: Christoph Lindemann
- Re: Extract Emf from Spoolfile
- Prev by Date: Re: Filter Hook
- Next by Date: AddPrinter
- Previous by thread: Device Name to Dos Name (IoQueryFileDosDeviceName)
- Next by thread: Re: Extract Emf from Spoolfile
- Index(es):