From 688b18d88c83951766628e513b1ec5d2760662a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Wed, 26 Oct 2016 22:43:53 +0000 Subject: [PATCH] [WINSPOOL_PRINT]: Improve the test application: - Do not hardcode the test file name, but instead take it from the command line (and add a Usage() function); - Do not use a too large fixed-size buffer to read & print the file, but instead use a correctly-size buffer, get the file size, and then do a loop to read data from the file by chunks & send it to the printer. svn path=/trunk/; revision=73047 --- .../cmdutils/winspool_print/CMakeLists.txt | 2 +- .../cmdutils/winspool_print/main.c | 50 ++++++++++++++----- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/rosapps/applications/cmdutils/winspool_print/CMakeLists.txt b/rosapps/applications/cmdutils/winspool_print/CMakeLists.txt index 0b212648e21..93975975a79 100644 --- a/rosapps/applications/cmdutils/winspool_print/CMakeLists.txt +++ b/rosapps/applications/cmdutils/winspool_print/CMakeLists.txt @@ -1,4 +1,4 @@ add_executable(winspool_print main.c) -set_module_type(winspool_print win32cui) +set_module_type(winspool_print win32cui UNICODE) add_importlibs(winspool_print winspool msvcrt kernel32) add_cd_file(TARGET winspool_print DESTINATION reactos/system32 FOR all) diff --git a/rosapps/applications/cmdutils/winspool_print/main.c b/rosapps/applications/cmdutils/winspool_print/main.c index 84173efa5f6..199954d2dbc 100644 --- a/rosapps/applications/cmdutils/winspool_print/main.c +++ b/rosapps/applications/cmdutils/winspool_print/main.c @@ -1,26 +1,38 @@ #include #include -int main() +void Usage(WCHAR* name) +{ + wprintf(L"Usage: %s testfile\n", name); +} + +int wmain(int argc, WCHAR* argv[]) { int ReturnValue = 1; - DWORD dwRead; - DWORD dwWritten; + DWORD dwFileSize; + DWORD dwRead, dwWritten; HANDLE hFile = INVALID_HANDLE_VALUE; HANDLE hPrinter = NULL; DOC_INFO_1W docInfo; - BYTE Buffer[20000]; + BYTE Buffer[4096]; - hFile = CreateFileW(L"testfile", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); + if (argc <= 1) + { + Usage(argv[0]); + return 0; + } + + hFile = CreateFileW(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("CreateFileW failed, last error is %lu!\n", GetLastError()); goto Cleanup; } - if (!ReadFile(hFile, Buffer, sizeof(Buffer), &dwRead, NULL)) + dwFileSize = GetFileSize(hFile, NULL); + if (dwFileSize == INVALID_FILE_SIZE) { - printf("ReadFile failed, last error is %lu!\n", GetLastError()); + printf("File is too big, or GetFileSize failed; last error is %lu!\n", GetLastError()); goto Cleanup; } @@ -30,6 +42,7 @@ int main() goto Cleanup; } + /* Print to a printer, with the "RAW" datatype (pDatatype == NULL or "RAW") */ ZeroMemory(&docInfo, sizeof(docInfo)); docInfo.pDocName = L"winspool_print"; @@ -45,10 +58,21 @@ int main() goto Cleanup; } - if (!WritePrinter(hPrinter, Buffer, dwRead, &dwWritten)) + while (dwFileSize > 0) { - printf("WritePrinter failed, last error is %lu!\n", GetLastError()); - goto Cleanup; + 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; + } } if (!EndPagePrinter(hPrinter)) @@ -66,11 +90,11 @@ int main() ReturnValue = 0; Cleanup: - if (hFile != INVALID_HANDLE_VALUE) - CloseHandle(hFile); - if (hPrinter) ClosePrinter(hPrinter); + if (hFile != INVALID_HANDLE_VALUE) + CloseHandle(hFile); + return ReturnValue; }