1 #include <stdio.h> 2 #include <windows.h> 3 4 void Usage(WCHAR* name) 5 { 6 wprintf(L"Usage: %s testfile\n", name); 7 } 8 9 int wmain(int argc, WCHAR* argv[]) 10 { 11 int ReturnValue = 1; 12 DWORD dwFileSize; 13 DWORD dwRead, dwWritten; 14 HANDLE hFile = INVALID_HANDLE_VALUE; 15 HANDLE hPrinter = NULL; 16 DOC_INFO_1W docInfo; 17 BYTE Buffer[4096]; 18 19 if (argc <= 1) 20 { 21 Usage(argv[0]); 22 return 0; 23 } 24 25 hFile = CreateFileW(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); 26 if (hFile == INVALID_HANDLE_VALUE) 27 { 28 printf("CreateFileW failed, last error is %lu!\n", GetLastError()); 29 goto Cleanup; 30 } 31 32 dwFileSize = GetFileSize(hFile, NULL); 33 if (dwFileSize == INVALID_FILE_SIZE) 34 { 35 printf("File is too big, or GetFileSize failed; last error is %lu!\n", GetLastError()); 36 goto Cleanup; 37 } 38 39 if (!OpenPrinterW(L"Dummy Printer On LPT1", &hPrinter, NULL)) 40 { 41 printf("OpenPrinterW failed, last error is %lu!\n", GetLastError()); 42 goto Cleanup; 43 } 44 45 /* Print to a printer, with the "RAW" datatype (pDatatype == NULL or "RAW") */ 46 ZeroMemory(&docInfo, sizeof(docInfo)); 47 docInfo.pDocName = L"winspool_print"; 48 49 if (!StartDocPrinterW(hPrinter, 1, (LPBYTE)&docInfo)) 50 { 51 printf("StartDocPrinterW failed, last error is %lu!\n", GetLastError()); 52 goto Cleanup; 53 } 54 55 if (!StartPagePrinter(hPrinter)) 56 { 57 printf("StartPagePrinter failed, last error is %lu!\n", GetLastError()); 58 goto Cleanup; 59 } 60 61 while (dwFileSize > 0) 62 { 63 dwRead = min(sizeof(Buffer), dwFileSize); 64 if (!ReadFile(hFile, Buffer, dwRead, &dwRead, NULL)) 65 { 66 printf("ReadFile failed, last error is %lu!\n", GetLastError()); 67 goto Cleanup; 68 } 69 dwFileSize -= dwRead; 70 71 if (!WritePrinter(hPrinter, Buffer, dwRead, &dwWritten)) 72 { 73 printf("WritePrinter failed, last error is %lu!\n", GetLastError()); 74 goto Cleanup; 75 } 76 } 77 78 if (!EndPagePrinter(hPrinter)) 79 { 80 printf("EndPagePrinter failed, last error is %lu!\n", GetLastError()); 81 goto Cleanup; 82 } 83 84 if (!EndDocPrinter(hPrinter)) 85 { 86 printf("EndDocPrinter failed, last error is %lu!\n", GetLastError()); 87 goto Cleanup; 88 } 89 90 ReturnValue = 0; 91 92 Cleanup: 93 if (hPrinter) 94 ClosePrinter(hPrinter); 95 96 if (hFile != INVALID_HANDLE_VALUE) 97 CloseHandle(hFile); 98 99 return ReturnValue; 100 } 101