├── .gitattributes ├── .gitignore ├── LICENSE.txt ├── README.md ├── WinPrintServer.sln └── WinPrintServer ├── WinPrintServer.cpp ├── WinPrintServer.vcxproj ├── WinPrintServer.vcxproj.filters └── WinPrintServer.vcxproj.user /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | /WinPrintServer/x64/Debug 34 | /x64/Debug 35 | /.vs 36 | /WinPrintServer/x64/Release 37 | /x64/Release 38 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WinPrintServer 2 | Windows TCP Print Server that will forward raw data directly to a printer 3 | 4 | ## Usage 5 | Run `WinPrintServer.exe` without any commandline arguments to present your default printer as a RAW printer on port 9100. 6 | 7 | To present a printer other than the default printer, you can specify the printer name on the commandline. 8 | 9 | ``` 10 | WinPrintServer [options] [printername] 11 | -h Show this help information 12 | printername The name of the printer to print. If not specified the default printer is used 13 | ``` 14 | 15 | ## Example 16 | 17 | Present a printer called `EPSON098CEF (WF-3520 Series)` as a RAW printer use the following command. 18 | 19 | ``` 20 | WinPrintServer "EPSON098CEF (WF-3520 Series)" 21 | ``` 22 | 23 | *Note* - Since the printer name has spaces, the name is quoted on the command line -------------------------------------------------------------------------------- /WinPrintServer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.1.32328.378 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinPrintServer", "WinPrintServer\WinPrintServer.vcxproj", "{F2450B87-DEB4-4C6D-A824-96A2C9D66A36}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {F2450B87-DEB4-4C6D-A824-96A2C9D66A36}.Debug|x64.ActiveCfg = Debug|x64 17 | {F2450B87-DEB4-4C6D-A824-96A2C9D66A36}.Debug|x64.Build.0 = Debug|x64 18 | {F2450B87-DEB4-4C6D-A824-96A2C9D66A36}.Debug|x86.ActiveCfg = Debug|Win32 19 | {F2450B87-DEB4-4C6D-A824-96A2C9D66A36}.Debug|x86.Build.0 = Debug|Win32 20 | {F2450B87-DEB4-4C6D-A824-96A2C9D66A36}.Release|x64.ActiveCfg = Release|x64 21 | {F2450B87-DEB4-4C6D-A824-96A2C9D66A36}.Release|x64.Build.0 = Release|x64 22 | {F2450B87-DEB4-4C6D-A824-96A2C9D66A36}.Release|x86.ActiveCfg = Release|Win32 23 | {F2450B87-DEB4-4C6D-A824-96A2C9D66A36}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {304135C2-4351-4D6E-8BF6-4F76748B8922} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /WinPrintServer/WinPrintServer.cpp: -------------------------------------------------------------------------------- 1 | // WinPrintServer.cpp : This file contains the 'main' function. Program execution begins and ends there. 2 | // 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | LPCWSTR dataType; 9 | std::wstring printerName = L"EPSON098CEF (WF-3520 Series)"; 10 | int printerPort = 9100; 11 | 12 | void log(LPCWSTR format, ...); 13 | void logFatal(LPCWSTR msg, int errCode); 14 | void logError(LPCWSTR msg, int errCode); 15 | 16 | void showUsage(); 17 | 18 | int wmain(int argc, wchar_t* argv[]) 19 | { 20 | WORD wVersionRequested = MAKEWORD(2, 2); 21 | WSADATA wsaData; 22 | 23 | int status = WSAStartup(wVersionRequested, &wsaData); 24 | if (status != 0) 25 | { 26 | logFatal(L"failed to initialize the socket library", status); 27 | } 28 | 29 | if (argc == 1) 30 | { 31 | DWORD cb; 32 | if (!GetDefaultPrinter(NULL, &cb) && GetLastError() != ERROR_INSUFFICIENT_BUFFER) 33 | { 34 | logFatal(L"no default printer defined", GetLastError()); 35 | } 36 | LPWSTR szBuffer = new WCHAR[cb]; 37 | GetDefaultPrinter(szBuffer, &cb); 38 | printerName = szBuffer; 39 | delete[]szBuffer; 40 | } 41 | else if (argc == 2) 42 | { 43 | if (_wcsicmp(argv[1], L"-h") == 0) 44 | { 45 | showUsage(); 46 | } 47 | printerName = argv[1]; 48 | } 49 | else 50 | { 51 | showUsage(); 52 | } 53 | 54 | SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0); 55 | if (serverSocket == INVALID_SOCKET) 56 | { 57 | logFatal(L"failed to create server socket", WSAGetLastError()); 58 | } 59 | 60 | sockaddr_in addr{}; 61 | addr.sin_family = AF_INET; 62 | addr.sin_port = htons(printerPort); 63 | addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); 64 | 65 | if (bind(serverSocket, (const sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) 66 | { 67 | closesocket(serverSocket); 68 | logFatal(L"failed to bind server socket", WSAGetLastError()); 69 | } 70 | 71 | sockaddr_in client{}; 72 | int size = sizeof(client); 73 | SOCKET clientSocket = INVALID_SOCKET; 74 | HANDLE hPrinter = INVALID_HANDLE_VALUE; 75 | 76 | if (!OpenPrinter(const_cast(printerName.c_str()), &hPrinter, NULL)) 77 | { 78 | logFatal(L"printer not available", GetLastError()); 79 | } 80 | 81 | DWORD cb; 82 | DWORD lerr; 83 | BOOL succeeded = GetPrinterDriver(hPrinter, NULL, 8, NULL, 0, &cb); 84 | if (!succeeded) 85 | { 86 | lerr = GetLastError(); 87 | if (lerr != ERROR_INSUFFICIENT_BUFFER) 88 | { 89 | logFatal(L"failed to get printer driver", lerr); 90 | ClosePrinter(hPrinter); 91 | } 92 | } 93 | 94 | BYTE *pdiBuffer = new BYTE[cb]; 95 | succeeded = GetPrinterDriver(hPrinter, NULL, 8, pdiBuffer, cb, &cb); 96 | if (!succeeded) 97 | { 98 | logFatal(L"failed to get printer driver", GetLastError()); 99 | ClosePrinter(hPrinter); 100 | } 101 | DRIVER_INFO_8* pdi8 = (DRIVER_INFO_8*)pdiBuffer; 102 | if ((pdi8->dwPrinterDriverAttributes & PRINTER_DRIVER_XPS) != 0) 103 | { 104 | dataType = L"XPS_PASS"; 105 | } 106 | else 107 | { 108 | dataType = L"RAW"; 109 | } 110 | delete[]pdiBuffer; 111 | ClosePrinter(hPrinter); 112 | 113 | log(L"Print Server Started for '%s'", printerName.c_str()); 114 | while (listen(serverSocket, 5) != SOCKET_ERROR) 115 | { 116 | log(L"waiting for connection"); 117 | clientSocket = accept(serverSocket, (sockaddr*)&client, &size); 118 | if (clientSocket == INVALID_SOCKET) 119 | { 120 | logError(L"failed to accept client connection", WSAGetLastError()); 121 | continue; 122 | } 123 | log(L"connection from %d.%d.%d.%d", client.sin_addr.S_un.S_un_b.s_b1, client.sin_addr.S_un.S_un_b.s_b2, client.sin_addr.S_un.S_un_b.s_b3, client.sin_addr.S_un.S_un_b.s_b4); 124 | 125 | DOC_INFO_1 di{}; 126 | di.pDocName = const_cast(L"RAW Print Job"); 127 | di.pOutputFile = NULL; 128 | di.pDatatype = const_cast(dataType); 129 | 130 | if (!OpenPrinter(const_cast(printerName.c_str()), &hPrinter, NULL)) 131 | { 132 | logError(L"printer not available", GetLastError()); 133 | goto cleanup; // Yes, yes I am sure you can do better. 134 | } 135 | 136 | if (StartDocPrinter(hPrinter, 1, (LPBYTE)&di) <= 0) 137 | { 138 | logError(L"failed to start the print job", GetLastError()); 139 | goto cleanup; 140 | } 141 | 142 | log(L"print job started"); 143 | char buffer[4096]; 144 | DWORD bytesWritten; 145 | while (true) 146 | { 147 | int bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0); 148 | if (bytesRead <= 0) 149 | { 150 | break; 151 | } 152 | BOOL result = WritePrinter(hPrinter, buffer, bytesRead, &bytesWritten); 153 | if (!result || bytesWritten != bytesRead) 154 | { 155 | logError(L"print failed", GetLastError()); 156 | break; 157 | } 158 | } 159 | EndDocPrinter(hPrinter); 160 | log(L"print job completed"); 161 | 162 | cleanup: 163 | if (clientSocket != INVALID_SOCKET) 164 | { 165 | closesocket(clientSocket); 166 | } 167 | 168 | if (hPrinter != INVALID_HANDLE_VALUE) 169 | { 170 | ClosePrinter(hPrinter); 171 | } 172 | } 173 | closesocket(serverSocket); 174 | WSACleanup(); 175 | } 176 | 177 | void logFatal(LPCWSTR msg, int errCode) 178 | { 179 | std::wcerr << L"fatal: " << msg << L"(" << errCode << L")" << std::endl; 180 | exit(-1); 181 | } 182 | 183 | void logError(LPCWSTR msg, int errCode) 184 | { 185 | std::wcerr << L"error: " << msg << L"(" << errCode << L")" << std::endl; 186 | } 187 | 188 | void log(LPCWSTR format, ...) 189 | { 190 | va_list args; 191 | va_start(args, format); 192 | vwprintf(format, args); 193 | va_end(args); 194 | printf("\r\n"); 195 | } 196 | 197 | void showUsage() 198 | { 199 | log(L"WinPrintServer v1.1\r\n"); 200 | log(L"WinPrintServer [options] [printername]"); 201 | log(L" -h Show this help information"); 202 | log(L" printername The name of the printer to print. If not specified the default printer is used"); 203 | exit(-1); 204 | } 205 | 206 | -------------------------------------------------------------------------------- /WinPrintServer/WinPrintServer.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {f2450b87-deb4-4c6d-a824-96a2c9d66a36} 25 | WinPrintServer 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | true 75 | 76 | 77 | false 78 | 79 | 80 | true 81 | 82 | 83 | false 84 | 85 | 86 | 87 | Level3 88 | true 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Level3 100 | true 101 | true 102 | true 103 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 104 | true 105 | 106 | 107 | Console 108 | true 109 | true 110 | true 111 | 112 | 113 | 114 | 115 | Level3 116 | true 117 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | Ws2_32.lib;%(AdditionalDependencies) 124 | 125 | 126 | 127 | 128 | Level3 129 | true 130 | true 131 | true 132 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 133 | true 134 | 135 | 136 | Console 137 | true 138 | true 139 | true 140 | Ws2_32.lib;%(AdditionalDependencies) 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /WinPrintServer/WinPrintServer.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /WinPrintServer/WinPrintServer.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | WindowsLocalDebugger 7 | 8 | --------------------------------------------------------------------------------