2015-05-04 13:39:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <windows.h>
|
|
|
|
|
2016-10-26 22:43:53 +00:00
|
|
|
void Usage(WCHAR* name)
|
|
|
|
{
|
|
|
|
wprintf(L"Usage: %s testfile\n", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int wmain(int argc, WCHAR* argv[])
|
2015-05-04 13:39:07 +00:00
|
|
|
{
|
|
|
|
int ReturnValue = 1;
|
2016-10-26 22:43:53 +00:00
|
|
|
DWORD dwFileSize;
|
|
|
|
DWORD dwRead, dwWritten;
|
2015-07-23 17:48:17 +00:00
|
|
|
HANDLE hFile = INVALID_HANDLE_VALUE;
|
2015-05-04 13:39:07 +00:00
|
|
|
HANDLE hPrinter = NULL;
|
|
|
|
DOC_INFO_1W docInfo;
|
2016-10-26 22:43:53 +00:00
|
|
|
BYTE Buffer[4096];
|
2015-05-04 13:39:07 +00:00
|
|
|
|
2016-10-26 22:43:53 +00:00
|
|
|
if (argc <= 1)
|
|
|
|
{
|
|
|
|
Usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hFile = CreateFileW(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
|
2015-07-23 17:48:17 +00:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
2015-05-04 13:39:07 +00:00
|
|
|
{
|
2015-07-23 17:48:17 +00:00
|
|
|
printf("CreateFileW failed, last error is %lu!\n", GetLastError());
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
2016-10-26 22:43:53 +00:00
|
|
|
dwFileSize = GetFileSize(hFile, NULL);
|
|
|
|
if (dwFileSize == INVALID_FILE_SIZE)
|
2015-07-23 17:48:17 +00:00
|
|
|
{
|
2016-10-26 22:43:53 +00:00
|
|
|
printf("File is too big, or GetFileSize failed; last error is %lu!\n", GetLastError());
|
2015-07-23 17:48:17 +00:00
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!OpenPrinterW(L"Dummy Printer On LPT1", &hPrinter, NULL))
|
|
|
|
{
|
|
|
|
printf("OpenPrinterW failed, last error is %lu!\n", GetLastError());
|
2015-05-04 13:39:07 +00:00
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
2016-10-26 22:43:53 +00:00
|
|
|
/* Print to a printer, with the "RAW" datatype (pDatatype == NULL or "RAW") */
|
2015-05-04 13:39:07 +00:00
|
|
|
ZeroMemory(&docInfo, sizeof(docInfo));
|
|
|
|
docInfo.pDocName = L"winspool_print";
|
|
|
|
|
|
|
|
if (!StartDocPrinterW(hPrinter, 1, (LPBYTE)&docInfo))
|
|
|
|
{
|
2015-07-23 17:48:17 +00:00
|
|
|
printf("StartDocPrinterW failed, last error is %lu!\n", GetLastError());
|
2015-05-04 13:39:07 +00:00
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!StartPagePrinter(hPrinter))
|
|
|
|
{
|
2015-07-23 17:48:17 +00:00
|
|
|
printf("StartPagePrinter failed, last error is %lu!\n", GetLastError());
|
2015-05-04 13:39:07 +00:00
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
2016-10-26 22:43:53 +00:00
|
|
|
while (dwFileSize > 0)
|
2015-05-04 13:39:07 +00:00
|
|
|
{
|
2016-10-26 22:43:53 +00:00
|
|
|
dwRead = min(sizeof(Buffer), dwFileSize);
|
|
|
|
if (!ReadFile(hFile, Buffer, dwRead, &dwRead, NULL))
|
|
|
|
{
|
|
|
|
printf("ReadFile failed, last error is %lu!\n", GetLastError());
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
dwFileSize -= dwRead;
|
|
|
|
|
|
|
|
if (!WritePrinter(hPrinter, Buffer, dwRead, &dwWritten))
|
|
|
|
{
|
|
|
|
printf("WritePrinter failed, last error is %lu!\n", GetLastError());
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
2015-05-04 13:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!EndPagePrinter(hPrinter))
|
|
|
|
{
|
2015-07-23 17:48:17 +00:00
|
|
|
printf("EndPagePrinter failed, last error is %lu!\n", GetLastError());
|
2015-05-04 13:39:07 +00:00
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EndDocPrinter(hPrinter))
|
|
|
|
{
|
2015-07-23 17:48:17 +00:00
|
|
|
printf("EndDocPrinter failed, last error is %lu!\n", GetLastError());
|
2015-05-04 13:39:07 +00:00
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue = 0;
|
|
|
|
|
|
|
|
Cleanup:
|
|
|
|
if (hPrinter)
|
|
|
|
ClosePrinter(hPrinter);
|
|
|
|
|
2016-10-26 22:43:53 +00:00
|
|
|
if (hFile != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(hFile);
|
|
|
|
|
2015-05-04 13:39:07 +00:00
|
|
|
return ReturnValue;
|
|
|
|
}
|